A Print Enabled Tree Control


Application Security Testing: An Integral Part of DevOps

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.



  • Broken Link

    Posted by anand_study on 08/30/2007 02:24am

    Unable to download samplecode ?

  • Fine.

    Posted by Legacy on 01/16/2004 12:00am

    Originally posted by: Feroz Zahid.

    Good work with the printing but let it be done without using microsoft foundation classes.

  • Bugs and Updates

    Posted by Legacy on 07/10/2002 12:00am

    Originally posted by: Mike Wild

    Sorry Guys
    but I gave up to update this article, because I tried it a few times, but it didn't realy work.
    I know about the Bugs you reported and I really wanted to post a new version, but it was so annoying.
    At the moment I'm trying to do the printing for C# and perhaps you see my posting there again.


    Posted by Legacy on 05/30/2002 12:00am

    Originally posted by: Niranjan

    My requirement is to print all the elements weather expanded or not expanded......Your code provides printing for items which are expanded only do you have any idea how i can implement it for printing for items under the nodes which are not expanded also?? I want to print the items under all nodes (including the nodes) even if the node is not expanded... hope to hear from u soon...
    Thanks a lot

  • Need help for using CWnd::Print

    Posted by Legacy on 11/26/2001 12:00am

    Originally posted by: emily

    I try to use CWnd::Print(...) tp print the entire dialog
    box, but it didn't work at all.
    Anyone used this function call before?


  • ww

    Posted by Legacy on 08/09/2001 12:00am

    Originally posted by: nazia

    what will be steps and code of tree control

  • array not initialized

    Posted by Legacy on 06/13/2001 12:00am

    Originally posted by: chuck

    in the constructor


    // added the line
    m_dibButtons[0] = NULL; m_dibButtons[1] = NULL;

    This eliminated an error in CAdvancedTreeCtrl::DeleteDibArrays when doing a print preview.

    this was with vc6, sp5

  • How about printing with check boxes??

    Posted by Legacy on 01/19/2000 12:00am

    Originally posted by: Steve Chia

    If a tree control has the check box property set, it will appear in the GUI but not in the printout. Does anyone know how to add in the check box in the printout??

    Thank you very much!! : )

  • How about printing with check boxes??

    Posted by Legacy on 01/19/2000 12:00am

    Originally posted by: Steve Chia

    If a tree control has the check box property set, it will appear in the GUI but not in the printout. Does anyone know how to add in the check box in the printout??

    Thank you very much!! : )

  • You are correct

    Posted by Legacy on 04/29/1999 12:00am

    Originally posted by: Douglas Peterson

    I can see now that you are correct. The documentation is inacurate.

    However, if two items are equal, does it matter if their positions are swapped?

    1 < should this go before or after the other 1?

    The result is that the items are correctly sorted.

  • Loading, Please Wait ...

  • You must have javascript enabled in order to post comments.

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • IT planners have far more options as to where to run their workloads than ever before. On-premises data centers, co-location facilities and managed services providers are now joined by hybrid multi-clouds – a combination of Software-, Infrastructure- and Platform-as-a-Service (SaaS, IaaS, and PaaS) execution venues. All have unique operational, performance and economic characteristics that need to be considered when deploying workloads. Submit the form to watch this webinar replay featuring 451 …

  • A CRM solution holds a wealth of information and document generation tools allow users to take that information and create documents with both visual appeal and function. Document generation is the process of automatically producing a file and document generation applications save companies time, mistakes, and money. You bought Salesforce to be more efficient — why are you still manually creating proposals, contracts, invoices, and account plans? Read this eBook to learn how you can automate virtually …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date
We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.