覆盖按钮,使backcolor从左到右填充像进度条

问题描述:

我想创建自定义按钮。 无论按钮是否添加了控件,它都会覆盖Paint Event,并使颜色移动并从左向右填充,这看起来像是进度条。覆盖按钮,使backcolor从左到右填充像进度条

这是我的逻辑。但是,我认为这不是一个正确的方法。请建议正确的方法来获得动画BackColor。 此按钮大小的代码库:

private void customButton() 
{ 
    Button customButton=new Button(); 
     customButton.Size.width=1; 
     custonButton.BackColor=Color.Green; 
    this.Controls.Add(customButton); 
    for (int i = 0; i<50; i+= 5) 
    { 

     customButton.Size.width += 1; 
    } 
} 

但是,该代码创造一个按钮有宽度= 50像素,和宽度将从左向右扩大,像进度条 我想创造和超越默认OnPaint事件,创建一个按钮有50个像素,当父控件添加backColor时,backColor将从左向右移动(动画)。 我是C#的新手,对OnPaint事件不太了解。

+1

你看了这里:http://msdn.microsoft.com/en-us/library/vstudio/cksxshce(v=vs.100).aspx? – Cadde 2014-12-06 10:43:43

+0

标签是一个更好的控制来做到这一点。 – 2014-12-06 15:12:41

关于如何更改按钮的背景内容的快速而脏的示例。

var bmp = new Bitmap(button1.Width, button1.Height); 
var g = Graphics.FromImage(bmp); 

var rect = new Rectangle(0, 0, bmp.Width, bmp.Height); 
using (var lgb = new LinearGradientBrush(rect, Color.Black, Color.LimeGreen, 0f)) 
    g.FillRectangle(lgb, rect); 

button1.BackgroundImage = bmp; 

按钮本身可以被修改为不具有3D风格,如果你想,调整其大小依然工作得很好。这里的要点是画它而不是调整它的大小。

+0

谢谢,你能给我一些如何按钮和标签是由C#的链接? – 2014-12-06 10:58:14

+0

@LinhBi你的意思是这样的:http://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/Button.cs,cb5c00483936b8f7? – Cadde 2014-12-06 12:57:05