C# 中的 Win api。 MouseMove 事件无法捕获快速移动

发布于 2024-12-11 19:25:50 字数 236 浏览 0 评论 0原文

我用 C# 中的 WinApi 函数创建了一个无边框的小窗口。我想在按下鼠标右键时移动此窗口。我试图通过任意化 WM_MOUSEMOVE 事件来捕获鼠标偏移。它似乎有效,我可以按住鼠标右键移动窗口。

但是当我移动鼠标太快时,我就会失去对窗口的控制。这是因为我的窗口太小,如果鼠标很快离开窗口,它就不会再收到 WM_MOUSEMOVE 消息,并且我无法计算 MouseOffset 来移动我的窗口。

那么,我该如何解决这个问题?

I have created a small Window without border by WinApi functions in C#. I want to move this window when right mouse button is pressed. I am trying to catch a mouse offset by anylizing WM_MOUSEMOVE event. It seems to work and i can move my window holding Right Mouse Button.

But I am loosing control of the window when i move my mouse too fast. That's because my Window is too small and if mouse leaves window very quick it doesn'n recieve WM_MOUSEMOVE messages anymore and i can't calculate a MouseOffset to move my Window.

So, How I can fix that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜中书 2024-12-18 19:25:50

您需要调用 SetCapture 来告诉 Windows 您的 hwnd 需要所有事件,即使鼠标实际上并未位于窗口上方。 http://msdn.microsoft。 com/en-us/library/windows/desktop/ms646262(v=vs.85).aspx

You need to call SetCapture to tell Windows that your hwnd wants all the events even when the mouse isn't physically over the window. http://msdn.microsoft.com/en-us/library/windows/desktop/ms646262(v=vs.85).aspx

南冥有猫 2024-12-18 19:25:50

您可以告诉 Windows 用户实际上将鼠标放在标题栏上,它会自动为您处理其余的事情。

[DllImport("user32.dll", CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool ReleaseCapture();

[DllImport("user32.dll", CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool PostMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);

internal const uint WM_NCLBUTTONDOWN = 0xA1;

internal const int HTCAPTION = 2; // Window captions
internal const int HTBOTTOMRIGHT = 17; // Bottom right corner

/// <summary>
/// Simulates a Windows drag on the window border or title.
/// </summary>
/// <param name="handle">The window handle to drag.</param>
/// <param name="dragType">A HT* constant to determine which part to drag.</param>
internal static void DragWindow(IntPtr handle, int dragType) {
    User32.ReleaseCapture();
    User32.PostMessage(handle, User32.WM_NCLBUTTONDOWN, new IntPtr(dragType), IntPtr.Zero);
}

You can tell Windows that the user actually moused down on the title bar and it will handle the rest automatically for you.

[DllImport("user32.dll", CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool ReleaseCapture();

[DllImport("user32.dll", CharSet = CharSet.Auto)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool PostMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);

internal const uint WM_NCLBUTTONDOWN = 0xA1;

internal const int HTCAPTION = 2; // Window captions
internal const int HTBOTTOMRIGHT = 17; // Bottom right corner

/// <summary>
/// Simulates a Windows drag on the window border or title.
/// </summary>
/// <param name="handle">The window handle to drag.</param>
/// <param name="dragType">A HT* constant to determine which part to drag.</param>
internal static void DragWindow(IntPtr handle, int dragType) {
    User32.ReleaseCapture();
    User32.PostMessage(handle, User32.WM_NCLBUTTONDOWN, new IntPtr(dragType), IntPtr.Zero);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文