<?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>Software Development &#187; RicD</title>
	<atom:link href="http://eclectrics.com/software/author/ricd/feed/" rel="self" type="application/rss+xml" />
	<link>http://eclectrics.com/software</link>
	<description></description>
	<lastBuildDate>Mon, 02 Jan 2012 17:55:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>When Web Services Go Bad</title>
		<link>http://eclectrics.com/software/2012/01/when-web-services-go-bad/</link>
		<comments>http://eclectrics.com/software/2012/01/when-web-services-go-bad/#comments</comments>
		<pubDate>Mon, 02 Jan 2012 17:27:55 +0000</pubDate>
		<dc:creator>RicD</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://eclectrics.com/software/2012/01/when-web-services-go-bad/</guid>
		<description><![CDATA[A few years ago I published a number of articles around using the National Weather Service NDFD web service to get current weather conditions and forecasts.&#160; Unfortunately a few months ago quite a few folks started mentioning the example applications had stopped working.&#160; I finally got time to look at the problem and have updated [...]]]></description>
			<content:encoded><![CDATA[<p>A few years ago I published a number of articles around using the National Weather Service NDFD web service to get current weather conditions and forecasts.&#160; Unfortunately a few months ago quite a few folks started mentioning the example applications had stopped working.&#160; I finally got time to look at the problem and have updated both the original articles and download examples.&#160; Just to tidy things up – and so folks won’t have to wait next time, I thought I would describe how I got everything working again.&#160; I’ll walk through the simplest example (the one from the “<a href="http://eclectrics.com/software/2009/08/consuming-ndfd-web-services-from-a-c-client/">Consuming NDFD Web Services from a C# Client</a>” post, but the debugging process and changes apply to all the examples that access the NDFD web service.</p>
<p><a href="http://eclectrics.com/software/wp-content/uploads/2012/01/InvalidZipCode.png"><img style="background-image: none; border-right-width: 0px; margin: 7px 10px 0px 0px; padding-left: 0px; padding-right: 0px; display: inline; float: left; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="InvalidZipCode" border="0" alt="InvalidZipCode" align="left" src="http://eclectrics.com/software/wp-content/uploads/2012/01/InvalidZipCode_thumb.png" width="210" height="173" /></a></p>
<p>&#160;</p>
<p>1.&#160; First I ran the original NdfdTest-Application example from the post.&#160; Yes, things weren’t looking so good. </p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>&#160;</p>
<p>2. So I downloaded and built the original project – and there were errors:</p>
<p><a href="http://eclectrics.com/software/wp-content/uploads/2012/01/badbuild.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="badbuild" border="0" alt="badbuild" src="http://eclectrics.com/software/wp-content/uploads/2012/01/badbuild_thumb.png" width="665" height="252" /></a></p>
<p>Clearly the service reference for the NDFD web service was not getting created.</p>
<p>&#160;</p>
<p>3. Visual Studio can update service references if you ask so I right-clicked on the NdfdServiceReference and selected “Update Service Reference” from the shortcut menu and built the project again:</p>
<p><a href="http://eclectrics.com/software/wp-content/uploads/2012/01/badcomplile2.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="badcomplile2" border="0" alt="badcomplile2" src="http://eclectrics.com/software/wp-content/uploads/2012/01/badcomplile2_thumb.png" width="679" height="147" /></a></p>
<p>Ah ha!&#160; [<strong>Change 1</strong>] They changed the signature of the method used to get forecasts!&#160; To see what’s happened, double click on the NdfdServiceReference from the Solution Explorer in Visual Studio then expand the NdfdTest.NdfdServiceReference node in the Object Browser and click on the ndfdXMLPortType interface.&#160; It turns out they changed the signatures for all the methods, adding the ability to specify whether you want results in English (unitType.e) or Metric (unitType.m) units.&#160; So I modified the NDFDgetByDay function, adding a unitType.e parameter and ran the application – and we are still getting an Invalid Zip Code error.&#160; But now you can see in the output window we are getting a protocol exception:</p>
<p><a href="http://eclectrics.com/software/wp-content/uploads/2012/01/exception.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="exception" border="0" alt="exception" src="http://eclectrics.com/software/wp-content/uploads/2012/01/exception_thumb.png" width="694" height="67" /></a></p>
<p>&#160;</p>
<p>4. This is good information, but not enough.&#160; So in Visual Studio I enabled breaking on any thrown Common Language Runtime Exception.&#160; I ran the application again, entered a zip code and bang!</p>
<p><a href="http://eclectrics.com/software/wp-content/uploads/2012/01/protocolexception.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="protocolexception" border="0" alt="protocolexception" src="http://eclectrics.com/software/wp-content/uploads/2012/01/protocolexception_thumb.png" width="701" height="152" /></a></p>
<p>What the heck is going on?&#160; We asked for for text/xml and we are getting text/html. </p>
<p>[<strong>Change 2</strong>] Somebody moved our web service – now it’s a web page instead!</p>
<p><a href="http://eclectrics.com/software/wp-content/uploads/2012/01/webpage.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="webpage" border="0" alt="webpage" src="http://eclectrics.com/software/wp-content/uploads/2012/01/webpage_thumb.png" width="377" height="473" /></a></p>
<p>&#160;</p>
<p>5. I admit at this point, as soon as I updated the service reference, I should have gone to the app.config file and looked at what was generated for me.&#160; Well, never too late…&#160; Low and behold, there is a new address for the NDFD web service:</p>
<p><a href="http://eclectrics.com/software/wp-content/uploads/2012/01/address.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="address" border="0" alt="address" src="http://eclectrics.com/software/wp-content/uploads/2012/01/address_thumb.png" width="619" height="137" /></a></p>
<p>When I wrote this series of posts, the NDFD web service was at <a href="http://www.weather.gov">http://www.weather.gov</a>.&#160; Now it’s at <a href="http://graphical.weather.gov">http://graphical.weather.gov</a> instead.</p>
<p>6. So finally, replace the address on our original ndfdXMLPort endpoint with the address from the newly generated ndfdXMLPort1 endpoint.&#160; Everything is working again.</p>
]]></content:encoded>
			<wfw:commentRss>http://eclectrics.com/software/2012/01/when-web-services-go-bad/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Getting the Current Weather Conditions</title>
		<link>http://eclectrics.com/software/2009/09/getting-the-current-weather-conditions/</link>
		<comments>http://eclectrics.com/software/2009/09/getting-the-current-weather-conditions/#comments</comments>
		<pubDate>Mon, 14 Sep 2009 17:17:47 +0000</pubDate>
		<dc:creator>RicD</dc:creator>
				<category><![CDATA[Projects]]></category>
		<category><![CDATA[BackgroundWorker]]></category>
		<category><![CDATA[Current weather conditions]]></category>
		<category><![CDATA[IsolatedStorage]]></category>
		<category><![CDATA[NDFD]]></category>

		<guid isPermaLink="false">http://eclectrics.com/software/?p=170</guid>
		<description><![CDATA[[January 2, 2012 Update: A number of changes have been made to the NDFD web service since I originally posted this article. Both the URL and the methods exposed by the service have changed. The text below and downloads have been updated to reflect these changes.] Unlike weather forecasts which are available for any point [...]]]></description>
			<content:encoded><![CDATA[<p><i>[January 2, 2012 Update: A number of changes have been made to the NDFD web service since I originally posted this article.  Both the URL and the methods exposed by the service have changed.  The text below and downloads have been updated to reflect these changes.]</i></p>
<p>Unlike weather forecasts which are available for any point in the US, current conditions are only available for specific spots where there are reporting stations.  The good news is that there are more than 2000 reporting stations, so it&#8217;s not too hard to find one close to you.  In this installment I am going to add the ability to get the current weather conditions to our library of weather tools. (If you are following along in this series, you will notice I have done a bit of mild refactoring, getting ready for the next steps – a local Windows &#8220;weather&#8221; service and a real desktop UI).
</p>
<p>As usual, the folks at the National Weather Service have made our job pretty easy.  You can take a look at what they&#8217;ve given us to work with <a href="http://www.weather.gov/xml/current_obs/">here</a>.  Each observation stations get its own web page containing an Xml document (updated hourly) describing the current weather conditions.  The URL for the page containing the current conditions for each weather station is formed by appending the station&#8217;s identification string to the base URL for current observations.  So for example to get the current conditions for Smithfield, Rhode Island the URL would look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">http://www.weather.gov/xml/current_obs/KSFZ.xml</pre></div></div>

<p>So now the puzzle comes down to how do we find the ID for the reporting station closest to the spot for which we want the current conditions?  We&#8217;ve got a few choices, but all of them start with this <a href="http://www.weather.gov/xml/current_obs/index.xml">document</a>, the index of reporting stations.  It contains an entry for every reporting station, formatted just like this excerpt for our Smithfield station:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;station<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;station_id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>KSFZ<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/station_id<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> 
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;state<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>RI<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/state<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> 
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;station_name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Smithfield<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/station_name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> 
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;latitude<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>41.91<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/latitude<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> 
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;longitude<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>-71.4<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/longitude<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> 
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;html_url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>http://weather.noaa.gov/weather/current/KSFZ.html<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/html_url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> 
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;rss_url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>http://weather.gov/xml/current_obs/KSFZ.rss<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/rss_url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> 
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xml_url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>http://weather.gov/xml/current_obs/KSFZ.xml<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xml_url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span> 
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/station<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
 </pre></div></div>

<p>The key pieces here are the <span style="color:#365f91; font-family:Courier New; font-size:9pt">station_name</span>, <span style="color:#365f91; font-family:Courier New; font-size:9pt">state</span> and <span style="color:#365f91; font-family:Courier New; font-size:9pt">station_id</span>.  We&#8217;re also going to find the <span style="color:#365f91; font-family:Courier New; font-size:9pt">latitude</span> and <span style="color:#365f91; font-family:Courier New; font-size:9pt">longitude</span> useful a bit later.  Since this is a reasonably large document (900kb) and we want to play really nice with the NWS folks so they&#8217;ll let us continue to use their toys (and we want our app to be responsive), we&#8217;ll download it once (using a BackgroundWorker) and cache a copy in IsolatedStorage (along with an option to refresh our cache when needed).
</p>
<p>So armed with the list of all the reporting stations, there are a couple of routes we could take.  On the one hand we could simply mimic what the NWS does on their current conditions web page:
</p>
<ol>
<li>Show the user a list of all the states and ask them to pick one.
</li>
<li>Based on their selection, show them a list of all the observation stations in that state and ask them to pick one.
</li>
<li>From their selection, compose the URL for the current conditions page and submit a web request.
</li>
</ol>
<p>The TestFromStateStation sample application (available for download at the end of this post) implements this approach.
</p>
<p>The other approach is to:
</p>
<ol>
<li>Ask the user for the Zip Code for their location.
</li>
<li>Query the NDFD web service for a location (latitude, longitude) associated with the zip code.
</li>
<li>Compute the distance between the zip location and the location of each observation station in the reporting stations index (see above) to find the closest station.
</li>
<li>Use the station Id from the closest station to compose the URL for the current conditions page and submit a web request.
</li>
</ol>
<p>This technique relies on being able to determine the distance between two points on the earth&#8217;s surface. There are a number of techniques we can use – you can read a really good description of your options thanks to the folks at <a href="http://www.movable-type.co.uk/scripts/latlong.html">movable-type</a>.  For our purposes, the spherical law of cosines is good enough (see ObservationStations.GetDistance() in the NationalWeatherServiceLibrary project in the attached solution).  The TestFromZip sample (see below) implements this approach.
</p>
<p>So there you have it, the ability to get the current weather conditions using either state/station or zip code.  As promised, here are the downloads: <a href="http://eclectrics.com/software/wp-content/uploads/2012/01/TestFromStateStationUpdate.zip">TestFromStateStation.exe</a>, <a href="http://eclectrics.com/software/wp-content/uploads/2012/01/TestFromZipUpdate.zip">TestFromZip.exe</a> and the <a href="http://eclectrics.com/software/wp-content/uploads/2012/01/WeatherTools2Update.zip">complete VS2010 solution</a>.
</p>
<p>At this point we&#8217;re done with the web service pieces of the project; we know how to get forecasts and current conditions for anywhere in the US.  Next it&#8217;s time to create the UI.
</p>
<p>
 </p>
]]></content:encoded>
			<wfw:commentRss>http://eclectrics.com/software/2009/09/getting-the-current-weather-conditions/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>XmlTreeView – A Custom Painted TreeView</title>
		<link>http://eclectrics.com/software/2009/08/xmltreeview-%e2%80%93-a-custom-painted-treeview/</link>
		<comments>http://eclectrics.com/software/2009/08/xmltreeview-%e2%80%93-a-custom-painted-treeview/#comments</comments>
		<pubDate>Fri, 28 Aug 2009 14:45:39 +0000</pubDate>
		<dc:creator>RicD</dc:creator>
				<category><![CDATA[Projects]]></category>
		<category><![CDATA[Custom Paint TreeView]]></category>
		<category><![CDATA[ISO-8859-1]]></category>
		<category><![CDATA[NDFD]]></category>

		<guid isPermaLink="false">http://eclectrics.com/software/2009/08/xmltreeview-%e2%80%93-a-custom-painted-treeview/</guid>
		<description><![CDATA[[January 2, 2012 Update: A number of changes have been made to the NDFD web service since I originally posted this article. Both the URL and the methods exposed by the service have changed. The text below and downloads have been updated to reflect these changes.] Last time I described how to build a WCF [...]]]></description>
			<content:encoded><![CDATA[<p><i>[January 2, 2012 Update: A number of changes have been made to the NDFD web service since I originally posted this article.  Both the URL and the methods exposed by the service have changed.  The text below and downloads have been updated to reflect these changes.]</i></p>
<p>Last time I described how to build a WCF client for the NDFD web service. The gist was we needed to create a custom <span style="color:#365f91; font-family:Consolas; font-size:9pt">TextMessageEncoder</span> that could handle the ISO-8859-1 encoding used by the service. With that done, it&#8217;s time to start thinking about how to use all the data to which we now have access. My goal over the next week or so is to convert the NDFD client into its own local WCF service, hosted in a Windows service, to add the ability to get current weather conditions, and finally to build a couple of desktop clients (a desktop Gadget and an ActiveX control) that use the service.</p>
<p>But before I jump into that, I&#8217;m going to take a small detour and talk about custom painting a <span style="color:#365f91; font-family:Consolas; font-size:9pt">TreeView</span>. This is motivated by the fact that the NDFD Xml data is a bit hard to visually parse when displayed as raw Xml, so I thought I&#8217;d simplify my debugging a bit by pushing the data into a custom painted <span style="color:#365f91; font-family:Consolas; font-size:9pt">TreeView</span>.</p>
<p>There are a couple approaches to custom painting in a managed <span style="color:#365f91; font-family:Consolas; font-size:9pt">TreeView</span>. The simplest (OwnerDraw) is to set the <span style="color:#365f91; font-family:Consolas; font-size:9pt">TreeView.DrawMode</span> property like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">DrawMode</span> <span style="color: #008000;">=</span> TreeViewDrawMode.<span style="color: #0000FF;">OwnerDrawText</span><span style="color: #008000;">;</span></pre></div></div>

<p>or</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">DrawMode</span> <span style="color: #008000;">=</span> TreeViewDrawMode.<span style="color: #0000FF;">OwnerDrawAll</span><span style="color: #008000;">;</span></pre></div></div>

<p>Then you override <span style="color:#365f91; font-family:Consolas; font-size:9pt">OnDrawNode</span> and do your painting. The big benefit with this approach is that the <span style="color:#365f91; font-family:Consolas; font-size:9pt">TreeView</span> does most of the heavy lifting – it determines when a <span style="color:#365f91; font-family:Consolas; font-size:9pt">TreeNode</span> needs redrawn as well as the bounding rectangle – all you need to do is draw the contents of a node when requested.</p>
<p>The problem with this approach is that as soon as your node content gets a bit complex, you start down a slippery slope of increasing complexity as you add hit test code for selection highlighting etc. And if your drawing code gets the least bit complex, you will start to notice redrawing flicker as you resize windows containing your custom painted <span style="color:#365f91; font-family:Consolas; font-size:9pt">TreeView</span>. (It&#8217;s unfortunate, but the built-in double buffering does not work for <span style="color:#365f91; font-family:Consolas; font-size:9pt">OwnerDrawText</span> or <span style="color:#365f91; font-family:Consolas; font-size:9pt">OwnerDrawAll</span>. You can get some improvement in the flicker by overriding <span style="color:#365f91; font-family:Consolas; font-size:9pt">WndProc</span> and eating all the <span style="color:#365f91; font-family:Consolas; font-size:9pt">WM_ERASEBKGND</span> messages, but it doesn&#8217;t really fix the problem.) So, except for the simplest situations, I usually end up using the second approach which is to do all the painting myself (UserPaint). It&#8217;s a little complex the first time, but most of the complexity is boilerplate code that you can reuse (the attached code is a pretty good starting point).</p>
<p>You enable user painting by setting several of the <span style="color:#365f91; font-family:Consolas; font-size:9pt">ControlStyles</span> bits for your <span style="color:#365f91; font-family:Consolas; font-size:9pt">TreeView</span> like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">SetStyle</span><span style="color: #000000;">&#40;</span>ControlStyles.<span style="color: #0000FF;">UserPaint</span>, <span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">SetStyle</span><span style="color: #000000;">&#40;</span>ControlStyles.<span style="color: #0000FF;">AllPaintingInWmPaint</span>, <span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">SetStyle</span><span style="color: #000000;">&#40;</span>ControlStyles.<span style="color: #0000FF;">OptimizedDoubleBuffer</span>, <span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>Now only <span style="color:#365f91; font-family:Consolas; font-size:9pt">OnPaint</span> and <span style="color:#365f91; font-family:Consolas; font-size:9pt">OnPaintBackground</span> get called (<span style="color:#365f91; font-family:Consolas; font-size:9pt">OnDrawNode</span> is out of the loop), and this is where the fun begins.</p>
<p>For starters, you&#8217;ll want to override both <span style="color:#365f91; font-family:Consolas; font-size:9pt">OnPaint</span> and <span style="color:#365f91; font-family:Consolas; font-size:9pt">OnPaintBackground</span>. For <span style="color:#365f91; font-family:Consolas; font-size:9pt">OnPaintBackground</span>, I find it&#8217;s usually most efficient to do nothing more than insure the base class <span style="color:#365f91; font-family:Consolas; font-size:9pt">OnPaintBackground</span> isn&#8217;t called:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> OnPaintBackground<span style="color: #000000;">&#40;</span>PaintEventArgs pevent<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #008080; font-style: italic;">// We can do a better job of predicting where nodes will paint the background</span>
	<span style="color: #008080; font-style: italic;">// and what area is left, so just disable background painting.</span>
	<span style="color: #008080; font-style: italic;">//base.OnPaintBackground(pevent);</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>This means in <span style="color:#365f91; font-family:Consolas; font-size:9pt">OnPaint</span>, we need to paint not only the <span style="color:#365f91; font-family:Consolas; font-size:9pt">TreeNodes</span> but the empty area where there are no <span style="color:#365f91; font-family:Consolas; font-size:9pt">TreeNodes</span> as well. So <span style="color:#365f91; font-family:Consolas; font-size:9pt">OnPaint</span> looks something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">protected</span> <span style="color: #0600FF;">override</span> <span style="color: #0600FF;">void</span> OnPaint<span style="color: #000000;">&#40;</span>PaintEventArgs e<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #008080; font-style: italic;">// If we have nodes, then we have to draw them - and that means everything</span>
	<span style="color: #008080; font-style: italic;">// about the node.</span>
	<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Nodes</span>.<span style="color: #0000FF;">Count</span> <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #008080; font-style: italic;">// If we have nodes then there has to be a Nodes[0]. Use it to get the</span>
		<span style="color: #008080; font-style: italic;">// height of one TreeNode - in a TreeView, all nodes are the same height</span>
		<span style="color: #008080; font-style: italic;">// (unless you are using PInvoke to call TVM_SETITEM and set TV_ITEM </span>
		<span style="color: #008080; font-style: italic;">// iIntegral member for a specific node - which we are not doing!).</span>
		<span style="color: #FF0000;">int</span> nodeHeight <span style="color: #008000;">=</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Nodes</span><span style="color: #000000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#93;</span>.<span style="color: #0000FF;">Bounds</span>.<span style="color: #0000FF;">Height</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #FF0000;">int</span> curY <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
&nbsp;
		<span style="color: #008080; font-style: italic;">// Walk down the client area, stepping by the height of one row, looking </span>
		<span style="color: #008080; font-style: italic;">// for nodes.</span>
		<span style="color: #0600FF;">while</span> <span style="color: #000000;">&#40;</span>curY <span style="color: #008000;">&lt;=</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">ClientRectangle</span>.<span style="color: #0000FF;">Height</span><span style="color: #000000;">&#41;</span>
		<span style="color: #000000;">&#123;</span>
			<span style="color: #008080; font-style: italic;">// This probably isn't the most efficient way to do this, but</span>
			<span style="color: #008080; font-style: italic;">// at least we aren't writing our own code to walk the tree...</span>
			TreeNode node <span style="color: #008000;">=</span> <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">GetNodeAt</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">0</span>, curY<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
			<span style="color: #008080; font-style: italic;">// If this rect does contain a node, draw the node.</span>
			<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>node <span style="color: #008000;">!=</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
			<span style="color: #000000;">&#123;</span>
				DrawTreeNodeEventArgs args <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> DrawTreeNodeEventArgs<span style="color: #000000;">&#40;</span>
					e.<span style="color: #0000FF;">Graphics</span>,
					node,
					e.<span style="color: #0000FF;">ClipRectangle</span>,
					GetNodeStates<span style="color: #000000;">&#40;</span>node<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// manufacture the proper TreeNodeStates</span>
&nbsp;
				DrawCustomNode<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">ref</span> args<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
			<span style="color: #000000;">&#125;</span>
&nbsp;
			<span style="color: #008080; font-style: italic;">// If this rect doesn't contain a node, we need to fill it with the </span>
			<span style="color: #008080; font-style: italic;">// background color.</span>
			<span style="color: #0600FF;">else</span>
				e.<span style="color: #0000FF;">Graphics</span>.<span style="color: #0000FF;">FillRectangle</span><span style="color: #000000;">&#40;</span>
					_backBrush, 
					<span style="color: #FF0000;">0</span>, 
					curY, 
					<span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">ClientRectangle</span>.<span style="color: #0000FF;">Width</span>, 
					nodeHeight<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
			curY <span style="color: #008000;">+=</span> nodeHeight<span style="color: #008000;">;</span>
		<span style="color: #000000;">&#125;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// If there are no nodes in the tree, we're going to have to paint the </span>
	<span style="color: #008080; font-style: italic;">// entire background.</span>
	<span style="color: #0600FF;">else</span>
		e.<span style="color: #0000FF;">Graphics</span>.<span style="color: #0000FF;">FillRectangle</span><span style="color: #000000;">&#40;</span>_backBrush, <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">ClientRectangle</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// Don't need - in this case we can guarantee no one is listening...</span>
	<span style="color: #008080; font-style: italic;">//base.OnPaint(e);</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>As you can see, our job is to just walk down the client area of the <span style="color:#365f91; font-family:Consolas; font-size:9pt">TreeView</span>, one node height at a time, checking to see if there is a node there. (There is a lot of optimizing opportunity here.) If we have a node we draw it; if there isn&#8217;t, we draw background. That&#8217;s all there is to it &#8211; well almost. You probably noticed there are a couple of methods stuck in the middle of <span style="color:#365f91; font-family:Consolas; font-size:9pt">OnPaint</span> that we haven&#8217;t talked about yet – and these can be a bit of a pickle.</p>
<p>The first problem we need to resolve is addressed by the <span style="color:#365f91; font-family:Consolas; font-size:9pt">GetNodeStates</span> method which supplies the <span style="color:#365f91; font-family:Consolas; font-size:9pt">TreeNodeStates</span> parameter for the <span style="color:#365f91; font-family:Consolas; font-size:9pt">DrawTreeNodeEventArgs</span> constructor. The stock <span style="color:#365f91; font-family:Consolas; font-size:9pt">TreeView</span> that you are familiar with has a curious behavior when it comes to highlighting selected <span style="color:#365f91; font-family:Consolas; font-size:9pt">TreeNodes</span>. If you click on one node to select it, its background gets painted with <span style="color:#365f91; font-family:Consolas; font-size:9pt">SystemBrushes.Highlight</span> and all is good. But if now you click-and-hold on a second node you will notice the first node loses its highlight and the second node is now highlighted – but it gets even more complex. If now (while still holding your mouse button down) you move your mouse a bit (say more than 15 pixels or so) the highlight reverts to the first node. This behavior is made possible by <span style="color:#365f91; font-family:Consolas; font-size:9pt">TreeNodeStates.Focused</span>. It turns out that in a <span style="color:#365f91; font-family:Consolas; font-size:9pt">TreeView</span>, node highlighting is indicated by <span style="color:#365f91; font-family:Consolas; font-size:9pt">Focused</span> rather than <span style="color:#365f91; font-family:Consolas; font-size:9pt">Selected</span>, so it&#8217;s the coming and going of <span style="color:#365f91; font-family:Consolas; font-size:9pt">Focused</span> for the first and second nodes that is prompting the highlighting behavior we just saw. The problem is that we don&#8217;t actually have a way to directly test for the <span style="color:#365f91; font-family:Consolas; font-size:9pt">Focused</span> state. If you are using <span style="color:#365f91; font-family:Consolas; font-size:9pt">OwnerDrawText</span> or <span style="color:#365f91; font-family:Consolas; font-size:9pt">OwnerDrawAll</span> you get the <span style="color:#365f91; font-family:Consolas; font-size:9pt">Focused</span> state handed to you when your <span style="color:#365f91; font-family:Consolas; font-size:9pt">OnDrawNode</span> override is called, but when doing our own drawing in <span style="color:#365f91; font-family:Consolas; font-size:9pt">OnPaint</span>, we need to manufacture the <span style="color:#365f91; font-family:Consolas; font-size:9pt">Focused</span> state and this is the purpose for <span style="color:#365f91; font-family:Consolas; font-size:9pt">GetNodeStates </span>(this is the secret sauce):</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">private</span> TreeNodeStates GetNodeStates<span style="color: #000000;">&#40;</span>TreeNode node<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	TreeNodeStates states <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// If mouse button is not down, then normal rules apply - if a node is</span>
	<span style="color: #008080; font-style: italic;">// selected, then it also has focus. We set _mouseDownNode in our</span>
	<span style="color: #008080; font-style: italic;">// OnMouseDown override and clear it in our OnMouseUp override.</span>
	<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>_mouseDownNode <span style="color: #008000;">==</span> <span style="color: #0600FF;">null</span><span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>node.<span style="color: #0000FF;">IsSelected</span><span style="color: #000000;">&#41;</span>
			states <span style="color: #008000;">=</span> TreeNodeStates.<span style="color: #0000FF;">Selected</span> <span style="color: #008000;">|</span> TreeNodeStates.<span style="color: #0000FF;">Focused</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// But if a mouse button has been clicked on a node, then we need to check</span>
	<span style="color: #008080; font-style: italic;">// to see if that node is currently drop-highlighted.</span>
	<span style="color: #0600FF;">else</span> <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>node <span style="color: #008000;">!=</span> _mouseDownNode<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #008080; font-style: italic;">// If this is the currently selected node, but a mouse button has been</span>
		<span style="color: #008080; font-style: italic;">// clicked-and-held on another node and that node is currently showing </span>
		<span style="color: #008080; font-style: italic;">// the drop-highlight, then we don't show selection highlighting for this</span>
		<span style="color: #008080; font-style: italic;">// node.</span>
&nbsp;
		<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>IsDropHighlighted<span style="color: #000000;">&#40;</span>_mouseDownNode<span style="color: #000000;">&#41;</span> <span style="color: #008000;">&amp;&amp;</span> node.<span style="color: #0000FF;">IsSelected</span><span style="color: #000000;">&#41;</span>
			states <span style="color: #008000;">=</span> TreeNodeStates.<span style="color: #0000FF;">Selected</span> <span style="color: #008000;">|</span> TreeNodeStates.<span style="color: #0000FF;">Focused</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// Othewise if this is the node that has been clicked-and-held and if</span>
	<span style="color: #008080; font-style: italic;">// this node is currently showing drop-highlighting, then show it. We </span>
	<span style="color: #008080; font-style: italic;">// still need to check to see if drop-highlighting is being shown for </span>
	<span style="color: #008080; font-style: italic;">// this node because if the user clicked-and-held on this node, but</span>
	<span style="color: #008080; font-style: italic;">// then started to move the mouse, drop-highlighting is removed by</span>
	<span style="color: #008080; font-style: italic;">// the underlying Win32 tree control.</span>
	<span style="color: #0600FF;">else</span> <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>node <span style="color: #008000;">==</span> _mouseDownNode<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>IsDropHighlighted<span style="color: #000000;">&#40;</span>node<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
			states <span style="color: #008000;">=</span> TreeNodeStates.<span style="color: #0000FF;">Focused</span><span style="color: #008000;">;</span>
	<span style="color: #000000;">&#125;</span>
&nbsp;
	<span style="color: #0600FF;">return</span> states<span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The final pieces here are a couple of calls to the underlying Win32 tree control to get the state of the nodes in question from which we can determine if a particular node is drop-highlighted:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">private</span> <span style="color: #FF0000;">bool</span> IsDropHighlighted<span style="color: #000000;">&#40;</span>TreeNode node<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #0600FF;">const</span> <span style="color: #FF0000;">int</span> MASK <span style="color: #008000;">=</span> NativeMethods.<span style="color: #0000FF;">TVIF_STATE</span> <span style="color: #008000;">|</span> NativeMethods.<span style="color: #0000FF;">TVIF_HANDLE</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #008080; font-style: italic;">// TV_ITEM stateMask doesn't seem to matter when reading state - we get</span>
	<span style="color: #008080; font-style: italic;">// all the state bits anyway (Windows7).</span>
	<span style="color: #0600FF;">const</span> <span style="color: #FF0000;">int</span> STATEMASK <span style="color: #008000;">=</span> NativeMethods.<span style="color: #0000FF;">TVIS_DROPHILITED</span><span style="color: #008000;">;</span>
&nbsp;
	NativeMethods.<span style="color: #0000FF;">TV_ITEM</span> lParam <span style="color: #008000;">=</span> 
		<span style="color: #008000;">new</span> NativeMethods.<span style="color: #0000FF;">TV_ITEM</span> <span style="color: #000000;">&#123;</span> 
			hItem <span style="color: #008000;">=</span> node.<span style="color: #0000FF;">Handle</span>, 
			mask <span style="color: #008000;">=</span> MASK, 
			stateMask <span style="color: #008000;">=</span> STATEMASK <span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
&nbsp;
	UnsafeNativeMethods.<span style="color: #0000FF;">SendMessage</span><span style="color: #000000;">&#40;</span>
		<span style="color: #008000;">new</span> HandleRef<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>, <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Handle</span><span style="color: #000000;">&#41;</span>, 
		NativeMethods.<span style="color: #0000FF;">TVM_GETITEMW</span>, 
		<span style="color: #FF0000;">0</span>, 
		<span style="color: #0600FF;">ref</span> lParam<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
	<span style="color: #0600FF;">return</span> <span style="color: #000000;">&#40;</span>lParam.<span style="color: #0000FF;">state</span> <span style="color: #008000;">&amp;</span> NativeMethods.<span style="color: #0000FF;">TVIS_DROPHILITED</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">!=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>(The definitions for the various Win32 methods, structures and constants can be found in the attached sample.)</p>
<p>The second method we haven&#8217;t talked about is DrawCustomNode – this is where I actually get around to drawing the TreeNodes. Since my goal for this project was to create a simple view of an XmlDocument, the code just implements the stylized drawing to do that – and this is where you would put your code to draw a BarGraphTreeNode or whatever. In my case, since I am &#8220;syntax coloring&#8221; Xml, there is quite a bit going on to insure various segments of the colored text line up correctly – but that&#8217;s another story.</p>
<p>The attached sample (<a href="http://eclectrics.com/software/wp-content/uploads/2012/01/WeatherToolsTesterUpdate.zip">executable</a>, <a href="http://eclectrics.com/software/wp-content/uploads/2012/01/WeatherToolsUpdate.zip">source</a>) contains a fully working test app using the NDFD web service to get weather forecasts for specific ZIP codes. It pushes the Xml forecast data into our new XmlTreeView. I hope you enjoy.</p>
]]></content:encoded>
			<wfw:commentRss>http://eclectrics.com/software/2009/08/xmltreeview-%e2%80%93-a-custom-painted-treeview/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

