MouseHover/MouseLeave event on the whole entire window

前端 未结 4 1889
再見小時候
再見小時候 2020-12-30 09:46

I have Form subclass with handlers for MouseHover and MouseLeave. When the pointer is on the background of the window, the events work fine, but wh

4条回答
  •  春和景丽
    2020-12-30 09:57

    There is no good way to make MouseLeave reliable for a container control. Punt this problem with a timer:

    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
            timer1.Interval = 200;
            timer1.Tick += new EventHandler(timer1_Tick);
            timer1.Enabled = true;
        }
    
        private bool mEntered;
    
        void timer1_Tick(object sender, EventArgs e) {
            Point pos = this.PointToClient(Cursor.Position);
            bool entered = this.ClientRectangle.Contains(pos);
            if (entered != mEntered) {
                mEntered = entered;
                if (!entered) {
                    // Do your leave stuff
                    //...
                }
            }
        }
    }
    

提交回复
热议问题