开发者

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.

R开发者_开发技巧ight 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.

Here is the problematic code :

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;
}

Could the problem be that I have Windows7?!


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.""

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.


You could try to use plain API calls:

HMODULE hDll = LoadLibrary("magicfuncdll.dll");
HOOKPROC hookLowLevelMouseFilter = (HOOKPROC)GetProcAddress(hDll, "_MagicFuncDLL_LowLevelMouseFilterProc@12");

// capture mouse events
HHOOK hMouseHook = SetWindowsHookEx(WH_MOUSE_LL, hookLowLevelMouseFilter, hDll, 0);

... do your thing

// release mouse
UnhookWindowsHookEx(hMouseHook);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜