MouseHover/MouseLeave event on the whole entire window

前端 未结 4 1872
再見小時候
再見小時候 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:48

    Ovveride the MouseLeave event to not trigger so long as the mouse enters a child control

        protected override void OnMouseLeave(EventArgs e)
        {
            if (this.ClientRectangle.Contains(this.PointToClient(Control.MousePosition)))
                return;
            else
            {
                base.OnMouseLeave(e);
            }
        }
    
    0 讨论(0)
  • 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
                    //...
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-30 10:01

    On your user control create a mousehover Event for your control like this, (or other event type) like this

    private void picBoxThumb_MouseHover(object sender, EventArgs e)
    {
        // Call Parent OnMouseHover Event
        OnMouseHover(EventArgs.Empty);
    }
    

    On your WinForm which hosts the UserControl have this for the UserControl to Handle the MouseOver so place this in your Designer.cs

    this.thumbImage1.MouseHover += new System.EventHandler(this.ThumbnailMouseHover);
    

    Which calls this method on your WinForm

    private void ThumbnailMouseHover(object sender, EventArgs e)
    {
    
        ThumbImage thumb = (ThumbImage) sender;
    
    }
    

    Where ThumbImage is the type of usercontrol

    0 讨论(0)
  • 2020-12-30 10:04

    When the mouse leave event is fired one option is to check for the current position of the pointer and see if it within the form area. I am not sure whether a better option is available.

    Edit: We have a similar question which might be of interest to you. How to detect if the mouse is inside the whole form and child controls in C#?

    0 讨论(0)
提交回复
热议问题