Controlling the MouseHover and MouseLeave Events

I created a simple shape with one simple effect - opacity, reduced when the mouse is not over the shape, and the shape becomes opaque when the mouse is over it. I am currently facing several difficulties: -

  • First, I did this -

     this.MouseHover += new EventHandler(Form1_MouseHover);
     this.MouseLeave += new EventHandler(Form1_MouseLeave);
    
          

    But I also had 1 richtextbox and as the mouse moved the form lost transparency again. I had to add this too: -

     richTextBox1.MouseHover+=new EventHandler(Form1_MouseHover);
     richTextBox1.MouseLeave+=new EventHandler(Form1_MouseLeave);
    
          

    I wonder if there was any better way, because there is still some white space between the borders of the richtextbox and the form, and the form loses its opacity when the mouse cursor goes there.

  • If the mouse is NOT over the shape (assuming initially), the shape is less opaque. Now, I want the shape to become opaque as soon as the mouse moves over it, but this only happens when the mouse movement over the shape stops completely. If I keep moving the mouse over the shape, it doesn't become opaque. Is it a problem with how events are stored in the message queue and all of this or I can do something because I have seen applications with the effect I am trying to implement.

+2


source to share


4 answers


MouseEnter / Leave events are too unreliable for this. Your best bet is to use a timer that checks if the mouse is in the window. Drop the timer on the form and make the code like this:

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
        this.Opacity = 0.99;
        timer1.Interval = 200;
        timer1.Enabled = true;
        timer1.Tick += timer1_Tick;
    }
    protected override void OnLoad(EventArgs e) {
        base.OnLoad(e);
        timer1_Tick(this, e);
    }
    private void timer1_Tick(object sender, EventArgs e) {
        this.Opacity = this.Bounds.Contains(this.PointToClient(Cursor.Position)) ? 0.99 : 0.20;
    }
}

      



Btw: Avoid increasing the Opacity to 1.0, which forces you to restore your own window and can have a lot of side effects. It is best to use 0.99.

+6


source


I may be wrong, but why are you using the MouseHover event? MouseHover detects when the mouse stops moving around the form and is typically used to display tooltips.

The event you're looking for is MouseEnter , which is the opposite of MouseLeave and detects when the mouse enters the client's window rectangle.

In the Leave event, just check if the cursor position is in the window client to see if it actually left the form or is just over the child control.



Ofc, if you are using a region you will 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;
        }
    }

      

+4


source


Add a timer then use below in timer event. The above answers will not work if you have custom / custom controls in your form. So you need to useClientRectangle

this.Opacity = this.ClientRectangle.Contains(this.PointToClient(Cursor.Position)) ? 0.99 : 0.20;

      

+1


source


private void Form1_MouseEnter(object sender, EventArgs e)
{
    this.Opacity = 1.0;
}

private void Form1_MouseLeave(object sender, EventArgs e)
{ 
    this.Opacity = 0.8;
}

      

0


source







All Articles