法一:
以拖动按钮为例:
从CButton派生一个CMyButton类,在窗体中增加一个按钮控件,用类向导声明控件变量为CMybutton m_ctrlButton,在CMyButton中增加一个消息句柄函数MOUSEMOVE的消息,然后在OnMouseMove函数中增加如下代码
void CMyButton::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if (nFlags==1)
{
ReleaseCapture();
SendMessage(WM_NCLBUTTONDOWN, HTCAPTION, 0);
}
CButton::OnMouseMove(nFlags, point);
}
就可以实现按钮控件的移动了
法二:
既能拖动,又能动态调整控件大小的方法:
LRESULT CMyButton::OnNcHitTest(CPoint point) { // TODO: Add your message handler code here and/or call default CRect rect; GetClientRect(&rect); ScreenToClient(&point); int nFrame = 3; rect.DeflateRect(nFrame, nFrame); //让矩形向内缩小nFrame个单位 if (rect.NormalizeRect(), !rect.PtInRect(point)) //不在矩形客户区 { if (point.x <= nFrame &&point.y <= nFrame) //左上角 return HTTOPLEFT; else if (point.x <= nFrame && point.y >= rect.bottom - nFrame) //左下角 return HTBOTTOMLEFT; else if (point.x >= rect.right - nFrame && point.y <= nFrame) //右上角 return HTTOPRIGHT; else if (point.x >= rect.right - nFrame && point.y >= rect.bottom - nFrame) //右下角 return HTBOTTOMRIGHT; else if (point.x <= nFrame) //向左拖动 return HTLEFT; else if (point.x >= rect.right - nFrame) //向右拖动 return HTRIGHT; else if (point.y <= nFrame) //向上拖动 return HTTOP; else if (point.y >= rect.bottom - nFrame) //向下拖动 return HTBOTTOM; } else return HTCAPTION; return CButton::OnNcHitTest(point); }