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

  • Live Event Date: August 20, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT When you look at natural user interfaces as a developer, it isn't just fun and games. There are some very serious, real-world usage models of how things can help make the world a better place – things like Intel® RealSense™ technology. Check out this upcoming eSeminar and join the panel of experts, both from inside and outside of Intel, as they discuss how natural user interfaces will likely be getting adopted in a wide variety …

  • "Security" is the number one issue holding business leaders back from the cloud. But does the reality match the perception? Keeping data close to home, on premises, makes business and IT leaders feel inherently more secure. But the truth is, cloud solutions can offer companies real, tangible security advantages. Before you assume that on-site is the only way to keep data safe, it's worth taking a comprehensive approach to evaluating risks. Doing so can lead to big benefits.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds