【发布时间】:2012-05-12 17:07:40
【问题描述】:
我有我的自定义按钮,我在其中覆盖了 OnPaint() 并仅在其中绘制文本。在运行时文本看起来不同 - 缺少字符之间的间距。这是按钮的设计和运行时的图像:
绘制方法如下:
protected override void OnPaint(PaintEventArgs pevent)
{
base.OnPaint(pevent);
if (base.ContainsFocus)
{
// Draw inner dotted rectangle when button is on focus
Pen pen = new Pen(Color.Gray, 3);
Point p = base.Location;
pen.DashStyle = System.Drawing.Drawing2D.DashStyle.Dot;
Rectangle rectangle = new Rectangle(4, 4, Size.Width - 8,
Size.Height - 8);
ControlPaint.DrawFocusRectangle(pevent.Graphics, rectangle);
}
// Draw the string to screen
SizeF sf = pevent.Graphics.MeasureString(displayText, this.Font,
this.Width);
Point ThePoint = new Point();
ThePoint.X = (int)((this.Width / 2) - (sf.Width / 2));
ThePoint.Y = (int)((this.Height / 2) - (sf.Height / 2));
pevent.Graphics.DrawString(displayText, Font,
new SolidBrush(Color.FromArgb(255, 255, 254, 255)), ThePoint);
this.Text = "";
}
知道我哪里出了问题以及如何处理吗?
【问题讨论】:
-
你用的是什么字体?
-
@Falle1234,字体 - Arial Rounded MT Bold,9pt
-
考虑改用
TextRenderer.DrawText方法。 -
我已尝试重现您的问题,但无法重现。这向我表明,您的 onPaint 方法没有问题。也许您的自定义按钮控件中的其他一些设置以某种方式搞砸了。
-
我从未尝试过任何这些,但默认情况下,一些代码被添加到
Program类中:Application.EnableVisualStyles();和Application.SetCompatibleTextRenderingDefault(false);- 这些可能与问题有关吗?这些可能在设计时没有设置。