如何顺利拖动对话框? [英] How to drag the dialog smoothly?

查看:128
本文介绍了如何顺利拖动对话框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用win32 api编写了一个带有对话框资源的程序,该程序不包含标题栏,然后我必须自己编写拖动对话框代码.
我将MK_LBUTTON == wParam的条件下的SetWindowPos函数放在WM_MOUSEMOVE中,如下所示:

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:

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;


但是对话框将无法顺利移动,在每个WM_MOUSEMOVE消息中都会处理SetWindowPos.
此外,对话"窗口不随光标一起移动.窗口速度低于光标.
我认为SetWindowPos是在每个WM_MOUSEMOVE消息中处理的,因此会减慢窗口速度.

那怎么解决呢?


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?

推荐答案

通常,您会处理WM_NCHITTEST [
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.


对于Win32,
For Win32,
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;
}


这可能对您有帮助.
http://www.programmersheaven.com/mb/CandCPP/60606/60606/move-a-window-in-mfc/


This might be helpful for you.
http://www.programmersheaven.com/mb/CandCPP/60606/60606/move-a-window-in-mfc/


我在这里犯了一个错误.我使用了lParam paramert记录光标的位置.
lParam的含义如下:
来自msdn:
http://msdn.microsoft.com/zh-cn/library/windows/desktop/ms645616(v = vs.85).aspx [
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[^]
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.



但是,当通过光标位置拖动窗口时,与屏幕的相对位置仅为true.
因此,我使用GetCursorPos记录和拖动窗口.现在效果很好〜.



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~.


这篇关于如何顺利拖动对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆