开发者

WinForms Mouse Capture

I have Windows Forms application and I need to capture the mouse movements outside my window. My simplified code in my window class is:

private void ButtonOnClick(object sender, EventArgs e)
{
    Capture = true;
    MouseMove += OnMouseMove;
}

private void OnMouseMove(object sender, MouseEventArgs e)
{
    Console.Out.Write("!");
}

As you see, when user presses a button, my program should start tracking mouse (even if it is outside of window - this is a key featue!) But I get really strange behavior. If I move mouse inside the window, everything is great, ! are written to console. But when I move mouse outside of the window, only OnMouseMove is c开发者_C百科alled only once (and the point is really outside). Then if I move mouse anywhere outside of the window it is not called anymore. If I return mouse back to window, everything is perfect. Move away - 1 message, move in the window - OK.

Can anybody help?


You would need a global mouse hook for that. I recommend that you first read something about hooks, eg. on MSDN. An example implementation in C# can be found at the CodeProject.

Hope that helps a bit.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜