阅读背景:

.Net Winforms中Graphics.DrawString和TextRenderingHint的可能错误

来源:互联网 

Here's the OnPaint method of a control that simply inherits from control and provides a property to get/set the textrenderinghint:

这是一个控件的OnPaint方法,它简单地从控件继承并提供了一个属性来获取/设置textrenderinghint:

 Private _mode as TextRenderingHint = SystemDefault.
 Public Property Mode as TextRenderingHint
    Get & Set _mode
 ...

 Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
    Dim g = e.Graphics
    Dim savMode = g.Save
    g.TextRenderingHint = Me._mode
    g.DrawString(Me.Text, Me.Font, Brushes.Black, 0, 0)
    g.Restore(savMode)
    MyBase.OnPaint(e)
 End Sub

Now, if you place two of these on a form, leave the first as default and change the second to AntiAlias, it looks fine at design-time but when you run the app, the first label's rendering mode has changed. Its as if the DrawString method has changed the systemdefault.

现在,如果将其中两个放在表单上,​​将第一个保留为默认值并将第二个更改为AntiAlias,它在设计时看起来很好,但是当您运行应用程序时,第一个标签的呈现模式已更改。就像DrawString方法改变了systemdefault一样。

Here's some observations:

以下是一些观察结果:

(1) If I explicity set the first control's mode to ClearTypeGridFit, which is the same as the default in my case, it fixes the problem.

(1)如果我明确地将第一个控件的模式设置为ClearTypeGridFit,这与我的情况下的默认模式相同,它可以解决问题。

(2) If you place a third control on the form and leave at the default mode, it fixes the problem.

(2)如果您在表单上放置第三个控件并保留默认模式,则可以解决问题。

(3) TextRenderer.DrawText doesn't replicate the problem.

(3)TextRenderer.DrawText不会复制问题。

(4) If I inherit label control and override the onpaint method to set the rendering mode, the problem is not replicated even though I set UseCompatibleTextRendering - which forces the label to render with DrawString instead of DrawText.

(4)如果我继承标签控件并覆盖onpaint方法来设置渲染模式,即使我设置了UseCompatibleTextRendering,也不会复制问题 - 这会强制标签使用DrawString而不是DrawText进行渲染。

I'm on XP with cleartype enabled and using visual studio 2008 express edition.

我在XP上启用了cleartype并使用了visual studio 2008 express edition。

ETA: I've tried it in C# and the same thing happens

ETA:我在C#中尝试过,同样的事情发生了

2 个解决方案

#1


1  

This sounds like the issue we ran into before. In your app startup code, is there a call to Application.SetCompatibleTextRenderingDefault(true)? (Or it might be set to false, I forget).

这听起来像我们之前遇到过的问题。在您的应用启动代码中,是否有对Application.SetCompatibleTextRenderingDefault(true)的调用? (或者它可能被设置为假,我忘了)。

If so, toggle the state of that bool to change the text rendering mode and it should work as expected.

如果是这样,切换该bool的状态以更改文本呈现模式,它应该按预期工作。

#2


0  

I had a similar problem. I called this:

我有类似的问题。我打电话给:

Image i = new Bitmap(size, size);
Graphics g = Graphics.FromImage(i);

// When this line is uncommented TextRenderingHint is broken for ALL other Graphics-Objects.
// Setting "g.TextRenderingHint" later works sometimes in unpredictable ways.
//g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
...

My startup code looked like this:

我的启动代码看起来像这样:

[STAThread]
static void Main(string[] args)
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);

Then I tried what Judah Himango suggested and everything worked just fine.
This looks definitely like a bug to me!

然后我尝试了Judah Himango的建议,一切正常。这对我来说绝对是个错误!

So just make sure you call:

所以请确保你打电话:

    Application.SetCompatibleTextRenderingDefault(true);

Worked for me!

为我工作!


分享到: