title: Collapse all nodes in treeview, except the last expanded one
body: <h2>INTRODUCTION AND RELEVANT INFORMATION:</h2>
  
  <p>I need to implement the following scenario: </p>
  
  <ol>
  <li><p>User expands a node, e.g. <code>Node 1</code>;</p></li>
  <li><p>User expands another node, e.g. <code>Node 2</code>;</p></li>
  <li><p>Collapse previous node ( <code>Node 1</code> );</p></li>
  </ol>
  
  <p>To visually explain what I mean, we shall use the following example image :</p>
  
  <p><img src="https://i.stack.imgur.com/bAdM3.jpg" alt="enter image description here"></p>
  
  <p>Now when user clicks on <code>Assembly 1</code> node or its child node <code>Components</code>, I need to collapse every other node. An example picture below illustrates this :</p>
  
  <p><img src="https://i.stack.imgur.com/zsFC6.jpg" alt="enter image description here"></p>
  
  <h2>MY EFFORTS TO IMPLEMENT THIS BEHAVIOR:</h2>
  
  <p>Browsing through Internet, and with a little thinking of my own, I was able to write helper function that collapses node and its children :</p>
  
  <pre><code>void CollapseNode( HWND hTree, HTREEITEM hti )
  {
      if( TreeView_GetChild( hTree, hti ) != NULL )
      {
          TreeView_Expand( hTree, hti, TVE_COLLAPSE );
          hti = TreeView_GetChild( hTree, hti );
          do
          {
              CollapseNode( hTree, hti );
          }
          while( ( hti = TreeView_GetNextSibling( hTree, hti ) ) != NULL  );
      }
  }
  </code></pre>
  
  <p>Reading through <em>MSDN</em> documentation I found <code>TVN_ITEMEXPANDING</code> and <code>TVN_ITEMEXPANDED</code> messages that might be useful.</p>
  
  <p>I have also found <code>NM_CLICK</code> notification that seems interesting since I can use <code>TVM_HITTEST</code> message to test if click was on <em>+/-</em> button.</p>
  
  <p>Also I have found <code>TVN_KEYDOWN</code> message that will help me with expanding when user presses <kbd>left arrow</kbd> or <kbd>right arrow</kbd> keys.</p>
  
  <h2>PROBLEM:</h2>
  
  <p>I can not think of an algorithm for using the above messages to solve my task. </p>
  
  <h2>QUESTION:</h2>
  
  <p>Can you please suggest me an algorithm for handling the above messages so I can call my <code>CollapseNode(..)</code> function ?</p>
  
  <p>Something like this:</p>
  
  <p><em>Hittest in <code>NM_CLICK</code> and then call your function</em> or <em>Store the last expanded item in a variable and collapse it in response to <code>TVN_ITEMEXPANDED</code></em> would be good for a start.</p>
  
  <p>Thank you.</p>