现在的位置: 首页 > 自动控制 > 工业·编程 > 正文

在对话框上拖动按钮并移动该按钮

2012-09-08 21:38 工业·编程 ⁄ 共 1054字 ⁄ 字号 暂无评论

// 派生自CButton类,主要过滤WM_LBUTTONDOWN 、WM_LBUTTONUP和WM_MOUSEMOVE消息。

BOOL m_bFlag = FALSE; // 成员变量,用来标示鼠标是否按下, 初始化为FALSE
CPoint m_pt; // 成员变量,用来保存当前坐标值

BOOL CXXButton::PreTranslateMessage(MSG* pMsg)
{
    // TODO: Add your specialized code here and/or call the base class
    switch(pMsg->message)
    {
    case WM_LBUTTONDOWN:
        {
            m_pt = pMsg->pt;
            m_bFlag = TRUE;
        }
        break;

    case WM_LBUTTONUP:
        {
            m_bFlag = FALSE;
        }
        break;

    case WM_MOUSEMOVE:
        {
            if(m_bFlag)
            {
                int cx = pMsg->pt.x - m_pt.x;
                int cy = pMsg->pt.y - m_pt.y;

                CRect rc;
                GetWindowRect(&rc);
                GetParent()->ScreenToClient(&rc);
                int nWidth = rc.Width();
                int nHeight = rc.Height();

                rc.left += cx;
                rc.top += cy;
                rc.right = rc.left + nWidth;
                rc.bottom = rc.top + nHeight;

                MoveWindow(rc);

                m_pt = pMsg->pt;
            }
        }
        break;

    default:
        break;
    }
    return CButton::PreTranslateMessage(pMsg);
}

给我留言

留言无头像?