如何使用C#在计算器中创建类似的按钮动画 [英] How to create button animation similar in calculator with C#
本文介绍了如何使用C#在计算器中创建类似的按钮动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当您将鼠标悬停在按钮上时,有人可以像在Win7计算器中那样创建winform动画吗,当前我使用一堆图像,然后在backgroundworker中将其循环,但是我认为它是错误的,这是我的代码:
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:
这会在鼠标进入时发生
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);
}
}
}
而这在鼠标离开时
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);
}
}
}
注意:我只是使用AllowDrop所以我不必费心声明新变量,我有42个按钮,所以我认为我需要更有效的解决方案。
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
支持不透明度(级别为1-100或0-1的两倍)。有关更多信息,请参见此。 - 在
中添加两个公共const int值
类,用于从外部轻松安全地进行范围检查。值可以是0、100或0、1或其他值,具体取决于您实现的不透明度。MaxOpacity
和MinOpacity
OpacityPictureBox - 制作一个
AnimatedPictureBox:UserControl
包含1个PictureBox
名为pbNormal
和1个OpacityPictureBox
名为opbHover
,都Dock = DockStyle.Fill
和一个名为timer $的计时器c $ c>。确保
pbNormal
低于opbHover
。 - 具有三个公共属性:
-
普通
类型为图片
的类型,其委派为pbNormal.Image
-
悬停
类型为Image
委托给opbHover.Image
-
AnimationInterval
int
分配到timer.Interval
- Make an
OpacityPictureBox : PictureBox
which supports opacity (in levels of 1-100 or double 0-1). See this for more information. - Add two public const int values of
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. - Make an
AnimatedPictureBox : UserControl
which holds 1PictureBox
namedpbNormal
and 1OpacityPictureBox
namedopbHover
, bothDock = DockStyle.Fill
, and one timer namedtimer
. Make sure thatpbNormal
is belowopbHover
. - Have three public properties:
Normal
of typeImage
which delegates intopbNormal.Image
Hover
of typeImage
which delegates intoopbHover.Image
AnimationInterval
of typeint
which delgates intotimer.Interval
代码:
private void Animate(int animationDirection) { this._animationDirection = animationDirection; this.timer.Start(); }
- 覆盖
OnMouseEnter
和OnMouseLeave
: - Override
OnMouseEnter
andOnMouseLeave
:
代码:
protected override void OnMouseEnter(EventArgs e) { this.Animate(1); base.OnMouseEnter(e); } protected override void OnMouseLeave(EventArgs e) { this.Animate(-1); base.OnMouseEnter(e); }
- 收听
定时器勾选
事件,并带有以下内容: - Listen to
timer.Tick
event and with this:
代码:
private void timer_Tick(object sender, EventArgs e) { var hoverOpacity = this.opbHover.Opacity + this._animationDirection; if (hoverOpacity < OpacityPictureBox.MinOpacity || hoverOpacity > OpacityPictureBox.MaxOpacity) { this.timer.Stop(); return; } this.opbHover.Opacity = hoverOpacity; }
这篇关于如何使用C#在计算器中创建类似的按钮动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
-
查看全文