绘制事件执行后按钮文本消失
本文关键字:文本 消失 按钮 事件 执行 绘制 | 更新日期: 2023-09-27 18:00:28
这很奇怪。当我在按钮的绘制事件中这样做时:
using (LinearGradientBrush brush = new LinearGradientBrush(button1.ClientRectangle,
Color.Orange,
Color.Red,
90F))
{
e.Graphics.FillRectangle(brush, this.ClientRectangle);
}
按钮的文本消失。我怎样才能取回短信?
您基本上是在控件的顶部绘制。您应该尝试对按钮进行子类化,并覆盖OnPaintBackground
以在文本后面绘制。
为什么不设置按钮的背景笔刷?
- 呼叫
button1.Invalidate()
。这将重新绘制文本。。。和整个按钮 - 以前的建议可能不会产生你想要的。由于您决定自己绘制按钮,您还将负责在背景上绘制文本,请参见
DrawString
如果您不想使用background属性或覆盖OnPaintBackground方法,可以执行以下操作:
//你的背景绘画代码
public void DrawText(Graphics g, Rectangle bounds, string text, Font font, Brush brush)
{
float x = bounds.Width / 2;
float y = bounds.Height /2;
SizeF textSize = g.MeasureString(text, font);
x = (x - (textSize.Width / 2) + bounds.X);
y = (x - (textSize.Height / 2) + bounds.Y);
g.DrawString(text, font, brush, new PointF(x, y));
}
并像这样使用
DrawText(g, button1.ClientRectangle, button1.Text, button1.Font, new SolidBrush(button1.ForeColor));
这些都没有经过实际测试,尽管。。。。
编辑:如果你选择走这条路,你必须记住,当控件被调整大小时,它将需要重新绘制控件。