按下鼠標時,記錄下鼠標在窗體上的坐標, 同時用一個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;
}