The simplest way to implement window dragging in WTL

Source: Internet
Author: User

Currently, dialog boxes are not framed in many dialog-based applications, which means that the dialog box does not have a title bar. As we all know, the window is moved by the mouse to drag the title bar of the window, then now the application of the dialog box does not have a title bar, the user How to move the dialog box? This example proposes a solution to this problem.

First, the realization method

There are two scenarios for solving a drag problem with no title bar window, one of which is to use conventional ideas to handle mouse drag events, when the window gets Wm_lbuttondown (OnLButtonDown), by setting a flag and calling CWnd: SetCapture () function to let the current window capture mouse messages, the application into the mobile mode, as long as there is a wm_mousemove message come over, you can move the frame window, and finally when the user released the mouse button, then wm_ The LBUTTONUP message processing routine clears the flag and calls the Cwnd::releasecapture () function to return the mouse control to Windows. This method is more cumbersome, first of all to decide where to move the window ready? Then you have to figure out how to redraw the window and so on, and the drag effect is different, depending on whether the show window contents on drag check box is selected on the effects page of the Properties dialog box on the screen display. So how do you know what information is set? The method is to call SystemParametersInfo (Spi_getdragfullwindows). It's too bad that Windows wants programmers to deal with these tedious things in detail.

Since Windows itself knows to move the window by clicking on the title bar with the mouse, can you imitate the mouse click-and-drag behavior anywhere in the client area of the window as if it were in the title bar? The answer is yes, and this creates the second way to move the window. In fact, it is not difficult to use the mouse to click the background of the dialog box, but you must understand the principle of dragging the window in the title bar. Windows first determines which window in the mouse point, and then sends a WM_NCHITTEST message to that window to find out which "non-client area" of the window (such as boundaries, maximize/minimize buttons, menus, headings, and so on) has the mouse cursor. The default window procedure then responds to the message and returns a specific code. If the mouse pointer falls in the title bar, then this particular code is htcaption, when Windows enters drag mode so that the user can move the window. So in the customer area with the mouse to drag the dialog box, so long as in the customer area to imitate the title bar mouse drag behavior can be. The following code implements the drag operation of the dialog box by processing the WM_NCHITTEST message:

[C-sharp]View Plaincopy
  1. LRESULT onnchittest (UINT /*umsg*/, WPARAM /*wparam*/, LPARAM 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 within the client area, the function returns htcaption. For a simple dialog box, you can simply use this code to implement the drag operation in the background of the dialog box. Because Windows uses Z-order coordinates to determine which window is under the mouse, all other objects in the dialog box work as usual. If the user clicks on a control, as long as the control is not a static bitmap image or text, Windows sends the mouse event to the control instead of the dialog box. Because the static bitmap image or text is transparent to the dialog box, the mouse is moved in the same way as the drag on the top, while other non-static controls, such as edit boxes, buttons, combo boxes, and so on in the dialog box, run as normal behavior.

From: in VC + + to implement the No Title bar dialog box Drag

The simplest way to implement window dragging in WTL (GO)

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.