鼠標按下、移動、釋放時,預期輸出 "down button"、"move button"、"delete button",但實際**缺少 "delete button"**。
`WM_LBUTTONUP` 消息未觸發,因為**鼠標捕獲**機制:
void CMyView::OnLButtonDown(UINT nFlags, CPoint point)
{
SetCapture(); // 1. 捕獲鼠標
// 記錄起始點、設置標志...
CView::OnLButtonDown(nFlags, point);
}
void CMyView::OnMouseMove(UINT nFlags, CPoint point)
{
if (GetCapture() == this) { // 只有捕獲期間才處理拖拽
// 繪製橡皮筋、更新狀態...
}
CView::OnMouseMove(nFlags, point);
}
void CMyView::OnLButtonUp(UINT nFlags, CPoint point)
{
if (GetCapture() == this) {
ReleaseCapture(); // 2. **必須釋放捕獲**
// 完成拖拽邏輯、輸出 "delete button"...
}
CView::OnLButtonUp(nFlags, point);
}
| 操作 | 作用 |
|---|---|
| `SetCapture()` | 讓窗口接收鼠標離開客戶區後的消息,實現拖拽超出邊界仍跟隨。 |
| `ReleaseCapture()` | 結束捕獲,恢復正常命中測試分發。缺失會導致 UP 消息丟失、後續點擊失效。 |
| `GetCapture() == this` | 防止多窗口/巢狀捕獲混亂,僅在自己捕獲時處理。 |
**MFC 鼠標捕獲:有借有還,`SetCapture` 必配 `ReleaseCapture`,否則消息鏈斷裂。**