A Print Enabled Tree Control

I realized a lot of developers are interested in code for printing CTreectrl. I was in the same situation and tried a lot of different ways, but every approach had its disadvantage.

The simplest solution seemed in the call of CWnd::Print(…), but this only prints the visible part of the control, which was not what I wanted. One way to make this approach work was to scroll the TreeCtrl in the printing code from the start to the end of the control, calling CWnd::Print(…) after every scroll, which was a little bit weird for me. Additionally I wanted as much flexibility in my code as possible, which means printing with or without icons, lines, buttons.

That’s why I began to implement the whole stuff by myself, following the next few steps:

Init ImageList:

In my opinion and my understanding of OOD the Treecontrol should be able to display itself, that’s why it should have the imagelist and initialize it.


This is also done by hand, like everything else. Iterate through the tree and create an array with the PagingInfos:

struct PagingInfos
   HTREEITEM    m_hStartItem;
   int          m_iNumberOfItems;


Iterate through the tree and print every node by code, including lines and buttons.

Imagelist vs DIB:

In an additional step I wanted to print the tree in color. This was easy for text, but not for the icons. I tried a lot to convert the imagelist to DIB, but what I really needed was a method, which gives back the DIB of one icon, not of the whole Imagelist.

Bugs and Improvements:

  • Compiling the release version is now working too.

  • If the convertion to DIB failes the program gives back an errorcode and prints without graphic( see Comment below).


After the first release, I received some emails about problems with the example project, which was crashing under some conditions. My problem is, that every time I tested my code under the described conditions it worked fine. What I’ve done now, was to extend the project with more errorhandling and it would be nice, if all users which are receiving errorcodes, could report me the following informations:

  • Errorcode

  • OS with servicepack

  • IDE with servicepack

  • Optional: Which conversion failed

Please report all bugs and improvements to me, thanks and enjoy it.

More by Author

Must Read