To drag a form in the client area with C # implementation

Source: Internet
Author: User
When you design an application, you may want the user to be able to drag the form through the client area, such as when the form does not have a title bar or create an irregular form, and the form is dragged through the client area.

A good example of what is happening around us is Microsoft Windows Media Player. Media player has a feature that allows you to change the appearance (skin-changing) of the user's hobby, and the title bar is hidden, but you can drag the form through the client area.

All right, let's get down to our journey.

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);
}

5. Save and run the project.

6. Try, click the form anywhere, is not all can drag the form ah?



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.