<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Blog &#124; BenHollis.net &#187; fonts</title>
	<atom:link href="http://benhollis.net/blog/tags/fonts/feed/" rel="self" type="application/rss+xml" />
	<link>http://benhollis.net/blog</link>
	<description>News about BenHollis.net and articles about Ben&#039;s interests</description>
	<lastBuildDate>Fri, 04 Jun 2010 05:39:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Setting the correct default font in .NET Windows Forms apps</title>
		<link>http://benhollis.net/blog/2007/04/11/setting-the-correct-default-font-in-net-windows-forms-apps/</link>
		<comments>http://benhollis.net/blog/2007/04/11/setting-the-correct-default-font-in-net-windows-forms-apps/#comments</comments>
		<pubDate>Wed, 11 Apr 2007 09:05:17 +0000</pubDate>
		<dc:creator>Ben Hollis</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[fonts]]></category>
		<category><![CDATA[Windows Forms]]></category>
		<category><![CDATA[Windows Vista]]></category>

		<guid isPermaLink="false">http://brh.numbera.com/blog/index.php/2007/04/11/setting-the-correct-default-font-in-net-windows-forms-apps/</guid>
		<description><![CDATA[I was working on XBList the other night when I realized something - the font used in its dialogs and the friends list wasn't Segoe UI. Segoe UI is the very pretty, ClearType-optimized new default dialog font in Windows Vista. In Windows XP and 2000, it's Tahoma, and in earlier editions it was Microsoft Sans [...]]]></description>
			<content:encoded><![CDATA[<p>I was working on <a href="http://brh.numbera.com/software/xblist/">XBList</a> the other night when I realized something - the font used in its dialogs and the friends list wasn't <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/uxguide/uxguide/Resources/WhatsNewInVista/fonts.asp">Segoe UI</a>. Segoe UI is the very pretty, ClearType-optimized new default dialog font in Windows Vista. In Windows XP and 2000, it's Tahoma, and in earlier editions it was Microsoft Sans Serif. You can see the subtle differences between them:</p>
<p class="blogimage"><img src='http://brh.numbera.com/blog/wp-content/uploads/2007/04/ms_system_fonts.png' alt='Microsoft System Fonts' /></p>
<p>In .NET and Windows Forms, the default font for controls is actually Microsoft Sans Serif, not the operating system's default dialog font! <a href="http://weblogs.asp.net/kdente/archive/2005/03/13/394499.aspx">Kevin Dente explains this on his blog.</a> This is not the only time Microsoft's dropped the ball on this - if you go through some dialogs in Vista you'll see that many of them use Tahoma or even Microsoft Sans Serif instead of Segoe UI. This is pretty funny, especially when Rule #1 of the <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/uxguide/uxguide/Resources/TopRules/TopRules.asp">Top Rules for the Windows Vista User Experience</a> is "Use the Aero Theme and System Font (Segoe UI)". <a href="http://blogs.msdn.com/michkap/archive/2006/06/11/626100.aspx">Mitch Kaplan offers up a pretty good explanation</a> for why getting it all right is very hard, but having a mix of old and new fonts still looks shoddy. </p>
<p>I don't want my apps to look shoddy. Embarrassingly enough, I've been hardcoding Tahoma in all my apps to get the more "modern" XP look.  Now that Vista's on the scene, it's clear that I want to select the correct default font for whichever OS my app is running on. As Kevin points out, <code>Control.DefaultFont</code> is no help here - it's what's driving Windows Forms' default choice of Microsoft Sans Serif in the first place. After some digging I found <a href="https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=115408">this Visual Studio feedback ticket (sign in required)</a>, where the Visual Studio guys explain that, while they couldn't fix the default, they did create a SystemFonts class to help out. They recommend putting this code in your Form's constructor:</p>
<div class="igBar"><span id="lcsharp-3"><a href="#" onclick="javascript:showPlainTxt('csharp-3'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-3">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Font</span> = SystemFonts.<span style="color: #0000FF;">DialogFont</span>;</div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">InitializeComponent<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Unfortunately, this doesn't work in a number of ways. The first is that on Vista, <code>SystemFonts.DialogFont</code> is... Tahoma! Closer, but not quite right yet. If you pop open SystemFonts in <a href="http://www.aisto.com/roeder/dotnet/">Reflector</a>, you'll see that the DialogFont property just does some simple platform-detection, and then just hardcodes Tahoma. This worked when it was just 2000/XP vs. 9x, but Vista throws it for a total loop. Fortunately the fix is easy - use <code>SystemFonts.MessageBoxFont</code> instead. This one seems to always return the correct default dialog font. </p>
<p>However, I ran into one more problem. If I set the default font on the Form, like the code above does, I get weird, bloated controls:</p>
<p class="blogimage"><img src='http://brh.numbera.com/blog/wp-content/uploads/2007/04/segoe_bad.png' alt='Setting the default font on the form screws up controls' /></p>
<p>Fortunately I've got a solution for that one too. Instead of setting the font on the Form and letting it inherit, just loop through the <code>Controls</code> property, and individually set the right font on each control:</p>
<div class="igBar"><span id="lcsharp-4"><a href="#" onclick="javascript:showPlainTxt('csharp-4'); return false;">PLAIN TEXT</a></span></div>
<div class="syntax_hilite"><span class="langName">C#:</span>
<div id="csharp-4">
<div class="csharp">
<ol>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #008080; font-style: italic;">// Set the default dialog font on each child control</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span>Control c <span style="color: #0600FF;">in</span> Controls<span style="color: #000000;">&#41;</span></div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#123;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp; &nbsp; c.<span style="color: #0000FF;">Font</span> = SystemFonts.<span style="color: #0000FF;">MessageBoxFont</span>;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000;">&#125;</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div>
</li>
<li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;color:#3A6A8B;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #008080; font-style: italic;">// Use a larger, bold version of the default dialog font for one control</span></div>
</li>
<li style="font-weight: bold;color:#26536A;">
<div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">label1</span>.<span style="color: #0000FF;">Font</span> = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Font<span style="color: #000000;">&#40;</span>SystemFonts.<span style="color: #0000FF;">MessageBoxFont</span>.<span style="color: #0000FF;">Name</span>, 12f, FontStyle.<span style="color: #0000FF;">Bold</span>, GraphicsUnit.<span style="color: #0000FF;">Point</span><span style="color: #000000;">&#41;</span>; </div>
</li>
</ol>
</div>
</div>
</div>
<p></p>
<p>Now I get a more familiar-looking dialog:</p>
<p class="blogimage"><img src='http://brh.numbera.com/blog/wp-content/uploads/2007/04/segoe_good.png' alt='Setting the default font on each control looks fine' /></p>
<p>I could always make a subclass of Form to do this for me, but I'm OK with copying it into each new form. With this code, all my controls come up with the pretty new Segoe UI font in Windows Vista, and Tahoma in XP. </p>
]]></content:encoded>
			<wfw:commentRss>http://benhollis.net/blog/2007/04/11/setting-the-correct-default-font-in-net-windows-forms-apps/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>
