VividTree - A Colorful and Picturesque Owner Drawn CTreeCtrl Class






4.85/5 (31 votes)
Sep 21, 2005
3 min read

392466

8075
An colorful owner drawn CTreeCtrl derived class loosly modeled after Skype's main window.
Introduction
This owner draw tree control allows for the creation of a tree control loosely resembling the embedded tree in Skype's main window. It demonstrates the basics of creating an owner-drawn tree control, but more importantly, demonstrates gradient backgrounds fills, bitmap backgrounds, flicker free drawing, and how to deal with these in a scrollable and resizable dialog.
Building and Running the Demo Code
Both VC++ 6.0 and VC++ 7.1 project files are included. VC++ 7.1 output directories use the normal Debug/Release directory names. VC+ 6.0 uses VS6_Debug and VS6_Release. The demonstration program has a context (right-click) menu which allows you to change play with some of the different display modes.
Using the code
Importing the code into your project is a fairly easy process. Below are some simple steps to follow:
Step 1: Import the files and resources
Start by simply adding the two source files VividTree.cpp and VividTree.h to your project. Now, add the two bitmap resources (tree_closed.bmp and tree_opened.bmp) and set their resource ID names to IDB_TREE_CLOSED
and IDB_TREE_OPENED
.
Step 2: Create or change your CTreeCtrl based object
If you already have a CTreeCtrl
object, change its declaration to VividTree
and include VividTree.h, or create a new CTreeCtrl
object and change its declaration to VividTree
.
Step 3: Customize the Look
Several customizations are available to change VividTree
's look. These class interfaces are shown below:
// BackGround Mode Interfaces enum BkMode { BK_MODE_BMP = 0, BK_MODE_GRADIENT, BK_MODE_FILL }; BkMode GetBkMode( ); void SetBkMode( BkMode bkgd_mode ); // Gradient Property Interfaces void SetBkGradients( COLORREF from, COLORREF to ); COLORREF GetBkGradientFrom( ); COLORREF GetBkGradientTo( ); bool GetGradientHorz(); void SetGradientHorz( bool horz ); // true = horizontal, false = vertical // Bitmap Background Interfaces bool GetBitmapTiledMode(); void SetBitmapTiledMode( bool tiled ); void SetBitmapID( UINT id ); // Leaf ICON Setup virtual void SetItemIcon( HICON icon ); virtual HICON GetItemIcon( HTREEITEM item ); // Overridable
The left most screenshot graphic (above) mostly shows the default look of VividTree
. The one customization done was to add a default icon to the tree leafs (see the code below). Note: the default implementation of GetItemIcon
only supports one icon, but GetItemIcon
is overridable to allow derived classes to be more prolific in their graphic usage.
m_icon = (HICON)LoadImage( AfxGetApp()->m_hInstance, MAKEINTRESOURCE(IDI_CODE_PROJECT), IMAGE_ICON, 16, 16, LR_VGACOLOR ); m_tree.SetItemIcon( m_icon );
In the second screenshot graphic above, a background bitmap is added. The bitmap is stretched to fit the size of the window and re-stretched on a window size change. Below is the initialization code added to achieve this:
m_tree.SetBitmapID( IDB_CODE_PROJECT ); m_tree.SetBkMode( VividTree::BK_MODE_BMP );
In the third screenshot graphic above, VividTree
is configured to tile the graphic in the client window. The code to set this mode is:
m_tree.SetBitmapTiledMode( true );
History
Below is the version history for VividTree
:
- 9/1/2005 - 1.0.0
Initial implementation.
Bug Reports
If you have a bug report, or a bug fix, you can contact me through email or with the forums below. I would appreciate a notification of any bugs discovered or improvements that could be made to help the control grow for everyone.
License
This code is provided "as is" with no expressed or implied warranty. You may use, or derive works from this file without any restrictions except those listed below:
- The original code header must be kept in the derived work.
- If your derived work is distributed in any form, you must notify the author and provide a short description of the product and intended audience.
- You may not sell this code or a derived version of it as part of a commercial code library.
Offering the author a free licensed copy of any end product using this code is not required, but does endear you with a bounty of good karma.
Other Code Projects Used by This Project:
- VC++7 to VC++6 project converter
This tool automatically converts Visual C++ 7.0 projects back to Visual C++ 6.0 projects.