在应用程序中独占/释放鼠标(Windows,C ++) [英] Grab exclusively/release mouse in application (Windows, C++)
问题描述
我已经花了很多时间试图专门抓住我的应用程序中的鼠标并重新发布它.
I have lost many hours trying to grab exclusively the mouse in my application and re-releasing it.
现在,我正确地抓住它:鼠标光标从屏幕上消失了,我可以很好地读取其属性.
Right now, I am grabbing it correctly: the mouse cursor disappears from screen and I can read its properties fine.
但是,我无法正确释放它!鼠标光标再次出现在屏幕上,但是没有其他应用程序再收到任何鼠标点击;除了我的.
However, I can't release it correctly! The mouse cursor reappears on screen but no other application is receiving any mouse clicks any more; except mine.
这是有问题的代码:
IDirectInputDevice8* mMouse;
void Win32Mouse::grab(bool grab)
{
if (mGrabMouse == grab)
return;
mMouse->Unacquire();
if (grab)
{
// grab = true; seems to work fine
coopSetting &= ~DISCL_BACKGROUND;
coopSetting &= ~DISCL_NONEXCLUSIVE;
coopSetting |= DISCL_FOREGROUND | DISCL_EXCLUSIVE;
}
else
{
// grab = false; this surely isn't working as it should
coopSetting &= ~DISCL_FOREGROUND;
coopSetting &= ~DISCL_EXCLUSIVE;
coopSetting |= DISCL_BACKGROUND | DISCL_NONEXCLUSIVE;
}
if( FAILED(mMouse->SetCooperativeLevel(mHwnd, coopSetting)) ) {
std::cout << "Failed to set coop level\n";
}
HRESULT hr = mMouse->Acquire();
if (FAILED(hr) && hr != DIERR_OTHERAPPHASPRIO) {
std::cout << "Failed to aquire mouse!\n";
}
mGrabMouse = grab;
}
可能是我有Windows7吗?!
Could the problem be that I have Windows7?!
推荐答案
可能是因为这个原因
http://doc.51windows. net/Directx9_SDK/?url =/directx9_sdk/input/ref/ifaces/idirectinputdevice9/setcooperativelevel.htm
"以独占方式获取鼠标或键盘设备的应用程序在收到WM_ENTERSIZEMOVE和WM_ENTERMENULOOP消息时,应始终取消获取设备.
否则,用户将无法操作菜单或移动窗口并调整窗口大小."
Possibly it because of this
http://doc.51windows.net/Directx9_SDK/?url=/directx9_sdk/input/ref/ifaces/idirectinputdevice9/setcooperativelevel.htm
""An application that acquires the mouse or keyboard device in exclusive mode should always unacquire the devices when it receives WM_ENTERSIZEMOVE and WM_ENTERMENULOOP messages.
Otherwise, the user cannot manipulate the menu or move and resize the window.""
可能不是它的完全答案
但也请尝试检查是否可能是您未在其他一些消息上获得鼠标.
例如在WM_LOSTFOCUS等上
May be its not totally answer
But try also to check if may be you not unacquire mouse on some other messeges.
For example on WM_LOSTFOCUS, etc.
这篇关于在应用程序中独占/释放鼠标(Windows,C ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!