我有按钮,当我将光标对准按钮时,我希望按钮背景发生变化 所以我使用mouseenter和mouseleave,它工作,但我想要更改背景时更平滑的效果
抱歉英语不好,我在c#看到了绿色 请帮帮我
我的剧本
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));
}
}
答案 0 :(得分:0)
这个想法与this answer中的想法相同。而不是使用Timer来更改BackColor的alpha,您将需要更改Button的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_Shown事件中调用它。确保所有图像尺寸都合适,并为ImageList提供32的ColorDepth!