C#检测鼠标移动消息

当C#窗口上有其它控件时,窗口本身检测不到消息。
1、使用WndProc、MouseMove不行,比如

 1 protected override void WndProc(ref Message m)
 2         {
 3             switch (m.Msg)
 4             {
 5                 case WM_MOUSEMOVE:
 6                     Debug.Write(string.Format("CurrentDate:{0}", DateTime.Now) + Environment.NewLine);
 7                     break;
 8             }
 9             base.WndProc(ref m);
10         }

2、使用PreFilterMessage

 1 public partial class Form1 : Form, IMessageFilter
 2 {
 3 //构造函数里增加
 4 public Form1()
 5 {
 6     InitializeComponent();
 7     Application.AddMessageFilter(this);
 8 
 9 }
10 public bool PreFilterMessage(ref Message msg)
11 {
12      const int WM_MOUSEMOVE = 0x200;
13      if (msg.Msg == WM_MOUSEMOVE)
14      {
15           TestPopUp();
16      }
17      return false;
18 }
19 }

 

posted @ 2018-02-01 09:32  zhaogaojian  阅读(1238)  评论(0编辑  收藏  举报