更新时间:2023-10-25 12:48:58
它似乎您想要发光效果,因此可以使用下一个想法:
It seems that you want a glow effect, so you can use the next idea:
OpacityPictureBox:PictureBox
支持不透明度(级别为1-100或0-1的两倍)。有关更多信息,请参见此。中添加两个公共const int值 MaxOpacity
和 MinOpacity
OpacityPictureBox
类,用于从外部轻松安全地进行范围检查。值可以是0、100或0、1或其他值,具体取决于您实现的不透明度。 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
OpacityPictureBox : PictureBox
which supports opacity (in levels of 1-100 or double 0-1). See this for more information.MaxOpacity
and MinOpacity
to the OpacityPictureBox
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 1 PictureBox
named pbNormal
and 1 OpacityPictureBox
named opbHover
, both Dock = DockStyle.Fill
, and one timer named timer
. Make sure that pbNormal
is below opbHover
.Normal
of type Image
which delegates into pbNormal.Image
Hover
of type Image
which delegates into opbHover.Image
AnimationInterval
of type int
which delgates into timer.Interval
代码:
private void Animate(int animationDirection)
{
this._animationDirection = animationDirection;
this.timer.Start();
}
OnMouseEnter
和 OnMouseLeave
:
OnMouseEnter
and OnMouseLeave
:代码:
protected override void OnMouseEnter(EventArgs e)
{
this.Animate(1);
base.OnMouseEnter(e);
}
protected override void OnMouseLeave(EventArgs e)
{
this.Animate(-1);
base.OnMouseEnter(e);
}
定时器勾选
事件,并带有以下内容:
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;
}