The so-called "removable form" is a form that can be dragged without the need to drag the title bar. This is a very useful technology on a form without a title bar. There have been many introductions on the Internet about this aspect, but they are not very detailed, and some implementations are not concise. Most importantly, these introductions are mostly the source code of a large film, which is rarely explained. I am afraid it will take some effort for beginners over the term (I am very painful to study this technique ). Here, I have provided a detailed explanation of this technique. I believe that beginners can learn how to create a drag-and-drop form.
(Note: The readers expected in this article are those who must use C # To develop the Windows program, but will not yet create a readable form .)
First, let's review the composition of the next Windows form. Please refer to this figure:
Figure 1 Composition of Windows Forms
This is a formal standard Windows form. First, the top of the form is a title bar, the rest is the form body, surrounded by a border around the form body, the border is not the user area where we place controls or draw images.
The figure also shows how to obtain the size of the elements of various forms. For the user area, System. Windows. Forms. Form provides the ClientSize attribute of the instance. I believe you are familiar with it. We can use this function to create a general form element (such as the title bar and border.. NET Class Library provides a class: System. windows. forms. systemInformation. This class provides some static attributes, such as CaptionHeight, which indicates the height of the title bar. Information about the SystemInformation class can be found in.. net sdk documentation directory ". NET Framework SDK-> reference-> class library-> System. windows. forms-> SystemInformation class (note: the hyperlink here is only installed by you.. NET Framework 1.1 (Simplified Chinese Version ). This is a very useful class. I hope you can remember it (you may have known it long ago, but I only know it-_-Khan ~~).
Next, let's take a look at how to move the form when dragging the mouse in the user area. See the figure below:
Figure 2 move a form
We can observe the position of the mouse in the form and the movement of the form. It is easy to find that the relative position of the mouse in the form remains unchanged when the form is dragged! Then, we only need to detect the mouse moving in the screen and modify the position of the form to achieve the purpose of dragging the form!
We know that in mouse message/event processing, we can only get the position of the mouse relative to the form. So, how do you know where the mouse is on the screen? Here we need to mention a class: System. Windows. Forms. Control class. Maybe you will be surprised: Isn't this the base class of all controls? Haha ~ Yes. di. However, even so, the Control class is not declared as an abstract class as other widely used base classes, and it provides a static attribute: MousePosition, you can use this attribute to obtain the position of the mouse relative to the screen. Information about the Control class can be found in. NET Framework document directory ". NET Framework SDK-> reference-> class library-> System. windows. forms-> Control class (note: the hyperlink here is only installed on you.. NET Framework 1.1 (Simplified Chinese Version ).
After learning how to obtain the information, making a mobile form actually becomes a very simple problem. The basic process is as follows: first, when you press the mouse (left button or a key you like), // sample code 1
Form. Top = Control. MousePosition. Y-mousePosition. Y;
Form. Left = Control. MousePosition. X-mousePosition. X;
This is not enough, because our mousePosition represents the relative coordinates of the mouse in the user area of the form, but the size of the Form title bar and border must be considered when moving the form. On the basis of the above, we corrected the code:
// Sample code 2
Form. Top = Control. MousePosition. Y-mousePosition. Y
-SystemInformation. FrameBorderSize. Height-SystemInformation. CaptionHeight;
Form. Left = Control. MousePosition. Y-mousePosition. Y
-SystemInformation. FrameBorderSize. Width;
That is to say, in height (ordinate), the height of the title bar and the height of the border are subtracted, while in width (abscissa), the width of the border is subtracted. However, when you create a form that can be dragged without a title bar or a border, use the code shown in "sample code 1.
The above code is just a demo code. The specific operations are as follows:
First, add a private domain to the form:
Private System. Drawing. Point mousePoint;
Then, add the mouse-pressed event processing method for the form (I am MainForm_MouseDown here. Don't forget to link this method to the MainForm. MouseDown event. Isn't that necessary ?) :
Private void MainForm_MouseDown (object sender, System. Windows. Forms. MouseEventArgs e ){
If (e. Button = MouseButtons. Left ){
This. mousePosition. X = e. X;
This. mousePosition. Y = e. Y;
}
}
Here, you must filter the mouse buttons.
Next, add the mouse movement event processing method for the form (I am here MainForm_MouseMove ):
Private void MainForm_MouseMove (object sender, System. Windows. Forms. MouseEventArgs e ){
If (e. Button = MouseButtons. Left ){
Form. Top = Control. MousePosition. Y-mousePosition. Y
-SystemInformation. FrameBorderSize. Height-SystemInformation. CaptionHeight;
Form. Left = Control. MousePosition. Y-mousePosition. Y
-SystemInformation. FrameBorderSize. Width;
}
}
Here, if the form does not have a title bar, you can remove "-SystemInformation. captionHeight "; if the form does not have a border, you can remove"-SystemInformation. frameBorderSize. height and-SystemInformation. frameBorderSize. width ".
Now, do you have a clear idea of how to use C # To create a window that can be dragged? Congratulations! Finally, we will give you a small gift, the desktop clock. Very concise and interesting ~~~
TIPS:
How to Create irregular forms
I have not learned a lot about this technique. Now I want to introduce the simplest method-by setting the Region attribute of the form. First, declare a System. drawing. drawing2D. graphicsPath variable. In this variable, add the shape Combination you want to use as the form profile. Then, generate a System from this variable. drawing. the Region instance and the Region attribute assigned to the form. For example:
GraphicsPath gp = new GraphicsPath ();
Gp. AddEllipse (0, 0,120,120 );
Region r = new Region (gp );
This. Region = r;
Add the above Code to the form constructor to get a custom form, this form is a circle with a diameter of 120 pixels (also the appearance of my clock ).
How to generate a form without Borders
Set the FormBorderStyle of the form to None. Is this simple? So
How to generate a form with a border but no title bar
First, set the ControlBox attribute of the form to false, that is, do not control the button (Form icon, maximum minimization button, and close button ). In this case, there should be no title bar, but we can still see that it is annoying to stay there. Therefore, we need to set the Text attribute of the form to null (note that it is String. empty, not null ).
Record the mouse position. Because the screen coordinate of the mouse changes while the relative coordinate of the form remains unchanged, we can calculate the change of the form position (assuming that the mousePosition has a System. drawing. point type, indicating the relative coordinates of the mouse in the form ):