我遇到了几种将渐变样式应用于Windows窗体应用程序中的对象的方法。所有的方法都涉及覆盖OnPaint方法。但是,我正在根据验证在运行时查看更改风格。应用新颜色(使用渐变)获取窗体按钮onClick
如何将新渐变样式应用于已经呈现的按钮(就像我可以使用BackColor一样)?
R, C.
UPDATE:这是我目前正在使用的代码。这似乎没有任何效果
private void Button_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
Graphics g = e.Graphics;
g.DrawString("This is a diagonal line drawn on the control",
new Font("Arial", 10), System.Drawing.Brushes.Blue, new Point(30, 30));
g.DrawLine(System.Drawing.Pens.Red, btn.Left, btn.Top,
btn.Right, btn.Bottom);
this.btn.Invalidate();
}
被称为由
btn.Paint += new PaintEventHandler(this.Button_Paint);
进一步更新当前代码
private void Button_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
g.DrawString("This is a diagonal line drawn on the control",
new Font("Arial", 10), System.Drawing.Brushes.Blue, new Point(30, 30));
g.DrawLine(System.Drawing.Pens.Red, btn.Left, btn.Top,
btn.Right, btn.Bottom);
}
private void btn_Click(object sender, EventArgs e)
{
btn.Paint += new PaintEventHandler(this.Button_Paint);();
btn.Invalidate();
}
谢谢,但我仍然没有发生任何事情。通过我的代码,我可以确认两行(添加新的PaintEventHandler并调用Invalidate())正在被击中。但是,函数Button_Paint没有被调用。我甚至在按钮上添加了一个Refresh(),没有任何改变。 – 2012-07-05 14:04:15
奇怪...我有这个作为示例应用程序运行...我会发布整个事情。 – andypaxo 2012-07-05 14:11:21
谢谢。我将用我正在运行的代码更新我的问题。 – 2012-07-05 14:15:28