Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I coded a program in win32 api with a dialog resource which dose not contain a title bar.Then i must write the draging dialog code by myself.
i placed a SetWindowPos function in the WM_MOUSEMOVE in the condtion of MK_LBUTTON==wParam like this:
C++
case WM_MOUSEMOVE:			
			if( MK_LBUTTON == wParam ){
				::SetWindowPos(hwnd,HWND_TOP,xw+GET_X_LPARAM(lParam)-x,yw+GET_Y_LPARAM(lParam)-y,w,h,SWP_SHOWWINDOW);
			}
			break;
case WM_LBUTTONDOWN:
			x = GET_X_LPARAM(lParam);
			y = GET_Y_LPARAM(lParam);
			::GetWindowRect(hwnd,&rect);
			xw = rect.left;
			yw = rect.top;
			break;

But the dialog will not move smoothly ,the SetWindowPos is processed in each WM_MOUSEMOVE message.
What's more,the Dialog window does not go with the cursor.the window speed is lower than the cursor.
I think that the SetWindowPos is processed in each WM_MOUSEMOVE message,so it slow down the window speed.

Then how to resolve it?
Posted
Updated 16-Oct-12 20:13pm
v4

Normally you would handle WM_NCHITTEST[^] to tell Windows that the click occured in the caption.

That way Windows will automatically handle the move as if you would have clicked on the caption.
 
Share this answer
 
Comments
Keanu L 17-Oct-12 1:56am    
Thanks for your solution.I have tried ,but My Dialog does not contain a title bar, the WM_NCHITTEST did not work.
Philippe Mori 17-Oct-12 11:23am    
When you receive that message, you have to modify the hit test result to pretend the clic occur in the title bar so that Windows will handle the move.

Also, if you have child control, I believe they will first receive the message and that the parent will only receive it if the child returns HT_TRANSPARENT.

If you need more information, then you can uses Google. I haven't done programming at the API level since many year but it is well known that WM_HITTEST can be used to make Windows think that the mouse is on the caption or any other area like a resizable border.

http://blogs.msdn.com/b/oldnewthing/archive/2011/02/18/10131176.aspx

http://social.msdn.microsoft.com/Forums/en-US/vclanguage/thread/435c18ec-92ca-40f3-bf72-0829ebedd5f9

http://stackoverflow.com/questions/8969852/how-to-move-parent-window-without-border-from-child-using-wm-nchittest
Keanu L 17-Oct-12 22:41pm    
Thanks for your advice,i will work further on it and get back.
For Win32,
C++
static void HandleClientAreaDrag (HWND hwnd, UINT msg, int mouseX, int mouseY)
{
    static int captureX = 0;
    static int captureY = 0;

    switch (msg)
    {
    case WM_LBUTTONDOWN:
      captureX = mouseX;
      captureY = mouseY;
      SetCapture (hwnd);
      break;

    case WM_LBUTTONUP:
      ReleaseCapture();
      break;

    case WM_MOUSEMOVE:
      if (GetCapture() == hwnd)
      {
        RECT rc;
        GetWindowRect (hwnd, &rc);
        int  newX   = rc.left + mouseX - captureX;
        int  newY   = rc.top  + mouseY - captureY;
        int  width  = rc.right - rc.left;
        int  height = rc.bottom - rc.top;
        UINT flags  = SWP_NOZORDER | SWP_NOACTIVATE;
        SetWindowPos (hwnd, NULL, newX, newY, width, height, flags);
      }
      break;
    }
}



case WM_MOUSEMOVE:
{
    int xPos = GET_X_LPARAM(lParam); 
    int yPos = GET_Y_LPARAM(lParam); 
    HandleClientAreaDrag(m_hWnd, WM_MOUSEMOVE, xPos, yPos );
    break;
}

case WM_LBUTTONUP:
{
    int xPos = GET_X_LPARAM(lParam); 
    int yPos = GET_Y_LPARAM(lParam); 
    HandleClientAreaDrag(m_hWnd, WM_LBUTTONUP, xPos, yPos);
    break;
}

case WM_LBUTTONDOWN:
{
    int xPos = GET_X_LPARAM(lParam); 
    int yPos = GET_Y_LPARAM(lParam); 
    HandleClientAreaDrag(m_hWnd, WM_LBUTTONDOWN, xPos, yPos );
    break;
}

This might be helpful for you.
http://www.programmersheaven.com/mb/CandCPP/60606/60606/move-a-window-in-mfc/
 
Share this answer
 
v3
Comments
Keanu L 17-Oct-12 2:08am    
Thanks.I learned the SetCapture function from your submit.But i have tried , when i pressed down the lbutton ,and moved it,the dialog window vacillate to the left and to the right.It does not move smootly.
Santhosh G_ 17-Oct-12 3:30am    
For smooth move effect, you can use a timer to move between two points.
The movement in each timer interval should be small to prepare a smooth effect.
I made a misktake here.I used the lParam paramert to record the position of the cursor.
The lParam is the following meaning:
from msdn:http://msdn.microsoft.com/zh-cn/library/windows/desktop/ms645616(v=vs.85).aspx[^]
SQL
lParam
The low-order word specifies the x-coordinate of the cursor. The coordinate is relative to the upper-left corner of the client area.

The high-order word specifies the y-coordinate of the cursor. The coordinate is relative to the upper-left corner of the client area.


But when to drag the window through the cursor position , the relative position to the screen is only true.
so , i used GetCursorPos to record and drag the window. it works well now~.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900