When the user removes the Winform form border and sets the appearance of the form by himself, the user cannot use the functions of the Windows Forms application to change the size of the custom form at will.
At this time, manually changing the size of the borderless form requires customizing the method of changing the form size to enable the right, bottom, and bottom right corner of the custom form to change the size of the form by dragging the mouse. When changing the size of the homemade form, the Position property of the Cursor class and the Width, Height, Top and Left properties of the form are mainly used. In addition, the calculation method of changing the form size is also used.
Position properties of the class
The Cursor class represents an image used to draw a mouse pointer, and its Position property is used to obtain or set the cursor position. The syntax format of this property is as follows:
public static Point Position {get;set;}
Parameter description
Attribute value: Point (using screen coordinates) structure that represents the cursor position.
2. Calculation method for changing the size of the form
The important algorithm is how to set the size of the form according to the movement of the mouse: first, use the X coordinate value of the current mouse on the screen to subtract the distance between the left margin of the form and the left margin of the screen, then add the distance between the mouse and the right end of the border, and set the value to the width of the current form. The implementation code is as follows:
=+( -Example_X);
Where Form is the form name, Pan is the control name that displays the form border, and Example_X is the X coordinate value when the mouse is pressed. Note: The above calculation method is executed in the MouseMove event of the Panel control.
design
(1) Resources resource picture management
(2) GetObject method design
The properties of the image resource are automatically generated, but the GetObject method to obtain or load images in the program needs to be manually designed.
internal static Image GetObject(string v) { return v switch { "panel_TitleLeft" => panel_TitleLeft, "panel_TitleMiddle" => panel_TitleMiddle, "panel_TitleRight" => panel_TitleRight, "panel_Left" => panel_Left, "panel_Right" => panel_Right, "panel_BottomLeft" => panel_BottomLeft, "panel_BottomMiddle" => panel_BottomMiddle, "panel_BottomRight" => panel_BottomRight, "pictureBox1_Image" => pictureBox1_Image, _ => null }; }
// //------------------------------------------------------------------------------ // <auto-generated> // This code is generated by the tool.// Runtime version: 4.0.30319.42000// // Changes to this file may cause incorrect behavior and if// Regenerate the code and these changes will be lost.// </auto-generated> //------------------------------------------------------------------------------ namespace _164.Properties { using System; /// <summary> /// A strongly typed resource class used to find localized strings, etc. /// </summary> // This class is by StronglyTypedResourceBuilder // The class is automatically generated through a tool similar to ResGen or Visual Studio. // To add or remove members, edit the .ResX file and rerun ResGen // (use /str as command option), or regenerate the VS project. [global::("", "17.0.0.0")] [global::()] [global::()] internal class Resources { private static global:: resourceMan; private static global:: resourceCulture; [global::("", "CA1811:AvoidUncalledPrivateCode")] internal Resources() { } /// <summary> /// Returns the cached ResourceManager instance used by this class. /// </summary> [global::(global::)] internal static global:: ResourceManager { get { if ((resourceMan, null)) { global:: temp = new global::("_164.", typeof(Resources).Assembly); resourceMan = temp; } return resourceMan; } } /// <summary> /// Rewrite the CurrentUICulture property of the current thread, for /// Perform rewrite using all resource lookups for this strongly typed resource class. /// </summary> [global::(global::)] internal static global:: Culture { get { return resourceCulture; } set { resourceCulture = value; } } /// <summary> /// Find a localized resource of type. /// </summary> internal static panel_BottomLeft { get { object obj = ("panel_BottomLeft", resourceCulture); return (()(obj)); } } /// <summary> /// Find a localized resource of type. /// </summary> internal static panel_BottomMiddle { get { object obj = ("panel_BottomMiddle", resourceCulture); return (()(obj)); } } /// <summary> /// Find a localized resource of type. /// </summary> internal static panel_BottomRight { get { object obj = ("panel_BottomRight", resourceCulture); return (()(obj)); } } /// <summary> /// Find a localized resource of type. /// </summary> internal static panel_Left { get { object obj = ("panel_Left", resourceCulture); return (()(obj)); } } /// <summary> /// Find a localized resource of type. /// </summary> internal static panel_Right { get { object obj = ("panel_Right", resourceCulture); return (()(obj)); } } /// <summary> /// Find a localized resource of type. /// </summary> internal static panel_TitleLeft { get { object obj = ("panel_TitleLeft", resourceCulture); return (()(obj)); } } /// <summary> /// Find a localized resource of type. /// </summary> internal static panel_TitleMiddle { get { object obj = ("panel_TitleMiddle", resourceCulture); return (()(obj)); } } /// <summary> /// Find a localized resource of type. /// </summary> internal static panel_TitleRight { get { object obj = ("panel_TitleRight", resourceCulture); return (()(obj)); } } /// <summary> /// Find a localized resource of type. /// </summary> internal static pictureBox1_Image { get { object obj = ("pictureBox1_Image", resourceCulture); return (()(obj)); } } internal static Image GetObject(string v) { return v switch { "panel_TitleLeft" => panel_TitleLeft, "panel_TitleMiddle" => panel_TitleMiddle, "panel_TitleRight" => panel_TitleRight, "panel_Left" => panel_Left, "panel_Right" => panel_Right, "panel_BottomLeft" => panel_BottomLeft, "panel_BottomMiddle" => panel_BottomMiddle, "panel_BottomRight" => panel_BottomRight, "pictureBox1_Image" => pictureBox1_Image, _ => null }; } } }
4. Example
// Manually change the size of the homemade formusing _164.Properties; namespace _164 { public partial class Form1 : Form { private Panel? panel_BR; private Panel? panel_Center; private Panel? panel_TitleRight; private Panel? panel_BottomLeft; private Panel? panel_BottomMiddle; private PictureBox? pictureBox1; private Panel? panel_TitleLeft; private Panel? panel_Right; private Panel? panel_Left; private Panel? panel_Title; private Panel? panel_Bottom; private Panel? panel_TitleMiddle; private static int example_X = 0; private static int example_Y = 0; private static int example_W = 0; private static Point cPoint; public static int Example_X { get => example_X; set => example_X = value; } public static int Example_Y { get => example_Y; set => example_Y = value; } public static int Example_W { get => example_W; set => example_W = value; } public static Point CPoint { get => cPoint; set => cPoint = value; } public Form1() { InitializeComponent(); Load += Form1_Load; } private void Form1_Load(object? sender, EventArgs e) { // // panel_Right // panel_Right = new Panel { BackColor = , BackgroundImage = ("panel_Right")!, BackgroundImageLayout = , Cursor = , Dock = , Location = new Point(286, 19), Name = "panel_Right", Size = new Size(6, 96), TabIndex = 10 }; panel_Right.MouseDown += new MouseEventHandler(Panel_Right_MouseDown!); panel_Right.MouseMove += new MouseEventHandler(Panel_Right_MouseMove!); // // panel_Left // panel_Left = new Panel { BackColor = , BackgroundImage = ("panel_Left")!, BackgroundImageLayout = , Dock = , Location = new Point(0, 19), Name = "panel_Left", Size = new Size(6, 96), TabIndex = 9 }; panel_Left.MouseDown += new MouseEventHandler(Panel_Right_MouseDown!); panel_Left.MouseMove += new MouseEventHandler(Panel_Right_MouseMove!); // // panel_Center // panel_Center = new Panel { BackColor = , Dock = , Location = new Point(6, 19), Name = "panel_Center", Size = new Size(280, 96), TabIndex = 13 }; // // panel_TitleRight // panel_TitleRight = new Panel { BackColor = , BackgroundImage = ("panel_TitleRight")!, BackgroundImageLayout = , Dock = , Location = new Point(284, 0), Name = "panel_TitleRight", Size = new Size(8, 19), TabIndex = 1, Tag = "3" }; panel_TitleRight.MouseDown += new MouseEventHandler(Panel_TitleLeft_MouseDown!); panel_TitleRight.MouseMove += new MouseEventHandler(Panel_TitleLeft_MouseMove!); // // panel_TitleMiddle // panel_TitleMiddle = new Panel { BackColor = , BackgroundImage = ("panel_TitleMiddle")!, BackgroundImageLayout = , Dock = , Location = new Point(12, 0), Name = "panel_TitleMiddle", Size = new Size(272, 19), TabIndex = 2, Tag = "2" }; panel_TitleMiddle.(pictureBox1); panel_TitleMiddle.MouseDown += new MouseEventHandler(Panel_TitleLeft_MouseDown!); panel_TitleMiddle.MouseMove += new MouseEventHandler(Panel_TitleLeft_MouseMove!); panel_TitleMiddle.SuspendLayout(); // // pictureBox1 // pictureBox1 = new PictureBox { Anchor = | , BackColor = , Cursor = , Image = ("")!, SizeMode = , Location = new Point(260, 3), Name = "pictureBox1", Size = new Size(11, 11), TabIndex = 0, TabStop = false, Tag = "11" }; += new EventHandler(PictureBox1_Click!); // // panel_TitleLeft // panel_TitleLeft = new Panel { BackColor = , BackgroundImage = ("panel_TitleLeft")!, BackgroundImageLayout = , Dock = , Location = new Point(0, 0), Name = "panel_TitleLeft", Size = new Size(12, 19), TabIndex = 0, Tag = "1" }; panel_TitleLeft.MouseDown += new MouseEventHandler(Panel_TitleLeft_MouseDown!); panel_TitleLeft.MouseMove += new MouseEventHandler(Panel_TitleLeft_MouseMove!); // // panel_BottomLeft // panel_BottomLeft = new Panel { BackColor = , BackgroundImage =("panel_BottomLeft")!, BackgroundImageLayout = , Cursor = , Dock = , Location = new Point(0, 0), Name = "panel_BottomLeft", Size = new Size(8, 10), TabIndex = 0 }; // // panel_BottomMiddle // panel_BottomMiddle = new Panel { BackColor = , BackgroundImage = ("panel_BottomMiddle")!, BackgroundImageLayout = , Cursor = , Dock = , Location = new Point(8, 0), Name = "panel_Bn", Size = new Size(276, 10), TabIndex = 2 }; panel_BottomMiddle.MouseDown += new MouseEventHandler(Panel_Right_MouseDown!); panel_BottomMiddle.MouseMove += new MouseEventHandler(Panel_Right_MouseMove!); // // panel_BottomRight // panel_BR = new Panel { BackColor = , BackgroundImage = ("panel_BottomRight")!, BackgroundImageLayout = , Cursor = , Dock = , Location = new Point(284, 0), Name = "panel_BR", Size = new Size(8, 10), TabIndex = 1 }; panel_BR.MouseDown += new MouseEventHandler(Panel_Right_MouseDown!); panel_BR.MouseMove += new MouseEventHandler(Panel_Right_MouseMove!); // // panel_Title // panel_Title = new Panel { BackColor = , Dock = , Location = new Point(0, 0), Name = "panel_Title", Size = new Size(292, 19), TabIndex = 7 }; panel_Title.(panel_TitleMiddle); panel_Title.(panel_TitleRight); panel_Title.(panel_TitleLeft); panel_Title.SuspendLayout(); // // panel_Bottom // panel_Bottom = new Panel { BackColor = , Dock = , Location = new Point(0, 115), Name = "panel_Bottom", Size = new Size(292, 10), TabIndex = 8 }; panel_Bottom.(panel_BottomMiddle); panel_Bottom.(panel_BR); panel_Bottom.(panel_BottomLeft); panel_Bottom.SuspendLayout(); // // Form1 // AutoScaleDimensions = new SizeF(6F, 12F); AutoScaleMode = ; ClientSize = new Size(292, 125); (panel_Center); (panel_Right); (panel_Left); (panel_Title); (panel_Bottom); FormBorderStyle = ; Name = "Form1"; Text = "Form1"; panel_BottomMiddle.ResumeLayout(false); panel_Title.ResumeLayout(false); panel_Bottom.ResumeLayout(false); } #region Use controls on the form to move the form /// <summary> /// Use controls to move the form /// </summary> /// <param Frm="Form">Form</param> /// <param e="MouseEventArgs">Move Events of Control</param> public static void FormMove(Form Frm, MouseEventArgs e) //Form or MouseEventArgs add namespace using; { if ( == ) { Point myPosittion = ;//Get the screen coordinates of the current mouse (, );//Reload the current mouse position = myPosittion;//Set the current position of the form on the screen } } #endregion #region Get the current location of the mouse /// <summary> /// Get the current location of the mouse /// </summary> /// <param Frm="Form">Form</param> /// <param e="MouseEventArgs">Some information about the mouse on the form</param> public static void FormScreenSizeInfo(Form Frm, MouseEventArgs e) { if ( == ) { Example_X = ; Example_Y = ; Example_W = ; } } #endregion #region Change the size of the form (used for mouse movement events) /// <summary> /// Change the size of the form (used for mouse movement events) /// </summary> /// <param Frm="Form">Form</param> /// <param Pan="Panel">Controls that set the form border</param> /// <param e="MouseEventArgs">Some information about the mouse on the form</param> public void FormScreen_EnlargeSize(Form Frm, Panel Pan, MouseEventArgs e) { if ( == ) { switch () { case "panel_Right": //If the right border of the form is moved { if (Width <= 70) //If the width of the form is less than or equal to 70 { = 70; //Set the width of the form to 70 //If you move the right border of the form with the mouse to the right if ( - + ( - Example_X) > ) { //Add to increase the width of the form according to the mouse movement value = - + ( - Example_X); } break; } //Add to increase the width of the form according to the mouse movement value = - + ( - Example_X); break; } case "panel_BR": //If you move the lower right corner of the form { //If the size of the form is not the minimum value of the form size if (Width > 70 && Height > (panel_Title!.Height + panel_BottomMiddle!.Height + 1)) { //Change the size of the form according to the mouse movement = - + ( - Example_Y); = - + ( - Example_X); } else { if (Width <= 70) //If the width of the form is less than or equal to the minimum value { = 70; //Set the width of the form to 70 if (Height <= (panel_Title!.Height + panel_BottomMiddle!.Height + 1))//If the height of the form is less than the minimum value { = panel_Title.Height + panel_BottomMiddle.Height + 1;//Set the minimum height of the form //If you move the bottom border of the form with the mouse down if ( - + ( - Example_Y) > ) { //Add to increase the height of the form according to the mouse movement value = - + ( - Example_Y); } break; } //If you move the form to the right with the mouse if ( - + ( - Example_X) > ) { //Increase the width of the form = - + ( - Example_X); } break; } if (Height <= (panel_Title!.Height + panel_BottomMiddle!.Height + 1))//If the height of the form is less than or equal to the minimum value { = panel_Title.Height + panel_BottomMiddle.Height + 1;//Set the height of the form to the minimum value = - + ( - Example_X);//Change the width of the form //If you move the border of the form down with the mouse if ( - + ( - Example_Y) > ) { = - + ( - Example_Y);//Increase the height of the form } break; } } break; } case "panel_Bn"://If the bottom border of the form is moved { if (Height <= (panel_Title!.Height + panel_BottomMiddle!.Height + 1))//If the height of the form is less than or equal to the minimum value { = panel_Title.Height + panel_BottomMiddle.Height + 1;//Set the height of the form to the minimum value //If you use the mouse to move the lower border of the form down if ( - + ( - Example_Y) > ) { = - + ( - Example_Y); //Increase the height of the form } break; } = - + ( - Example_Y); //Increase the height of the form break; } } } } #endregion private void Panel_Right_MouseDown(object sender, MouseEventArgs e) { FormScreenSizeInfo(this, e);//Get the current location of the mouse } private void Panel_Right_MouseMove(object sender, MouseEventArgs e) { FormScreen_EnlargeSize(this, (Panel)sender, e);//Change the size of the form } private void PictureBox1_Click(object sender, EventArgs e) { Close(); } private void Panel_TitleLeft_MouseDown(object sender, MouseEventArgs e) { int Tem_X = -; if (Convert.ToInt16(((Panel)sender).Tag!.ToString()) == 2)//If the moving is the middle part of the title bar Tem_X = - - panel_TitleLeft!.Width; if (Convert.ToInt16(((Panel)sender).Tag!.ToString()) == 3)//If the header is moved is the end of the title bar Tem_X = -(Width - ((Panel)sender).Width) - ; CPoint = new Point(Tem_X, -); } private void Panel_TitleLeft_MouseMove(object sender, MouseEventArgs e) { FormMove(this, e); } } }
This is the article about how to manually change the size of a homemade form in C#. For more related content related to C#, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!