OnPaint 覆盖无法在图片框顶部绘制白色文本
我正在编写一个编辑歌曲元数据的应用程序。为此,我有一个窗口,其中选择了歌曲标签,您可以在旧标签和新标签之间进行选择。我有一个自定义控件,每个标签框有 3 个图片框。左盖一张,中盖一张,右盖一张。然后,我覆盖了 UserControl 的 OnPaint,以将文本绘制到控件上。除非我尝试在其中包含图像的图片框顶部使用白色文本,否则效果很好。白色似乎变得半透明。我在下面附上了图片来证明这一点。
黑色文本
黑色文本图像 http://bentrengrove.com.au/blackText .PNG
白色文本
白色文本图像 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();
}
如果有人有任何想法为什么我不能用白色绘制,我们将不胜感激。
I am writing an application that edits meta data of songs. To do this I have a window that has selected song tags and you can choose between the old and the new. I have a custom control with 3 pictureboxes in it for each tag box. One picture box for the left cap, one for the middle and one for the right cap. I then have overwritten OnPaint for the UserControl to draw the text onto the control. This works fine except when I try to use white text on top of a picture box that has an image in it. The white seems to become semi transparent. I have attached pictures below demonstrating this.
Black Text
Image of Black Text http://bentrengrove.com.au/blackText.PNG
White Text
Image of White Text http://bentrengrove.com.au/WhiteText.PNG
Here is the code for my OnPaint method
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();
}
Please if anyone has any ideas why I can't draw in white the help would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我自己已经解决了这个问题。该问题是由于订单项目绘制在表单上造成的。因为我是在控件 OnPaint 方法中绘制图片框,所以首先调用 OnPaint。即使 base.OnPaint 是我的 onPaint 方法中的第一项,控件的绘画也会在此方法完成后绘制。通过删除中间的图片框并在 OnPaint 中绘制其图像,文本可以正确绘制白色。我仍然不确定为什么这个问题只出现在白色文本上,而不会出现在其他颜色上。
I have solved this problem myself. The problem is due to the order items are painted on the form. Because I was painting over a picturebox in the controls OnPaint method, OnPaint is called first. Even with base.OnPaint being the first item in my onPaint method, the Painting of the controls are painted after this method finishes. By removing the middle picturebox and painting its image inside OnPaint the text draws white correctly. I am still not sure why this problem only appears on white text and does not occur on other colors.
您绝对确定您最终使用的刷子是白色的吗?您能否验证
_isSelected
和this.Enabled
都为true
吗?检查调试器以查看这些值。我在
DimGray
背景上放置了一些Gray
文本,它看起来与您现在的类似。Are you absolutely sure that the brush you end up using is white? Can you verify that both
_isSelected
andthis.Enabled
istrue
? Check in the debugger to see the values.I put some
Gray
text on aDimGray
background and it looks similar to how you have it now.