I am creating a CustomButton control for my application. Now, What I want to do that when the mouse goes on the button it should show glow effect and when mouse leave it should get back to normal. But the glow effect should not displayed immediately. It should display with animation. just like Chrome Browser Tab Page. I have tried this logic in the button control.
This is my logic. But, I think this is not a proper way. please suggest proper way to get glow effect.
private void ShowGlow()
{
for (int i = 0; i<50; i+= 5)
{
Sleep(100);
Graphics g = this.CreateGraphics();
Color color = Color.FromArgb(i, 150,150,25);
g.FillRectangle(new SolidBrush(color), this.ClientRectangle);
}
}
Additional Details Visual Studio 2005, Windows XP, Windows Form Controls
I suggest you a simpler method. Create two images, with glow effect and without. And use this code.
On MouseEnter:
On MouseLeave:
This is what I usually do in my projects.
Here is some code that uses timers and overrides the OnPaint method. It skips by 10 instead of 1 because I was afraid you wouldn't see the effect fast enough otherwise. The Timer interval is in milliseconds and was set to 100 because that was what you were using in your original example for sleep. If you need the effect to work faster, you can reduce the interval. If it should be slower, you can increase the interval, or decrease how much you increment alpha with each tick.