Original: lover_p
The so-called "dragable form" is a form that can be dragged without dragging the title bar, which is a very useful technology on a form without the title bar. About this technical skills, there are many introductions on the Internet, but they are not very detailed, and some implementations are not simple enough. Most of these introductions, most of them are the source code of large pieces, very little explanation; beginners semester will probably cost some strength (I am very painful when I study this skill). Here, I made a detailed explanation of this technique. I believe that beginners can learn how to make dragable forms. (Note: The readers expected in this article are those who have some C # develop Windows programs but temporarily do not produce dragable forms.) First, let's review a lower Windows form. Please see this picture:
Figure 1 Windows Form
This is a formal standard Windows form. First, the top of the form is a title bar, and the remaining portion is the body of the form, surrounding the outer frame per circumservation of the form body, the border is not the user area of the control or drawing graphics. A method of obtaining a size of various form constructor is also labeled. For the user area, System.Windows.Forms.form provides instance attribute clientsize, I believe everyone is already familiar. For the size of the active form, such as the title bar, border, etc.), we can use a class. Net class library: system.windows.forms.systeminformation, this class provides some static Attributes, such as Captionheight, which represents the height of the title bar. Information about the SystemInformation class can be found at the .NET SDK Document Directory .NET Framework SDK -> Reference -> Class Library -> System.Windows.Forms -> SystemInformation class (Note: The hyperlink here is only installed) The .NET Framework 1.1 Simplified Chinese version is also valid when a supporting document is installed. This is a very useful class, I hope everyone can remember it (maybe you know, but I know -_-sweat ~~). Next, let's see how to move the form when dragging the mouse in the user area. Please see this picture below:
Figure 2 Movement of the form
Let's observe the movement of the mouse in the form and the movement of the form, it is easy to find that during the process of being dragged, the relative position of the mouse in the form is always constant! Then, we only need to detect the movement of the mouse on the screen and modify the location of the form, you can reach 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 your mouse is in your screen? Here is to mention a class: System.Windows.Forms.Control class. Maybe you will be very surprised: Is this not a base class for all controls? Oh ~ is this di. However, even though this, the Control class is not declared as abstract classes like other widely used base classes, and it provides a static property: mousePosition, through this property, you can get the position of the mouse relative to the screen. Information about the Control class can be found in the .NET Framework SDK -> Reference -> Class Library -> System.Windows.Forms -> Control class (Note: The hyperlink here is only installed. " The .NET Framework 1.1 Simplified Chinese version is also valid when a supporting document is installed. After knowing how to get this information, the production of mobile forms is actually a very simple problem. The basic process is this: First, in the mouse (left button or a key you like), record the mouse position; You can calculate the change in the location of the form (assuming mouseposition has the system.drawing.point type, indicating the relative coordinate of the mouse in the form): // Sample code 1Form.top = control.mousePosition.y - mousePosition.y; form .Left = control.mouseposition.x - mousePosition.x; This is not yet, because our mousePosition is represented by the relative coordinate of the mouse in the form user area, but in the mobile form, consider the form header bar and The size of the border. On the basis, we correct the code to: // sample code 2Form.top = control.mouseposition.y - mouseposition.y - systeminformation.framebordersize.Height - Systeminformation.captionHeight; form.left = control.mouseposition.y - MousePosition.y - systeminformation.framebordersize.width; That is, on the height (ordinate) to subtract the height of the title bar and the height of the border, on the width (horizontal) to subtract the width of the border. However, when making a dragable form that has neither the title bar does not have a border, it is possible to use the code shown in Example Code 1. The above code is just an exemplary code.
The specific operation is as follows: First, add a private domain to the form: private system.drawing.point mousepoint; then add the mouse to the form, press the event processing method (I am mainform_mousedown, don't forget to link the method link to Mainform .Mousedown event, this doesn't have to say it?): Private void mainform_mousedown (object sender, system.windows.Forms.MouseEventArgs e) {if (e.button == mousebuttons.left) {this.mouseposition.x = EX; This.MousePosition.y = EY;}} Note here to filter the mouse button. Next, add a mouse mobile event processing method for the form (I am 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 is not The title bar can remove "-Systeminformation.captionHeight"; if there is no border, you can remove "-systeminformation.framebordersize.height" and "-systeminformation.framebordersize.width". Now, is you very clear about how to use C # production to drag the window? congratulations! Finally, send you a small gift - desktop clock. Very simple, very interesting ~~~ additional tips three: How to make an irregular form This skill I have not very rich, now introduce a simple method - by setting the form of the region property. First, declare a system.drawing.drawing2d.graphicspath variable, add your expectations as the shape combination of the form outline; then generate a system.drawing.region instance from this variable, and assign the form property I.e. Such as: 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 of constructor, We can get a custom form, this form is a circular shape having a diameter of 120 pixels (also the appearance of my clock).
How to generate a form without a border to set the form's FORMDERDERSTYLE to NONE. Is this simple matter? So how do you generate a border but no title bar first set the form's ControlBox property to false, that is, do not control buttons (form icon, maximum minimization button, and shut down). According to this, there should be no title bar, but we can still see it is annoying to stay there, so we have to set the form's Text property (note that it is string.empty, not null).