You must first understand the message passing mechanism of windows, and when there is a mouse activity message, the system sends a WM_NCHITTEST message to the form as the basis for judging the occurrence of the message. If you click on the title bar, the form received the message value is Htcaption, similarly, if the message received is Htclient, indicating that the user clicks the customer area, that is, the mouse message occurred in the customer area.
When overloading the form's WndProc method, you can intercept the WM_NCHITTEST message and change the message, when judging the mouse event occurs in the client area, rewrite the change message, send the htcaption to the form, so that the message from the form will be htcaption, Dragging a form in the client area by mouse is like dragging through the title bar.
Note: When you overload WndProc and overwrite mouse events, the entire form's mouse events change.
Example:
1. Create a C # engineering file, the default form is Form1.
2. Click Code on the View panel.
3. Paste the following code into the Form1 class
Private Const int wm_nchittest = 0x84;
Private Const int htclient = 0x1;
Private Const int htcaption = 0x2;
4. Overwrite mouse message in Form1
protected override void WndProc (ref message M)
{
Switch (m.msg)
{
Case Wm_nchittest:
Base. WndProc (ref m);
if ((int) M.result = = htclient)
M.result = (IntPtr) htcaption;
Return
Break
}
Base. WndProc (ref m);
}
Although this method can be dragged in the customer area, but if there are similar dividerpanel such as the time, the control can not drag the form, and then use another method, the problem is not resolved, there are talent to study each other.
A "drag form" is a form that you can drag without dragging the title bar, which is a very useful technique in a form without a title bar. On this side of the skills, online has been a lot of introductions, but are not very detailed, some of the implementation is not concise. Most important, these introductions are mostly large tracts of source code, rarely explained, beginners semester to be afraid to take some effort (I am learning this skill is very painful). Here, I have a detailed explanation of this technique, I believe you beginners friends can learn how to make a drag form.
(Note: This article expects readers to be friends who have the basics of developing Windows programs in C # but are not making drag forms for the time being.) )
First, let's review the composition of the next Windows Form. Take a look at this picture:
Figure 1 Windows Forms composition
This is a formalized standard for Windows Forms. First, the top of the form is a title bar, and the rest is the body of the form, surrounded by a border outside the main body of the form, which is not the user area where we place the control or draw the graphic.
The figure also identifies the method of obtaining the dimensions of various form-building elements. For user area, System.Windows.Forms.Form provides instance attribute ClientSize, I believe we are already familiar with it. And to Wooki the general form construction elements (such as title bars, borders, etc.), we can use. NET class Library: System.Windows.Forms.SystemInformation, a class that provides static properties such as CaptionHeight that represent the height of the title bar. Information about the SystemInformation class can be referenced in the. NET SDK Documentation directory. NET Framework SDK-> reference-> class library-> System.Windows.Forms-> systeminform ation class "(Note: The hyperlinks here are only valid if you have installed the. NET Framework 1.1 Simplified Chinese version and have a companion document installed.) This is a very useful class, I hope you can remember it (maybe you already know, but I just know-_-Khan ~ ~).
Next, let's look at how to move the form when you drag the mouse over the user area. Take a look at this picture below:
Figure 2 Movement of the form
We look at the position of the mouse in the window and the movement of the form, it is easy to find: In the process of the form being dragged, the mouse in the relative position of the window is always unchanged. So, as long as we detect the mouse moving on the screen and modify the position of the form, we can achieve the purpose of dragging the form.
We know that in mouse message/event handling, you can only get the position of the mouse relative to the form. So how do you know where the mouse is in the screen? Here again a class: System.Windows.Forms.Control class. You may be surprised: this is not the base class for all controls. Oh ~ is so di. However, while this is the case, the control class is not declared as an abstract class like other widely used base classes, and it provides a static property: MousePosition, which allows the mouse to be positioned relative to the screen. Information about the control class can be found in the. NET Framework Document catalog. NET Framework SDK-> reference-> class library-> System.Windows.Forms-> control class (note: this Hyperlinks are available only if you have installed the. NET Framework 1.1 Simplified Chinese version and installed the supporting documentation.
Once you know how to get this information, making a mobile form is actually a very simple question. The basic process is this: first, record the mouse position when you press the mouse (left or one of your favorite keys), because the screen coordinates of the mouse change while the form is moving, but the relative coordinates are unchanged. We can calculate the change in the position of the form (assuming that mouseposition has a System.Drawing.Point type that represents the relative coordinates of the mouse in the form):
|
Example code 1 Form.top = CONTROL.MOUSEPOSITION.Y-MOUSEPOSITION.Y; Form.left = control.mouseposition.x-mouseposition.x; |
That's not going to work, because our mouseposition represents the relative coordinates of the mouse in the user area of the form, but when you move the form, consider the size of the form's title bar and border. On top of that, we'll revise the code to:
|
Example 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, on the height (ordinate), subtract the height of the title bar and the height of the border, and subtract the width of the border on the width (horizontal axis). However, when you make a drag form that has neither a title bar nor a border, you can use the code shown in "Example code 1".
The above code is just a demonstration code. The specific actions are as follows:
First, add a private field to the form:
|
Private System.Drawing.Point Mousepoint; |
Then, add the mouse down event-handling method for the form (I'm mainform_mousedown here, and don't forget to link the method to the Mainform.mousedown event, which is not much to say.) ):
|
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; } } |
Notice here to filter the mouse button.
Next, add the mouse move event handling method for the form (I'm mainform_mousemove here):
|
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; } } |
The problem has not been solved with this method.