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 : PictureBox
which supports opacity (in levels of 1-100 or double 0-1). See this for more information.MaxOpacity
andMinOpacity
to theOpacityPictureBox
class, 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 : UserControl
which holds 1PictureBox
namedpbNormal
and 1OpacityPictureBox
namedopbHover
, bothDock = DockStyle.Fill
, and one timer namedtimer
. Make sure thatpbNormal
is belowopbHover
.Normal
of typeImage
which delegates intopbNormal.Image
Hover
of typeImage
which delegates intoopbHover.Image
AnimationInterval
of typeint
which delgates intotimer.Interval
AnimatedPictureBox
, 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._animationDirection
of typeint
, which will be -1 or 1.Code:
OnMouseEnter
andOnMouseLeave
:Code:
timer.Tick
event and with this:Code: