Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 捕获用户控件中的所有鼠标事件_C#_.net_Winforms_Mouseevent - Fatal编程技术网

C# 捕获用户控件中的所有鼠标事件

C# 捕获用户控件中的所有鼠标事件,c#,.net,winforms,mouseevent,C#,.net,Winforms,Mouseevent,我试图捕获用户控件中的所有鼠标事件(即使是发生在子控件中的事件)。为此,我使用“覆盖WndProc”方法: protected override void WndProc(ref Message m) { System.Diagnostics.Debug.WriteLine(m.Msg.ToString()); base.WndProc(ref m); } 我对鼠标事件特别感兴趣,但这似乎不起作用。我确实获得了鼠标按钮向上/向下事件,但我没有获得鼠标移动和鼠标滚轮事件 有什么想法吗?你

我试图捕获用户控件中的所有鼠标事件(即使是发生在子控件中的事件)。为此,我使用“覆盖WndProc”方法:

protected override void WndProc(ref Message m)
{
  System.Diagnostics.Debug.WriteLine(m.Msg.ToString());
  base.WndProc(ref m);
}
我对鼠标事件特别感兴趣,但这似乎不起作用。我确实获得了鼠标按钮向上/向下事件,但我没有获得鼠标移动和鼠标滚轮事件


有什么想法吗?

你能做的最好是在你的控制下实施

 public class CustomMessageFilter:UserControl,IMessageFilter
{
    public bool PreFilterMessage(ref Message m)
    {
        //Process your message here
        throw new NotImplementedException();

    }
}
您可以在PreFilterMessage方法中编写消息过滤逻辑。 然后将其安装到Main方法中的消息过滤器列表中

 Application.AddMessageFilter(new CustomMessageFilter());

这是一个应用程序级钩子,意味着您可以控制应用程序中的所有Win32消息。

捕获控件中所有鼠标事件的正确方法是调用该控件的方法


通常这是一种临时状态,如拖放、用户绘图等。

实际上我尝试过(代码与您建议的几乎相同),但从未调用过该方法。您是如何将其注册到用户控件(MessageFilter)的?例如:Application.AddMessageFilter(this);(在UCs构造函数中)不,您需要在消息循环开始之前安装,也就是说,在Application.Run()之前;如果不可能的话?用户控件将在运行时由本地C++应用程序实例化。