<?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"
	>

<channel>
	<title>Raj Prasad</title>
	<atom:link href="http://blog.rajprasad.net/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.rajprasad.net</link>
	<description>American. Indian. Law Student. Programmer. Pizza Maker.</description>
	<pubDate>Sat, 08 Nov 2008 04:25:06 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6</generator>
	<language>en</language>
			<item>
		<title>Setting Up Mono and mod_mono in Plesk 8.6</title>
		<link>http://blog.rajprasad.net/2008/08/setting-up-mono-and-mod_mono-in-plesk-86/</link>
		<comments>http://blog.rajprasad.net/2008/08/setting-up-mono-and-mod_mono-in-plesk-86/#comments</comments>
		<pubDate>Thu, 07 Aug 2008 22:38:26 +0000</pubDate>
		<dc:creator>Raj</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[apache]]></category>

		<category><![CDATA[asp.net]]></category>

		<category><![CDATA[c#]]></category>

		<category><![CDATA[centos]]></category>

		<category><![CDATA[mono]]></category>

		<category><![CDATA[plesk]]></category>

		<category><![CDATA[yum]]></category>

		<guid isPermaLink="false">http://blog.rajprasad.net/?p=106</guid>
		<description><![CDATA[Mono is the the open-source version of the Microsoft&#8217;s .NET runtime. Mono runs on Linux, Unix, BSD, Solaris, and Windows. Mod_mono and XSP are Mono components that bring ASP.NET functionality to non-Microsoft servers.  For an example of an ASP.NET page running, in Mono, check out Hello World, 2!
Getting Mono up and running in a Plesk [...]]]></description>
			<content:encoded><![CDATA[<p>Mono is the the open-source version of the Microsoft&#8217;s .NET runtime. Mono runs on Linux, Unix, BSD, Solaris, and Windows. Mod_mono and XSP are Mono components that bring ASP.NET functionality to non-Microsoft servers.  For an example of an ASP.NET page running, in Mono, check out <a href="http://mono.rajprasad.net/helloworld2.aspx">Hello World, 2!</a></p>
<p>Getting Mono up and running in a Plesk system is suprisingly easy. I use CentOS, which is a Fedora-based system. Consequently, these instructions are geared toward CentOS Plesk systems. However, with the exception of steps 1 and 2, these instructions should work for any Linux-based Plesk system.</p>
<p><strong>0) SSH into your server.</strong></p>
<p><strong>1) Add the <a href="http://ftp.novell.com/pub/mono/download-stable/rhel-4-i386/mono.repo">Mono repository</a> to your yum repositories.</strong></p>
<p>In CentOS, yum stores a list of its repositories in /etc/yum.repos.d, so adding the Mono repository is as easy as downloading the mono.repo file to this directory.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw3">cd</span> /etc/yum.repos.d/<br />
<span class="kw2">wget</span> http://<span class="kw2">ftp</span>.novell.com/pub/mono/download-stable/rhel<span class="nu0">-4</span>-i386/mono.repo</div>
<p><strong>2) Install Mono, XSP, and mod_mono.</strong></p>
<div class="dean_ch" style="white-space: wrap;">yum <span class="kw2">install</span> mono<br />
yum <span class="kw2">install</span> xsp<br />
yum <span class="kw2">install</span> mod_mono</div>
<p><strong>3) Configure Apache to use mod_mono.</strong></p>
<p>Switch to the httpd customized configuration directory. In most Plesk systems, this should be located at /etc/httpd/conf.d.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw3">cd</span> /etc/httpd/conf.d<br />
vi mod_mono.conf</div>
<p>In vi, or your favorite command line editor, paste the following:</p>
<div class="dean_ch" style="white-space: wrap;">&lt;IfModule !mod_mono.c&gt;<br />
LoadModule mono_module modules/mod_mono.so</p>
<p>AddType application/x-asp-net .aspx<br />
AddType application/x-asp-net .asmx<br />
AddType application/x-asp-net .ashx<br />
AddType application/x-asp-net .asax<br />
AddType application/x-asp-net .ascx<br />
AddType application/x-asp-net .soap<br />
AddType application/x-asp-net .rem<br />
AddType application/x-asp-net .axd<br />
AddType application/x-asp-net .cs<br />
AddType application/x-asp-net .config<br />
AddType application/x-asp-net .Config<br />
AddType application/x-asp-net .dll<br />
DirectoryIndex index.aspx<br />
DirectoryIndex Default.aspx<br />
DirectoryIndex default.aspx<br />
&lt;/IfModule&gt;</div>
<p>Save the file.</p>
<p><strong>4) Restart Apache</strong></p>
<p>From your Plesk control panel, go to the Server&gt;Services page. Restart the Web Server (Apache). You can also restart Apache from the command line.</p>
<div class="dean_ch" style="white-space: wrap;">/usr/<span class="kw3">local</span>/psa/admin/sbin/websrvmng -a<br />
/usr/<span class="kw3">local</span>/psa/admin/sbin/websrvmng &#8211;restart</div>
<p><strong>5) Wait a few minutes.</strong></p>
<p>Plesk doesn&#8217;t restart immediatly, despite what the Plesk control panel (or the command prompt) may tell you.</p>
<p><strong>6) Create a test ASP.Net page</strong></p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw2">&lt;%</span>@ Page Language=<span class="st0">&quot;C#&quot;</span> <span class="kw2">%&gt;</span><br />
&lt;html&gt;<br />
&lt;script language=&quot;C#&quot; runat=&quot;server&quot;&gt;<br />
void Page_Load(object sender, EventArgs e)<br />
{<br />
&nbsp; &nbsp; Response.Write(&quot;&lt;br&gt;Hello World! (Response.Write)&lt;br&gt;&quot;);<br />
&nbsp; &nbsp; lbHelloWorld.Text = &quot;Hello World! (HTML element changed.)&quot;;<br />
}<br />
&lt;/script&gt;<br />
&lt;body&gt;<br />
&nbsp; &nbsp; &lt;asp:label id=&quot;lbHelloWorld&quot; runat=&quot;server&quot; /&gt;<br />
&lt;/body&gt;<br />
&lt;/html&gt;</div>
<p><strong>7) That&#8217;s it!</strong></p>
<ul>Some caveats:</p>
<li>The default configuration for mod_mono uses the Mono binaries compatible with .Net 1.1. These binaries don&#8217;t support VB.</li>
<li>Mod_mono is slow. It does not yet support pre-compiled binaries.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://blog.rajprasad.net/2008/08/setting-up-mono-and-mod_mono-in-plesk-86/feed/</wfw:commentRss>
		</item>
		<item>
		<title>XNA 2D Camera Tutorial</title>
		<link>http://blog.rajprasad.net/2008/07/2d-camera-in-xna/</link>
		<comments>http://blog.rajprasad.net/2008/07/2d-camera-in-xna/#comments</comments>
		<pubDate>Thu, 31 Jul 2008 02:31:16 +0000</pubDate>
		<dc:creator>Raj</dc:creator>
		
		<category><![CDATA[xna]]></category>

		<category><![CDATA[2d]]></category>

		<category><![CDATA[c#]]></category>

		<category><![CDATA[game programming]]></category>

		<category><![CDATA[graphics]]></category>

		<category><![CDATA[programming]]></category>

		<category><![CDATA[sample code]]></category>

		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.rajprasad.net/?p=8</guid>
		<description><![CDATA[I&#8217;ve just posted a tutorial on creating a 2D camera for XNA. The XNA2dCamera class provides a clean, simple interface to camera functionality in 2-D games. It handles translation, rotation, and scaling/zoom with ease. Most importantly, the class is graphics-engine agnostic, so it can be used for platformer engines, puzzle game engines, and isometric engines.
The [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve just posted a tutorial on creating a 2D camera for XNA. The XNA2dCamera class provides a clean, simple interface to camera functionality in 2-D games. It handles translation, rotation, and scaling/zoom with ease. Most importantly, the class is graphics-engine agnostic, so it can be used for platformer engines, puzzle game engines, and isometric engines.</p>
<p>The full contents of the XNA2dCamera class are on the last page of the tutorial.</p>
<p><a title="Read the XNA 2D Camera Tutorial" href="/tutorials/2d-camera-in-xna/">Read the XNA 2D Camera tutorial</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.rajprasad.net/2008/07/2d-camera-in-xna/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Creating a FontCombBox for the ToolStrip in C#</title>
		<link>http://blog.rajprasad.net/2008/07/creating-a-fontcombobox-in-csharp/</link>
		<comments>http://blog.rajprasad.net/2008/07/creating-a-fontcombobox-in-csharp/#comments</comments>
		<pubDate>Tue, 22 Jul 2008 21:05:23 +0000</pubDate>
		<dc:creator>Raj</dc:creator>
		
		<category><![CDATA[c#]]></category>

		<category><![CDATA[code]]></category>

		<category><![CDATA[custom control]]></category>

		<category><![CDATA[programming]]></category>

		<category><![CDATA[sample code]]></category>

		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://rajprasad.net/?p=1</guid>
		<description><![CDATA[Word&#8217;s font selection combobox displays a preview of each font. It&#8217;s a great feature which makes it much easier for a user to select a font. Instead of going through each font one-by-one to see what the font looks like, the user can see the results for all installed fonts in one fell swoop.
Accomplishing this [...]]]></description>
			<content:encoded><![CDATA[<p>Word&#8217;s font selection combobox displays a preview of each font. It&#8217;s a great feature which makes it much easier for a user to select a font. Instead of going through each font one-by-one to see what the font looks like, the user can see the results for all installed fonts in one fell swoop.</p>
<p>Accomplishing this feat using C# in .NET 1.0 and 1.1 was no easy feat. The length of many older code samples easily reached into the hundreds of lines. .NET 2.0 introduced new controls and features that made the job much easier.</p>
<p>The C# .NET 2.0 solution only requires one class and less than 40 lines of code. All we need to do is handle several of the events thrown by the ToolStripComboBox&#8217;s underlying ComboBox control. My solution extends the ToolStripComboBox, but you can accomplish the same thing without extending the control.<br />
<span id="more-1"></span></p>
<h2><span style="text-decoration: underline;">Extending the ToolStripComboBox</span></h2>
<div class="dean_ch" style="white-space: wrap;"><span class="br0">&#91;</span><a href="http://www.google.com/search?q=System.ComponentModel&#038;as_sitesearch=msdn2.microsoft.com%2Fen-us%2F"><span class="kw5">System.<span class="me1">ComponentModel</span></span></a>.<span class="me1">DesignerCategory</span> <span class="br0">&#40;</span> <span class="st0">&quot;code&quot;</span> <span class="br0">&#41;</span><span class="br0">&#93;</span><br />
<span class="br0">&#91;</span>ToolStripItemDesignerAvailability <span class="br0">&#40;</span> ToolStripItemDesignerAvailability.<span class="me1">All</span> <span class="br0">&#41;</span><span class="br0">&#93;</span><br />
<span class="kw1">public</span> <span class="kw4">class</span> ToolStripFontComboBox : ToolStripComboBox<br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">protected</span> <span class="kw4">int</span> iDropWidth = <span class="nu0">0</span>;</p>
<p><span class="co1">// . . .</span></div>
<p>[System.ComponentModel.DesignerCategory ( "code" )]<br />
This line instructs the IDE to restrict this class to the code view. Without this line, double-clicking the file from the Solution Explorer brings up the Designer view, which is completely unnecessary for this project.</p>
<p>[ToolStripItemDesignerAvailability ( ToolStripItemDesignerAvailability.All )]<br />
This line instructs the IDE to show the ToolStripFontComboBox control in the Designer when editing ToolStrips. using ToolStripItemDesignerAvailability.All results in our control showing up in all Toolstrip-based controls, including DropDowns and ContextMenus.</p>
<p>iDropWidth stores the width of the largest item. It&#8217;s value will be calculated in the MeasureItem event handler.</p>
<h2><span style="text-decoration: underline;">Constructor</span></h2>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">public</span> ToolStripFontComboBox <span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="co1">// Set the draw mode so we can take over item drawing</span><br />
&nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">ComboBox</span>.<span class="me1">DrawMode</span> = DrawMode.<span class="me1">OwnerDrawVariable</span>;<br />
&nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">AutoCompleteMode</span> = AutoCompleteMode.<span class="me1">Append</span>;</p>
<p>&nbsp; &nbsp; <span class="co1">// Handle the events</span><br />
&nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">ComboBox</span>.<span class="me1">DropDown</span> += <span class="kw3">new</span> EventHandler <span class="br0">&#40;</span> DropDown <span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">ComboBox</span>.<span class="me1">MeasureItem</span> += <span class="kw3">new</span> MeasureItemEventHandler <span class="br0">&#40;</span> MeasureItem <span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">ComboBox</span>.<span class="me1">DrawItem</span> += <span class="kw3">new</span> DrawItemEventHandler <span class="br0">&#40;</span> DrawItem <span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; <span class="co1">// Create the list of fonts, and populate the ComboBox with that list</span><br />
&nbsp; &nbsp; PopulateFonts<span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></div>
<p>We need to to set the underlying ComboBox&#8217;s DrawMode to DrawMode.OwnerDrawVariable or DrawMode.OwnerDrawFixed. OwnerDrawVariable means that each item can have its own, variable size. OwnerDrawFixed means that each item will have the same, fixed size.</p>
<p>Setting the DrawMode to one of the OwnerDraw&#8230; options allows us to handle the the underlying ComboBox&#8217;s item drawing events. It&#8217;s also possible to just use the default DrawMode, but then we&#8217;d have to override the underlying ComboBox&#8217;s protected OnPaint event. This isn&#8217;t possible with a ToolStripComboBox, because the control inherits from the ToolStripItem class instead of the ComboBox class. Overriding the OnPaint event is also a masochistic endeavor best left to C++ programmers and other self-flaggelators.</p>
<p>We&#8217;re only going to handle the DropDown, MeasureItem, and DrawItem events thrown by the underlying ComboBox. Those event handlers will be defined in our class.</p>
<p>Finally, we call the PopulateFonts() method, which will search for installed fonts and then populate the ComboBox with the names of those fonts.</p>
<h2><span style="text-decoration: underline;">Handling the Events</span></h2>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">protected</span> <span class="kw1">void</span> DropDown <span class="br0">&#40;</span> <span class="kw4">object</span> sender, EventArgs e <span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">ComboBox</span>.<span class="me1">DropDownWidth</span> = iDropWidth;<br />
<span class="br0">&#125;</span></div>
<p>A ComboBox throws the DropDown event when the DropDown portion is displayed. In this event, we set the width of the DropDown. We&#8217;re doing this here to make sure the width of the DropDown is properly set when we know we&#8217;re going to use it. Setting this property here allows us to account for changes to the ComboBox&#8217;s Items collection, and makes the control more usable because we don&#8217;t have to unnecessarily restrict access to properties that would be exposed by a normal ToolStripComboBox.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">protected</span> <span class="kw1">void</span> MeasureItem <span class="br0">&#40;</span> <span class="kw4">object</span> sender, MeasureItemEventArgs e <span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span> e.<span class="me1">Index</span> &gt; <span class="nu0">-1</span> <span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">string</span> szFont = Items<span class="br0">&#91;</span>e.<span class="me1">Index</span><span class="br0">&#93;</span>.<span class="me1">ToString</span> <span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; Graphics g = <span class="kw1">this</span>.<span class="me1">ComboBox</span>.<span class="me1">CreateGraphics</span> <span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; SizeF size = g.<span class="me1">MeasureString</span> <span class="br0">&#40;</span> szFont, <span class="kw3">new</span> Font <span class="br0">&#40;</span> szFont, <span class="kw1">this</span>.<span class="me1">Font</span>.<span class="me1">Size</span> <span class="br0">&#41;</span> <span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// Set the Item&#8217;s Width, and iDropWidth if the item has a greater width</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; e.<span class="me1">ItemWidth</span>= <span class="br0">&#40;</span><span class="kw4">int</span><span class="br0">&#41;</span>size.<span class="me1">Width</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span>e.<span class="me1">ItemWidth</span> &gt; iDropWidth<span class="br0">&#41;</span> <span class="br0">&#123;</span> iDropWidth = e.<span class="me1">ItemWidth</span>; <span class="br0">&#125;</span></p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="co1">// If .NET gives you problems drawing fonts with different heights, set a maximum height</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; e.<span class="me1">ItemHeight</span> = <span class="br0">&#40;</span><span class="kw4">int</span><span class="br0">&#41;</span>size.<span class="me1">Height</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span> e.<span class="me1">ItemHeight</span> &gt; <span class="nu0">20</span> <span class="br0">&#41;</span> <span class="br0">&#123;</span> e.<span class="me1">ItemHeight</span> = <span class="nu0">20</span>; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
<span class="br0">&#125;</span></div>
<p>A ComboBox throws the MeasureItem event whenever it needs to measure the size of an item for drawing. In the code above, the ComboBox has already been populated with fonts. Each item contains the name of the font. The MeasureItem event occurs before drawing, so the event does not pass us a Graphics object. We need to create our own. We then use the Graphics object to measure the size of a string, containing the name of a font, rendered using that same font. After we&#8217;ve retrieved the size, we run a few checks just to make sure everything is kosher. We also set the iDropWidth variable to the size of the widest item.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">protected</span> <span class="kw1">void</span> DrawItem <span class="br0">&#40;</span> <span class="kw4">object</span> sender, DrawItemEventArgs e <span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="co1">// DrawBackground handles drawing the background (i.e,. hot-tracked v. not)</span><br />
&nbsp; &nbsp; <span class="co1">// It uses the system colors (Bluish, and and white, by default)</span><br />
&nbsp; &nbsp; <span class="co1">// same as calling e.Graphics.FillRectangle ( SystemBrushes.Highlight, e.Bounds );</span><br />
&nbsp; &nbsp; e.<span class="me1">DrawBackground</span> <span class="br0">&#40;</span><span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span> e.<span class="me1">Index</span> &gt; <span class="nu0">-1</span> <span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw4">string</span> szFont = Items<span class="br0">&#91;</span>e.<span class="me1">Index</span><span class="br0">&#93;</span>.<span class="me1">ToString</span> <span class="br0">&#40;</span><span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; Font fFont = <span class="kw3">new</span> Font <span class="br0">&#40;</span> szFont, <span class="kw1">this</span>.<span class="me1">Font</span>.<span class="me1">Size</span> <span class="br0">&#41;</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; Rectangle rectDraw = e.<span class="me1">Bounds</span>;</p>
<p>&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span> <span class="br0">&#40;</span> e.<span class="me1">State</span> &amp;&amp; DrawItemState.<span class="kw1">Default</span> <span class="br0">&#41;</span> == e.<span class="me1">State</span> <span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.<span class="me1">Graphics</span>.<span class="me1">DrawString</span> <span class="br0">&#40;</span> szFont, fFont, SystemBrushes.<span class="me1">WindowText</span> <span class="br0">&#41;</span>, rectDraw <span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.<span class="me1">Graphics</span>.<span class="me1">DrawString</span> <span class="br0">&#40;</span> szFont, fFont, SystemBrushes.<span class="me1">HighlightText</span> <span class="br0">&#41;</span>, rectDraw <span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="co1">// Uncomment this if you actually like the way the focus rectangle looks</span><br />
&nbsp; &nbsp; <span class="co1">//e.DrawFocusRectangle ();</span><br />
<span class="br0">&#125;</span></div>
<p>DrawItem() is the the meat, cheese, and potatoes of our project. It handles all the work of drawing our fonts names in their respective fonts. This version draws the background and the font string, but it does not draw the focus rectangle. I don&#8217;t like the way the focus rectangle looks, but if you do, just uncomment the function call.</p>
<p>e.DrawBackground() draws the background color for the ComboBox using the System.Highlight color. In the default Windows themes, this color is a shade of blue, but some themes change that color. This function call only saves us a line of code, since it&#8217;s a trivial matter to draw a rectangle. (See the comment in the code.)</p>
<p>First, we retrieve the font&#8217;s name from the ComboBox (each item is the name of an installed font) and create the font object. We&#8217;ll be using the font object to draw the font&#8217;s name. We then get the bounding rectangle for drawing.</p>
<p>The color we&#8217;ll use to draw the text depends on the item&#8217;s state. We use the SystemColors.WindowText color to draw any item that isn&#8217;t currently selected or hot-tracked by the mouse. Hot-tracked items are drawn using the SystemColors.HighlightText color.</p>
<p>We determine the item&#8217;s state by checking e.State and comparing it to the DrawItemState enumeration. DrawItemState enumerates many possible values, but as as configured in our extended control, the ToolStripComboBox will only send 3 types of states: e.Default, e.HotLight, and e.Selected. For our purposes, it only matters whether the item is in its default state or not, so we&#8217;ll only test for the default state. Each item can have several states at once (for example, an item can be selected and hot-tracked at the same time), so we can&#8217;t just test for equality. Instead, we have to use a bit comparison to test only the state we want. To do this, we bitwise AND the DrawItemState item with the e.State we were passed, and then compare the result with the state we&#8217;re checking for.</p>
<p>Finally, we draw the font string. We don&#8217;t need to create our own Graphics object to do the drawing because the DrawItemEventArgs object passed as e includes the Graphics object the ComboBox is using to draw the items.</p>
<h2><span style="text-decoration: underline;">Generating the List of Fonts</span></h2>
<p>For the purposes of simplicity, this tutorial only illustrates the .NET way to search for installed fonts. I might get around to posting the Windows API way to do it later. The benefit of the .NET way is simplicity. The Windows API way offers more control. For example, it&#8217;s (relatively) easy to select just Western fonts through the API, but doing this in .NETisn&#8217;t possible, as far as I know.</p>
<div class="dean_ch" style="white-space: wrap;"><span class="kw1">public</span> <span class="kw1">void</span> PopulateFonts <span class="br0">&#40;</span><span class="br0">&#41;</span><br />
<span class="br0">&#123;</span><br />
&nbsp; &nbsp; <span class="kw1">foreach</span> <span class="br0">&#40;</span> FontFamily ff <span class="kw1">in</span> FontFamily.<span class="me1">Families</span> <span class="br0">&#41;</span><br />
&nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span> ff.<span class="me1">IsStyleAvailable</span> <span class="br0">&#40;</span> FontStyle.<span class="me1">Regular</span> <span class="br0">&#41;</span> <span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">Items</span>.<span class="me1">Add</span> <span class="br0">&#40;</span> ff.<span class="me1">Name</span> <span class="br0">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="br0">&#125;</span><br />
&nbsp; &nbsp; <span class="kw1">if</span> <span class="br0">&#40;</span> Items.<span class="me1">Count</span> &gt; <span class="nu0">0</span> <span class="br0">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span class="kw1">this</span>.<span class="me1">SelectedIndex</span> = <span class="kw1">this</span>.<span class="me1">FindString</span> <span class="br0">&#40;</span> <span class="st0">&quot;Times&quot;</span> <span class="br0">&#41;</span>;<br />
<span class="br0">&#125;</span></div>
<p>The FontFamily class takes care of all the work for us. It contains a list of all the fonts installed on the system in the FontFamily.Members field. In case you&#8217;re wondering, in Windows terminology, font families are what we commonly refer to as fonts. The font family name is the font name we commonly associate with fonts. Font, in this context, actually refers to the specific font/font-style/font-size combination. For example, &#8220;Times New Roman&#8221; is a font family, while &#8220;Times New Roman&#8221; in bold and size 12pt is a font.</p>
<p>The code above only checks if each font family has the regular font style available. However, for most document editing applications, it&#8217;s a good idea to also check for the Bold and Italic font styles, and only add font families which also include those styles. Windows can emulate the underline style on if a font family doesn&#8217;t include its own underline style.</p>
<h2><span style="text-decoration: underline;">Closing Remarks</span></h2>
<p>To use this code, just grab it and finish up the class definition by adding a closing bracket. This class uses the System, System.Drawing, and System.Windows.Forms namespaces.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.rajprasad.net/2008/07/creating-a-fontcombobox-in-csharp/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
