Drag a form in the customer area

Source: Internet
Author: User

When you design an applicationProgramYou may want to be able to drag the form through the customer area. For example, when the form does not have a title bar or creates an irregular form, you can only drag the form through the customer area.

A good example is Microsoft Windows Media Player. Media player has a function that allows you to change the appearance (skin) based on your interests. The title bar is hidden at this time, but you can drag the form through the customer area.

First, you must understand the message transmission mechanism in windows. When a message is active with a mouse, the system sends the wm_nchittest message to the form to determine the message location. If you click the title bar and the message value received by the form is htcaption, similarly, if the received message is htclient, the user clicks the customer area, that is, the mouse message occurs in the customer zone.

 

When implemented using MFC, the system responds to the wm_nchittest event function. When you press the left mouse button in the client area, htcaption is returned, deceiving windows to achieve the same effect as dragging the window in the title bar.

 
Uint cfloatwnd: onnchittest (cpoint pt) {uint nhittest = cdialog: onnchittest (PT); If (nhittest = htclient &: getasynckeystate (mk_lbutton) <0) // If you press the left mouse button, the return value of the getasynckeystate function is less than 0 nhittest = htcaption; return nhittest ;}

C # during implementation, when the wndproc method of the form is reloaded, You can intercept the message wm_nchittest and modify the message. When the mouse event occurs in the customer zone, rewrite the message and send the htcaption to the form, in this way, the message received by the form is htcaption, And you can drag the form in the customer area with the mouse as if you were dragging the form through the title bar.

Note: After you reload wndproc and rewrite the mouse event, the mouse event of the entire form changes accordingly.

Example:
1. Create a C # project file. The default form is form1.

2. Click code on the view panel.

3. Set the followingCodePaste it to the form1 class

Private const int wm_nchittest = 0x84;
Private const int htclient = 0x1;
Private const int htcaption = 0x2;

4. Rewrite the 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 );
}

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.