-
按下鼠标时,记录下鼠标在窗体上的坐标, 同时用一个BOOL变量记录下鼠标左键按下的状态
弹起左键,则记录状态的布尔变量为FALSE.
处理鼠标移动事件,如果左键同时按下,则为鼠标拖动窗体运动,设置窗体新的左上角坐标。
view plaincopy to clipboardprint?
·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······15001.void CNoBorderFormDlg::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
CDialog::OnMouseMove(nFlags, point);
if (this->_leftButtonDown)
{
/// 鼠标左键按下时,可以拖动窗体
CPoint pointChanged = point - (this->_mousePoint);
RECT rect;
this->GetWindowRect(&rect);
this->SetWindowPos(NULL, rect.left pointChanged.x, rect.top pointChanged.y, 0, 0, SWP_NOSIZE);
}
}void CNoBorderFormDlg::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值CDialog::OnLButtonDown(nFlags, point);
this->_leftButtonDown = TRUE;
this->_mousePoint = CPoint(point);
}
void CNoBorderFormDlg::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
CDialog::OnLButtonUp(nFlags, point);
this->_leftButtonDown = FALSE;
}
void CNoBorderFormDlg::OnMouseLeave()
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
CDialog::OnMouseLeave();
this->_leftButtonDown = FALSE;
}
C++ 无边框的拖动窗口代码
![](https://img2024.cnblogs.com/blog/35695/202408/35695-20240826070647288-243310440.jpg)