【发布时间】:2011-02-14 22:40:01
【问题描述】:
我正在编写一个编辑歌曲元数据的应用程序。为此,我有一个选择歌曲标签的窗口,您可以在旧的和新的之间进行选择。我有一个自定义控件,每个标签框都有 3 个图片框。左帽一个,中间一个,右帽一个。然后我覆盖了 UserControl 的 OnPaint 以将文本绘制到控件上。这很好用,除非我尝试在其中包含图像的图片框顶部使用白色文本。白色似乎变得半透明。我在下面附上了图片来证明这一点。
黑色文字
Image of Black Text http://bentrengrove.com.au/blackText.PNG
白色文字
Image of White Text http://bentrengrove.com.au/WhiteText.PNG
这是我的 OnPaint 方法的代码
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
Brush b;
Font f;
if (_isSelected && this.Enabled) //Determines if the tag has the boxes visible, i.e is a selected tag
{
b = new SolidBrush(Color.White);
f = new System.Drawing.Font("Segoe UI", 8, FontStyle.Regular);
}
else
{
b = new SolidBrush(Color.Gray);
f = new System.Drawing.Font("Segoe UI", 8, FontStyle.Regular);
}
var textSize = g.MeasureString(_text, f); //We will resize the tag boxes based on the size of the text
StringFormat drawFormat = new StringFormat();
drawFormat.Alignment = StringAlignment.Near;
RectangleF layoutRectangle = new RectangleF(leftCap.Width, 1, textSize.Width, 16);
if (textSize.Width >= 105)
_text = String.Format("{0}...", _text.Substring(0, 15)); //There is only so much room to display text
middle.Width = (int)textSize.Width + rightCap.Width;
rightCap.Left = middle.Left + middle.Width - rightCap.Width;
g.DrawString(_text, f, b, layoutRectangle, drawFormat); //Draw the string for this control based on what has been set to text
//Clean up
g.Dispose();
b.Dispose();
f.Dispose();
}
如果有人知道为什么我不能用白色画出任何想法,我们将不胜感激。
【问题讨论】:
-
如果你使用更大的字体,它会显示得更好吗?将 FontStyle 设置为 FontStyle.Bold 有帮助吗?我觉得这是一个抗锯齿问题。
-
不,不幸的是它也有同样的问题,看起来有点灰色,但加粗。
-
加粗文字图片:link
标签: c# winforms windows-7 gdi+