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.

Paging:

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;
};

DrawTreeCtrl:

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).

Comment:

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.



Downloads

Comments

  • Broken Link

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

    Unable to download samplecode ?

    Reply
  • 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.

    Reply
  • 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.

    Reply
  • PRINT THE TREE CONTROL & ALL ELEMENTS UNDER ALL NODES

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

    Originally posted by: Niranjan

    Hi,
    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
    Niranjan

    Reply
  • 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?
    Thanks,

    Emily

    Reply
  • ww

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

    Originally posted by: nazia



    what will be steps and code of tree control

    Reply
  • array not initialized

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

    Originally posted by: chuck

    in the constructor

    CAdvancedTreeCtrl::CAdvancedTreeCtrl()...{
    ..

    // 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

    Reply
  • 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!! : )

    Reply
  • 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!! : )

    Reply
  • 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?

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

    The result is that the items are correctly sorted.

    Reply
  • Loading, Please Wait ...

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

Top White Papers and Webcasts

  • On-demand Event Event Date: September 10, 2014 Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild." This loop of continuous delivery and continuous feedback is how the best mobile …

  • Packaged application development teams frequently operate with limited testing environments due to time and labor constraints. By virtualizing the entire application stack, packaged application development teams can deliver business results faster, at higher quality, and with lower risk.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds