However, there are some situations in which WPF cannot use ClearType, and has to resort to grayscale antialiasing (it cannot render aliased text because of its pixel independent architecture) which comes out pretty blurry for small text sizes, in my opinion.
Here’s when WPF can’t use ClearType:
- Window/Popup AllowsTransparency = true. This creates an HwndSource with UsesPerPixelOpacity = true (i.e. a layered window.) All context menus and tooltips in WPF have this turned on with no trivial way of turning it off. More on this later on.
- Visual (or some ancestor) has a Bitmap Effect. (Sidebar: You should really avoid using those on any complex Visual. They yield the worse performance. If you want a shadow under something, take an empty Rectangle or some other Shape, apply the effect on it and put it under the more complex Visual.)
- Text from another Visual appearing in a VisualBrush.
- Rendering a Visual using RenderTargetBitmap.
- Setting the HwndTarget.BackgroundColor to Transparent (needed for extending DWM glass to client area.)
Also, there are registry settings that can enable, disable or configure ClearType, both system-wide and WPF-specific.
In Vista, ClearType is turned on by default, so WPF also uses it. In XP, however, it is not. And so by default WPF is rendering grayscaled text. From my experience, ClearType is better even on CRT monitors, so when I write WPF applications, if ClearType is off, I display a message recommending to turn it on, and make the WPF-specific registry changes.
Now, for my woes. Take a look at these screenshots:
The left one is with ClearType, the right one without. I hope the difference is clear. As I mentioned earlier, context menus, tooltips and combo box use the Popup class, which has the AllowsTransparancy set to true. This is hardcoded. The reason for this is obvious: the designers of WPF wanted you to be able to customize these windows as you saw fit. And it can truly be used to do wonderful things (see this styled tooltips example. Quite effortless, if you consider what you had to do to get this done in Win32.) But I think readability is more important in these cases. At any rate, this should be configurable.
Aside from the text issues, layered windows’ performance is much worse than normal windows. Even under Vista, where they are hardware-accelerated, the menu highlight is lagging after the mouse sometimes.
Frustrated a bit, I came up with a somewhat dubious solution to these issues.
For ComboBoxes and MenuItems, I created an attached Dependency Property, which, when attached to a control, attempts to find the “PART_Popup” in its template and set its AllowsTransparency property to false. Caveats:
- You lose the animation when opening a combo box (slide) or a menu (fade).
- You lose the shadow.
- If you apply a transform, the popup will not match it (Then again, who would want a skewed combo box? But a rotated menu might be useful.)
For ContextMenus and ToolTips, I create subclasses, overrode IsOpenProperty metadata and added an additional changed handler. The Framework Property Metadata documentation states that:
The actual property system behavior for PropertyChangedCallback is that implementations for all metadata owners in the hierarchy are retained and added to a table, with order of execution by the property system being that the most derived class’s callbacks are invoked first. Inherited callbacks run only once, counting as being owned by the class that placed them in metadata.
Either I don’t understand it well or the documentation is wrong, since the method I specified ran after the original method. To solve that, I wrote a class that inherits FrameworkPropertyMetadata and reverses the execution order, so I could create the Popup myself without setting the AllowsTransparency to true. Caveats:
- I use reflection to get to private fields. Yes, I know it’s bad… 😛
- Again, you lose shadows and animations.
- Tooltips have rounded corners by default (at least on Vista) so you’ll see gray 1-pixel dots on the corners. But you can change the tooltip’s default style to get rid of this.
I tried to regain the shadows using cheaper means (CS_DROPSHADOW window class style) but it’s difficult to reach.
You may think I’m crazy to go through all of that just for a few blurry texts, but I think this really impacts the overall readability of my applications.
Tags.NET 4 Animation AppFabric Async Axum Blog C# ClearType Cloud CLR CodeValue Concurrency Contests Deep Zoom Experiments Generics Google Ink Lectures Modeling Performance Personal Pivot Prism Programming Languages Prolog Reflector ReSharper Roslyn RoslynPad Silverlight Themes Threading Tips WCF Windows 7 Windows 2003 Windows Azure Windows Forms Windows Phone Windows Vista WPF WPF Contrib XAML Zune
- August 2016
- March 2016
- February 2016
- December 2015
- July 2015
- April 2015
- September 2014
- April 2014
- March 2014
- July 2013
- May 2013
- February 2013
- June 2012
- May 2012
- June 2011
- November 2010
- August 2010
- July 2010
- June 2010
- March 2010
- December 2009
- November 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- November 2007
- September 2007
- May 2007
- February 2007
- November 2006
- October 2006
- February 2006
- August 2005
- August 2004
- July 2004
- June 2004
- May 2004