基礎知識:鼠標在窗口內移動,點擊或者釋放時都會產生WM_NCHITTEST消息,響應函數OnNcHitTest會返回一個枚舉值,系統會根據這個枚舉值進行相應的處理。當返回值爲HTCAPTION時,系統會認爲此時鼠標位於標題欄上,於是當鼠標按下並移動時就會執行拖動操做。
LRESULT OnNcHitTest(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { POINT pt; RECT rcClient; RECT rcCaption; rcCaption = m_pm.GetCaptionRect(); GetClientRect(m_pm.GetPaintWindow(), &rcClient); pt.x = GET_X_LPARAM(lParam); pt.y = GET_Y_LPARAM(lParam); ::ScreenToClient(m_pm.GetPaintWindow(), &pt); //xml中設置bottom爲-1時,整個窗口區域均可以拖動 if (-1 == rcCaption.bottom) { rcCaption.bottom = rcClient.bottom; } if ((pt.x >= rcClient.left) && (pt.x < rcClient.right) && (pt.y >= rcCaption.top) && (pt.y < rcCaption.bottom)) { return HTCAPTION; } return __super::OnNcHitTest(uMsg, wParam, lParam, bHandled); }
基礎知識:咱們能夠模擬在win32中窗口移動的函數處理過程。簡單的說,咱們只須要在本身的窗口中對WM_LBUTTONDOWN、WM_MOUSEMOVE、WM_LBUTTONUP這三個消息進行處理便可。在WM_LBUTTONDOWN中記錄鼠標左鍵被按下時的信息,WM_MOUSEMOVE中記錄鼠標移動距離,WM_LBUTTONUP記錄鼠標左鍵彈起。
LRESULT OnLButtonDown(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { //記錄鼠標按下 is_lbutton_down_ = true; //鼠標按下時的座標 start_point_.x = GET_X_LPARAM(lParam); start_point_.y = GET_Y_LPARAM(lParam); bHandled = TRUE; return 0; } LRESULT OnLButtonUp(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { //左鍵彈起,改變鼠標狀態 is_lbutton_down_ = false; bHandled = TRUE; return 0; } LRESULT OnMouseMove(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) { if (is_lbutton_down_ == true) { POINT point; //獲取當前鼠標的位置 ::GetCursorPos(&point); ::ScreenToClient(m_pm.GetPaintWindow(), &point); //獲取新的位置 int Dx = point.x - start_point_.x; int Dy = point.y - start_point_.y; start_rect_.left += Dx; start_rect_.right += Dx; start_rect_.top += Dy; start_rect_.bottom += Dy; //將窗口移到新的位置 SetWindowPos(m_hWnd, HWND_TOP, start_rect_.left, start_rect_.top, 0, 0, SWP_NOSIZE); } bHandled = TRUE; return 0; }
MSDN:The _TrackMouseEvent function posts messages when the mouse pointer leaves a window or hovers over a window for a specified amount of time. This function calls TrackMouseEvent if it exists, otherwise it emulates it.
具體方法以下:函數
BOOL is_mouse_track_=TRUE ;
if (is_mouse_track_) { TRACKMOUSEEVENT csTME; csTME.cbSize = sizeof (csTME); csTME.dwFlags = TME_LEAVE|TME_HOVER; csTME.hwndTrack = m_hWnd ; csTME.dwHoverTime = 10; // 鼠標在按鈕上停留超過10ms ,才認爲狀態 HOVER ::_TrackMouseEvent (&csTME); is_mouse_track_=FALSE ; }
is_mouse_track_=TRUE ;