<?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; Custom Paint TreeView</title>
	<atom:link href="http://eclectrics.com/software/tag/custom-paint-treeview/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>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>

