VC实现自定义按钮响应拖动

代码
void CMoveableButton::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_bStartMove = TRUE;
CButton::OnLButtonDown(nFlags, point);
}

void CMoveableButton::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_bStartMove = FALSE;
CButton::OnLButtonUp(nFlags, point);
}

void CMoveableButton::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if (m_bStartMove)
{
ReleaseCapture();
SendMessage(WM_NCLBUTTONDOWN, (WPARAM)HTCAPTION,
0);
}
CButton::OnMouseMove(nFlags, point);
}

==========================

OnMouseMove的另一种写法:

void CMoveableButton::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if (m_bStartMove)
{
CRect rect;

ClientToScreen(
&point);
this->GetParent()->ScreenToClient(&point);

GetClientRect(
&rect);
point.Offset(
-rect.Width()/2, -rect.Height()/2);
//        这里可能需要控制按钮的移动范围,使其不移动到界面外,方法:CWnd::SetWindowPlacement
//   也可以自己实现

SetWindowPos(NULL, point.x , point.y , 0, 0, SWP_NOZORDER|SWP_NOSIZE);
// afxDump<<point.x<<","<<point.y<<"\n";
Invalidate();
}
CButton::OnMouseMove(nFlags, point);
}
原文地址:https://www.cnblogs.com/aoyihuashao/p/1623710.html