不要那种让所有控件挨个处理的方式

解决方案 »

  1.   


    foreach(Control ctl in this.Controls)
    {
       ctl.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form2_MouseMove);
    }private void Form2_MouseMove(object sender, MouseEventArgs e)
    {}
      

  2.   

    protected override void WndProc(ref Message m)
    {
        if (m.Msg == 32)
        {
            Point p = this.PointToClient(Control.MousePosition);
            Console.WriteLine(p.X.ToString() + "-" + p.Y.ToString());
        }    base.WndProc(ref m);
    }
      

  3.   

    在Form中重载这个消息处理函数。
      

  4.   

    你的需求是什么?是要知道鼠标移动到什么位子还是要知道鼠标是否进入了某个范围?只是单纯的感知鼠标属否移动,用API或者WndProc都可以的
      

  5.   

    动辄用钩子不是个好习惯。使用消息过滤来检查鼠标移动:public partial class Form1 : Form, IMessageFilter
    {
        public Form1()
        {
            InitializeComponent();
            Application.AddMessageFilter(this);    }
        public bool PreFilterMessage(ref Message msg)
        {
            const int WM_MOUSEMOVE = 0x200;
            if (msg.Msg == WM_MOUSEMOVE)
            {
                this.Text = "Mouse = " + Cursor.Position;
            }
            return false;
        }
    }
      

  6.   

    4楼的方法,我试过了,当鼠标进入子控件区域的时候,窗体的WndProc是不会被调用的。
    也许是我测试不对,请你再确认一下?
      

  7.   

    之前试过不带Application.AddMessageFilter(this)的。加上再试试。
      

  8.   

    比如说用form做小悬浮窗,里面图片用pictruybox控件显示,当鼠标点中pictruybox的时候,窗体的鼠标事件是不响应的。
      

  9.   

    这个可行啊。gomoku这个高手啊。不知道怎么了,感觉c#论坛热心高手比以前的vc板块少。这种热心高手太少了。我之前有些问题都没得到答案。