在日常工作中经常遇到在一个消息中捕获其它消息,比如当用户按下鼠标左键时,还想捕获鼠标移动的消息。这个该怎么处理呢?其实只需要在OnLButtonDown()中使用::GetMessage(&msg, NULL, 0, 0);来不停的截取系统消息,并根据msg的消息码来处理你想截获的消息即可。在下面这段代码中,当用户按下鼠标左键时在OnLButonDown()中将截获所有的系统消息,知道收到LButtonUp消息。
void CTestCaptureDlg::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default MSG msg; SetCapture(); while (true) { ::GetMessage(&msg, NULL, 0, 0); if (msg.message == WM_MOUSEMOVE) { TRACE("MouseMove消息已截获! /n"); } if (msg.message == WM_LBUTTONUP) { TRACE("LButtonUp消息已截获! /n"); break; } } ReleaseCapture(); CDialog::OnLButtonDown(nFlags, point); } void CTestCaptureDlg::OnMouseMove(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default TRACE("消息未截获! /n"); CDialog::OnMouseMove(nFlags, point); }
捕获鼠标消息
本文介绍了一种在用户按下鼠标左键时捕获鼠标移动及释放消息的方法。通过在OnLButtonDown()函数中使用GetMessage()函数,可以持续监听并处理WM_MOUSEMOVE和WM_LBUTTONUP等消息。
1726

被折叠的 条评论
为什么被折叠?



