计时器改变背景按钮
本文关键字:按钮 背景 改变 计时器 | 更新日期: 2023-09-27 18:06:38
我有一个按钮,当我把光标放在按钮上时,我想改变按钮的背景所以我使用了鼠标进入键和鼠标离开键,效果很好但是我想在改变背景时效果更平滑
抱歉我的英语不好,我是c#新手请帮帮我
my script
private void button1_Click(object sender, EventArgs e)
{
}
private void button1_MouseEnter(object sender, EventArgs e)
{
button1.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.administrator_icon));
}
private void button1_MouseLeave(object sender, EventArgs e)
{
button1.BackgroundImage = ((System.Drawing.Image)(Properties.Resources.alhaq));
}
}
思路与本回答相同。而不是使用定时器来改变背景色的alpha,你会想要改变你的按钮的ImageList的索引。
更改它与其他帖子非常相似:
private void button3_MouseEnter(object sender, EventArgs e)
{
timer1.Interval = 25;
timer1.Start();
}
private void button3_MouseLeave(object sender, EventArgs e)
{
timer1.Stop();
button3.ImageIndex = 0;
index = 1;
}
int index = 1;
private void timer1_Tick(object sender, EventArgs e)
{
index++; ;
button3.ImageIndex = index;
if (index >= imageList1.Images.Count)
{ timer1.Stop(); button3.ImageIndex = 1; index = 1; }
}
区别在于ImageList。除非您想逐帧创建它,否则动态创建它的简单例程就可以完成这项工作。首先,必须将非活动映像和活动映像分别分配给索引0和1。然后调用它将从图像nr 1创建step
额外的图像:
public void makeTransPix(int steps)
{
using (Bitmap bmp = new Bitmap( imageList1.Images[1] ) )
{
for (int t = 0; t < steps; t++)
{
Bitmap tBmp = new Bitmap(bmp.Width, bmp.Height);
using (var g = Graphics.FromImage(tBmp))
{
Rectangle R = new Rectangle(0, 0, bmp.Width, bmp.Height);
using (var brush = new SolidBrush(button1.BackColor))
g.FillRectangle(brush, R);
var colorMatrix = new ColorMatrix();
colorMatrix.Matrix33 = t / (float)steps;
var imageAttributes = new ImageAttributes();
imageAttributes.SetColorMatrix(
colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
g.DrawImage(bmp, R, 0, 0, bmp.Width, bmp.Height, GraphicsUnit.Pixel, imageAttributes);
}
imageList1.Images.Add(tBmp);
}
}
}
你可以调整步长来改变速度。您可以在form_showed事件中调用它。确保所有的图像大小合适,并给你的ImageList颜色深度32!