MFC non-title bar dialog box Move processing method

Source: Internet
Author: User

Many times, for the sake of aesthetics of the interface, we will hide the title bar, while in the customer area by self-painting simulation title bar. However, this processing can cause the form to not move. Here are a few ways to move:

1, the use of deception way. That is, when the mouse clicks on the customer area, we cheat windows, let Windows think we clicked in the title bar, so that the drag process through Windows processing. So we can greatly reduce the amount of code. Here's how:

In response to the left mouse button press the message wm_lbuttondown, the corresponding function, directly send spoofed message:

PostMessage (Wm_nclbuttondown, Htcaption, Makelparam (Point.x, Point.y));

Wm_nclbuttondown the left button is pressed in the non-client area, Htcaption is the title bar, so in this way, you can implement the No title bar dialog box move problem.

So here's the problem.

With this kind of operation, our dialog will not be able to respond to the WM_LBUTTONUP message, and I find that wm_nclbuttonup is generally not responsive when I write the program. Of course, you can register wm_mousehover and wm_mouseleave messages, in response to the two messages to deal with the corresponding code, or the use of the next hook method, the hook to send the correct message, but this method is not recommended, the implementation is also more troublesome.

So here's a second method.

2, in the WM_MOUSEMOVE message, the processing window to move. The specific is to determine whether the left mouse button is pressed, if so, get the mouse to move to the location, and then move the window can be. The specific implementation is as follows:

Void cmydlg::onmousemove (Uint nflags, cpoint point)   {       static cpoint prepoint = cpoint (0, 0);       if (mk_lbutton == nflags)       {            if (Point != prepoint)             {                 CPoint ptTemp = point - PrePoint;                 CRect rcWindow;                 getwindowrect (&rcWindow);                 rcwindow.offsetrect ( PTTEMP.X, PTTEMP.Y);  &nbsP;              movewindow (&rcWindow );                return  ;           }        }      prepoint = point;      cdialog::o Nmousemove (nflags, point);  }

Through the above message, you can move the window. However, if only response to the above message found two problems: first, when the mouse press and hold the left button quickly move, the mouse will move out of the window, the window does not move, the drag effect is not good; second, the left-hand release message does not respond in this case. Here's how to fix it:

First left-click to set mouse capture in the message

SetCapture ();

Release mouse capture in left-click Release message

Realesecapture ();

Through the above processing, you can drag perfectly, while the left-hand raised message will not be affected.


Therefore, the first method is more concise, suitable for the situation that does not need to respond to other messages of the mouse, the second method is relatively troublesome, but the implementation is also very good, and does not affect other messages.

MFC non-title bar dialog box Move processing method

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.