Can someone help about create a winform animation like in Win7 Calculator when you hover mouse over button, currently i use bunch of image then looping it in backgroundworker, but i think its wrong, this is my code:
this occur when mouse enter,
private void bgTurnOn_DoWork(object sender, DoWorkEventArgs e)
{
Label labelSender = (Label)e.Argument;
int ii = labelSender.ImageIndex;
for (int i = ii + 4; i <= 11; i++)
{
if (labelSender.AllowDrop)
{
labelSender.ImageIndex = i;
Thread.Sleep(40);
}
}
}
and this when mouse leave
private void bgTurnOff_DoWork(object sender, DoWorkEventArgs e)
{
Label labelSender = (Label)e.Argument;
int ii = labelSender.ImageIndex;
for (int i = ii; i >= 0; i--)
{
if (!labelSender.AllowDrop)
{
labelSender.ImageIndex = i;
Thread.Sleep(80);
}
}
}
note: I just use AllowDrop so I do not bother to declare new variable, i have 42 button, so i think i need more efficient solution.
It seems that you want a glow effect, so you can use the next idea:
OpacityPictureBox : PictureBoxwhich supports opacity (in levels of 1-100 or double 0-1). See this for more information.MaxOpacityandMinOpacityto theOpacityPictureBoxclass, for easy and safe range checks from the outside. The values might be 0, 100 or 0, 1, or something else, depending on your implementation of opacity.AnimatedPictureBox : UserControlwhich holds 1PictureBoxnamedpbNormaland 1OpacityPictureBoxnamedopbHover, bothDock = DockStyle.Fill, and one timer namedtimer. Make sure thatpbNormalis belowopbHover.Normalof typeImagewhich delegates intopbNormal.ImageHoverof typeImagewhich delegates intoopbHover.ImageAnimationIntervalof typeintwhich delgates intotimer.IntervalAnimatedPictureBox, after callingInitializeComponents, doopbHover.Opacity = 0;. You can also dothis.Cursor = Cursors.Hand;if you want the cursor to change into a hand when hovering over it._animationDirectionof typeint, which will be -1 or 1.Code:
OnMouseEnterandOnMouseLeave:Code:
timer.Tickevent and with this:Code: