MouseHover and MouseLeave Events controlling

前端 未结 4 1703
无人及你
无人及你 2020-12-11 09:18

I was building a simple form with one simple effect- opacity reduced when mouse is not over the form, and form becomes opaque when mouse is over it. I am currently encounter

4条回答
  •  忘掉有多难
    2020-12-11 09:39

    I might be wrong, but why would you use MouseHover event? MouseHover detects when the mouse stop moving on the form and is usually used to show Tooltips.

    The event you are looking for is MouseEnter which is the opposite of MouseLeave and detects when the mouse enters the client rect of a window.

    In the Leave event, just check if the cursor position is in the window client rect to know if it did actually leave the form or if it is just on top of child control.

    Ofc if you use a region, you'll have to adapt the code.

     private void Form1_MouseEnter(object sender, EventArgs e)
        {
            this.Opacity = 1;
        }
    
        private void Form1_MouseLeave(object sender, EventArgs e)
        {
    
            if (!this.ClientRectangle.Contains(this.PointToClient(Cursor.Position)))
            {
                this.Opacity = 0.5;
            }
        }
    

提交回复
热议问题