The easiest way to drag windows in wtl !!!

Source: Internet
Author: User

Currently, dialog boxes in many dialog box-based applications do not contain frameworks. That is to say, the dialog box does not have a title bar. As we all know, window movement is implemented by dragging the title bar of the window with the mouse. Now the dialog box in the application does not have the title bar. How do you move the dialog box? This example provides a solution to this problem.


I. Implementation Method

There are two solutions to solve the drag problem without a title bar window. One solution is to use a conventional approach to handle the drag and drop event. When the window gets wm_lbuttondown (onlbuttondown, you can set the flag and call the cwnd: setcapture () function to capture the mouse message in the current window. The application enters the moving mode. When the message wm_mousemove is received, the Framework window can be moved accordingly, finally, when the user releases the mouse button, the wm_lbuttonup Message Processing Routine clears the flag and calls the cwnd: releasecapture () function to return the mouse control to Windows. This method is cumbersome. First, you need to decide where to move the window? Then you need to figure out how to re-paint the window, and check whether the "show window content" check box of "visual effect" item on the "effect" page of the "Display Properties" dialog box on the screen is selected, the drag effect is different. So how do you know the settings? The method is to call systemparametersinfo (spi_getdragfullwindows ). It's really bad for Windows programmers to handle these complicated things with no extra detail.

Because Windows itself knows that you can move the window by clicking the title bar, can you imitate the clicking and dragging behavior of the mouse anywhere in the client area of the window as if it were in the title bar? The answer is yes, which leads to the second method of dragging the window. In fact, it is not difficult to drag the background of the dialog box with the mouse clicked, but you must understand the principle of dragging the window in the title bar. Windows first determines which window the mouse clicks in, then, send a wm_nchittest message to the window to find out which "non-customer zone" (such as border, maximize/minimize button, menu, title, etc.) of the window has the mouse cursor. Then the default Window Process responds to the message and returns a specific code. If the mouse pointer falls in the title bar, the specific code is htcaption. In this case, Windows enters the drag-and-drop mode so that you can move the window. Therefore, to drag the dialog box with the mouse in the customer area, you only need to imitate the mouse drag behavior in the title bar in the customer area. The following code processes the wm_nchittest message to drag the dialog box:

[C-sharp]View
Plaincopy

  1. Lresult onnchittest (uint/* umsg */, wparam/* wparam */, lparam, bool &/* bhandled */)
  2. {
  3. Crect RC;
  4. Point pt;
  5. PT. x = get_x_lparam (lparam );
  6. PT. Y = get_y_lparam (lparam );
  7. Getclientrect (& rc );
  8. Clienttoscreen (& rc );
  9. Return RC. ptinrect (PT )? Htcaption: 0;
  10. }

 

 

The above code is easy to understand. When the mouse falls into the customer zone, the function returns htcaption. For a simple dialog box, you can use this code to drag the dialog box. Because Windows uses the Z-order coordinates to determine the window under the mouse, all other objects in the dialog box work as usual. If you click a control, as long as the control is not a static bitmap image or text, Windows will send the mouse event to the control, rather than the dialog box. Because the static bitmap image or text is transparent to the dialog box, you can move the cursor over it, other non-static controls such as the edit box, button, and combo box in the dialog box are executed as usual.

 

From:Drag a dialog box without a title bar in VC ++

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.