【发布时间】:2012-07-05 11:27:14
【问题描述】:
我遇到了几种将渐变样式应用于 Windows 窗体应用程序中的对象的方法。所有方法都涉及重写 OnPaint 方法。但是,我正在根据验证在运行时更改样式。
如何将新的渐变样式应用到已经渲染的按钮(就像我可以使用 BackColor 一样)?
R, C.
更新:这是我目前使用的代码。好像没有效果
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();
}
【问题讨论】: