views:

41

answers:

1

Here's the OnPaint method of a control that simply inherits from control and provides a property to get/set the 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.

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.

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

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

(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.

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

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

A: 

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).

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

Judah Himango
Unfortunately there is no call. I'd be surprised if that did solve the issue, though, because I'm drawing the text myself and explicitly using the gdi+ drawstring method. Also, with my observation 2, above, it seems to be a deeper issue.
Jules
Ok. Might still be worth a shot, add that call to your app startup. Try with both SetCompatibleTextRenderingDefault(true) and SetCompatibleTextRenderingDefault(false) and see what effect it has on your application. It's been awhile, having long since switched to WPF, but I seem to recall this having an effect even on the DrawString stuff.
Judah Himango