WIN32 no title bar of the window to move all sorts of methods

WIN32 no title bar of the window to move all sorts of methods

Reprinted Source: http: //blog.csdn.net/fuyun_cloud/article/details/8008197

   

First, take a look at how the system is to move the program window under normal circumstances. When the user in the window title bar area (non-working area), press the left mouse button will happen the following things:

 ◆ transmission system to the window procedure WM_NCLBUTTONDOWN message.

 ◆  the WM_NCLBUTTONDOWN message will eventually be transmitted to the window procedure in the DefWindowProc () function to.

 ◆  the DefWindowProc () function in accordance with the left mouse button is pressed and moved, and HTCAPTION position information of the mouse is pressed many, i.e., the window to the default actions the message along with the operation of moving the mouse cursor to identify FIG.

Here as an exercise to test, first set the following statement in the window callback function (ie, the window procedure function) in which: 

case WM_NCLBUTTONDOWN: 

    return 0; 

Then, also in the window title bar hold down the left mouse button and moving the mouse, but this time they are not accompanied by a window with a mouse movement, how is this going?

      This is because the above statement with a "return 0" statement of reasons. The statement that WM_NCLBUTTONDOWN message fails to pass the DefWindowProc () function, the function will return the window procedure in advance, of course, impossible to move the window operations performed. This also confirms the fact that from the negative side, that is, to finalize the moving window operation by DefWindowProc () function to complete.

    Such a procedure may be outlined by the above analysis of operation:

I.e., transmits the user presses the left mouse button in the window title bar system → WM_NCLBUTTONDOWN message → the DefWindowProc () function receives the user moves the mouse message → → the DefWindowProc () function execution window moves along with the operation of the mouse.

It follows a conclusion that in order to achieve the operation to move the window, must have two conditions:

    One is to press the button and move the mouse ( the DefWindowProc () function will detect this condition); the second is able to send the left mouse button is pressed WM_NCLBUTTONDOWN message and returns HTCAPTION identification.

    Below Based on the above analysis, in the absence of a window title bar of the situation and take defraud DefWindowProc () function works to achieve mobile operation without the title bar of the window entity.

 

 

1, sends sends sends message sends WM_NCLBUTTONDOWN

 

  In the title bar of the window is not the case, when you press the left mouse button on the window entity, the system will not send WM_NCLBUTTONDOWN message, it is because the mouse cursor is pressed in the work area of the window, then the system sends It is WM_LBUTTONDOWN message. But by the above analysis, we can know DefWindowProc () function does not care WM_NCLBUTTONDOWN messages sent by whom, but only if there is concern that the message is sent. So long as we press the left mouse button event, the initiative will WM_NCLBUTTONDOWN message is sent, would not satisfy these two conditions can do! The following code is based on this idea to design.

  case WM_LBUTTONDOWN:

     SendMessage(hWnd,WM_NCLBUTTONDOWN,HTCAPTION,0);   

      break; 

    When the message is sent, via HTCAPTION parameters to DefWindowProc () function to bring a message telling it is to press the left mouse button in the title bar at the non-working area of the window. Of course, this is a disinformation, but DefWindowProc () function at face value and to perform the appropriate action based on this information.

 

2, active transmission sends an unsolicited message sends WM_SYSCOMMAND

 

case WM_LBUTTONDOWN:   

 SendMessage(hWnd,WM_SYSCOMMAND,SC_DRAGMOVE,0);   

  break; 

    It can be used WM_SYSCOMMAND message to move a window, thanks to a newly extended SC_DRAGMOVE style flag literally be able to see the "drag movement" means. The flag in the low version of the compiler (VC 6.0 would be no) is not found, it is declared in advance when they refer to the sign:

  #define  SC_DRAGMOVE  0xF012 

 It also can be used as a constant value:

  SendMessage(hWnd,WM_SYSCOMMAND,0xF012,0); 

    When the above-described mechanism without moving the title bar of the window form with the window title bar is similar, at the same end are made the DefWindowProc () function to accomplish the actual operation; difference is that different ways of sending messages, implicitly by the system is a containing transmission; the other is transmitted by the program disclosed.

    Taking the above two ways to move a window, move the window title bar with the visual effect is the same, that is, when moving, moving a first indication box (a dashed box), and so the new location after moving good form , release the mouse button when the window was only entity is really moved to a position pointed to by a dashed box. So, can directly move the window dotted box entity without it? The answer is yes.

    In fact, the operating system is ready to move two windows, one is the dotted rectangle, the other is not dashed box, only Windows system default is dotted rectangle. However, if we add the following statement in the above example code: 

SystemParametersInfo(SPI_SETDRAGFULLWINDOWS,true,NULL,0); 

 which is:

  case WM_LBUTTONDOWN:      

     SystemParametersInfo(SPI_SETDRAGFULLWINDOWS,true,NULL,0);  

     SendMessage(hWnd,WM_NCLBUTTONDOWN,HTCAPTION,0);    break;

    So that when the window moves, the dashed lines indicate the frame does not appear. Note that the above statement of the position of the order can not be wrong, otherwise, the dashed box will appear when you move.

However, the system default manner indicated with a dashed box to weigh a moving window through. This is because there are ways to move the dashed box window, not being really move at the beginning of the window, but with a frame to specify the location of the window to be reached, then a one-time move the window to the specified location (in the specified location redrawn) at. In other words, in the process of moving the window, the window needs to be redrawn entity only once. If you do not dotted box, but move directly entity window, then in the process of moving the window, the window will be drawn to form N times, increasing the burden on system processing graphics, the rendering of a serious decline in the quality of the window, creating a bad visual effects .

    For this reason, in practice, the programming may be so arranged that: in order to reflect the shaped profiled window visual effect, can be used without a dashed box way to move the window; for a general rectangular window can by way of the dotted rectangle window moves to seeking to ensure the quality redraw the window. Indeed, the code provided above can not dotted box shaped window moves, but the SystemParametersInfo () function is a system-level, is invoked on the desktop will affect all program windows are moved in no way a dashed box, if so if. Desktop will make the overall visual effect is greatly reduced. If you do not want to affect the movement of the effect of other windows, but only requires the dashed box does not appear when moving the profiled window, then the window procedure function can then add the following code: 

  case WM_MOUSEMOVE:   

   SystemParametersInfo(SPI_SETDRAGFULLWINDOWS,false ,NULL,0);    break;

 

 

3, self c / c ++ Code

 

    Self-compiled code can achieve the same operation without moving the title bar of the window, and more flexible. For example, the operation may be performed by the left mouse button or the right mouse button. The following is the code to do the actual movement of the window by right, add the following statement in a procedure of the program window function:

 

static POINT pt, pe;   

  

static RECT rt, re;    

  

case WM_RBUTTONDOWN:   

  

    SetCapture (hWnd); // set the mouse capture (to prevent loss of the mouse cursor hot spot ran window)       

  

    GetCursorPos (& pt); // Get the current position of the mouse cursor pointer      

  

     GetWindowRect (hWnd, & rt); // get the window position and size      

  

     re.right = rt.right-rt.left; // save window width       

  

     re.bottom = rt.bottom-rt.top; // save the window height     

  

     break;    

  

case WM_RBUTTONUP:       

  

    ReleaseCapture (); // release the mouse capture, return to normal state      

  

     break;  case WM_MOUSEMOVE:     

  

    GetCursorPos (& pe); // Get the new position of the pointer     

  

     if (wParam == MK_RBUTTON) // when the right mouse button is pressed    

  

    {         

  

        re.left = rt.left + (pe.x - pt.x); // horizontal position of the new window    

  

          re.top = rt.top + (pe.y - pt.y); // new window vertical position  

  

          MoveWindow (hWnd, re.left, re.top, re.right, re.bottom, true); // moving window     

  

  }     break;  

 

Original Address: http: //blog.csdn.net/fuyun_cloud/article/details/8008197

Guess you like

Origin blog.csdn.net/ZDT_zdh/article/details/83650319