如何在 C# 中处理 WM_SETCURSOR [英] How to handle WM_SETCURSOR in C#

查看:55
本文介绍了如何在 C# 中处理 WM_SETCURSOR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的媒体播放器应用程序中,我使用 SetCursor(NULL) 隐藏了光标,并确保 Windows 不会重置光标状态,我在我的程序中处理了 WM_SETCURSORWndProc 方法.

In my media player application, I hid the cursor using SetCursor(NULL) and to make sure that Windows does not reset the cursor state, I handled WM_SETCURSOR in my WndProc method.

protected override void WndProc(ref Message m)
{
    switch (m.Msg)
    {
        case WM.SETCURSOR:
            base.WndProc(ref m);
            int lowWord = (m.LParam.ToInt32() << 16) >> 16;
            if (lowWord == HTCLIENT && FullScreen)
            {
                SetCursor(IntPtr.Zero); // hides cursor
                m.Result = (IntPtr)1; // return TRUE; equivalent in C++
            }
            return;
    }
}

然而,当光标在客户区时(又名 LOWORD(lParam) == HTCLIENT),WM_SETCURSOR 永远不会在 WndProc 中被触发.因此,当光标位于客户区时,我实际上从未收到 WM_SETCURSOR 消息,只有在 LOWORD(lParam) != HTCLIENT 时才收到.

However when the cursor is in the client area (aka LOWORD(lParam) == HTCLIENT), WM_SETCURSOR is never triggered in WndProc. So I never actually get the WM_SETCURSOR message when the cursor is in the client area and only get it when LOWORD(lParam) != HTCLIENT.

但是在 Spy++ 中,它清楚地表明应用程序收到了 WM_SETCURSORWM_MOUSEMOVE 消息.

However in Spy++, it clearly shows that the application received the WM_SETCURSOR and WM_MOUSEMOVE messages.

消息在哪里丢失/处理?我需要做什么才能在 C# 中接收 WM_SETCURSOR 消息?

Where is the message getting lost/handled? What do I have to do in order to receive the WM_SETCURSOR message in C#?

推荐答案

我的应用程序有几个面板覆盖了该应用程序.所以另一位用户友好地向我指出,由于每个控件都有自己的 WndProcWM_SETCURSOR 方法没有被传递到它下面的表单.为了接收这些消息,我必须用自己的 WndProc 方法覆盖每个面板.

My application has several panels covering the application. So another user pointed out to me kindly that since every control has its own WndProc, the WM_SETCURSOR method wasn't being passed on to the form underneath it. In order to receive those messages, I would have to override each of those panels with its own WndProc method.

但是,如果没有控件覆盖光标所在的表单,则上述代码确实有效.

However the above code does work if there are no controls covering the form where the cursor is.

这篇关于如何在 C# 中处理 WM_SETCURSOR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆