NO

Author Topic: TreeView Drawing  (Read 6571 times)

Offline WiiLF23

  • Member
  • *
  • Posts: 89
TreeView Drawing
« on: December 15, 2023, 02:54:39 AM »
I have a question regarding custom drawing for the TreeView control. I have been having several issues painting, setting full row select (RC or manually), and I get as far as to getting uxtheme set for it, but that is just basic explorer style (Windows XP+).

Code: [Select]
#include <uxtheme.h>
#pragma comment(lib, "uxtheme.lib")
...
SetWindowTheme(hTreeView, L"Explorer", NULL);

I tried this, not working
Code: [Select]
LONG_PTR style = GetWindowLongPtr(hTreeView, GWL_STYLE);
SetWindowLongPtr(hTreeView, GWL_STYLE, style | TVS_FULLROWSELECT);

Setting the control background works
Code: [Select]
SendMessage(hTreeView, TVM_SETBKCOLOR, 0, RGB(255, 255, 255));

EDIT:
https://learn.microsoft.com/en-us/windows/win32/controls/tree-view-control-window-styles
says "This style cannot be used in conjunction with the TVS_HASLINES style.". This was my mistake. I have TVS_HASLINES enabled.

Now for painting, I still cant get this part working.
Subclassing the control with a WNDPROC subclasses, but I get no paint events. I cant figure this out. Often in different attempts, I get a blank TreeView control with just the window color.

Does anyone have a project or link demonstrating padding and selection color for the TreeView control? Thanks ;D


Cheers!
« Last Edit: December 15, 2023, 03:04:57 AM by WiiLF23 »

Offline John Z

  • Member
  • *
  • Posts: 860
Re: TreeView Drawing
« Reply #1 on: December 15, 2023, 11:18:03 AM »
Hi fearless WiiLF23

Intercept       case WM_CTLCOLORLISTBOX:
then check that call is from your treeview
        if ( (HWND)lParam == GetDlgItem(gHWND, TreeView1) ).... if so then
//treeview color
TreeView_SetBkColor(GetDlgItem(gHWND, TreeView1),gColor);

where gColor previously selected by
gColor = Select_Color(gHWND,gColor);// main windows handle, last or initial gColor
or just part of base code.
-----------
Code: [Select]
//-----------------------------------------------------------------------
// Function : Select_Color
// Task : Select a color and return it
// Returns : color
// Input : owning window, initial color RGB number
// Globals : not this time
//-----------------------------------------------------------------------
DWORD Select_Color(HWND hwnd, DWORD startcolor)
{

CHOOSECOLOR cc;                 // common dialog box structure
static COLORREF acrCustClr[16]; // array of custom colors
static DWORD rgbCurrent;     // initial color selection

// Initialize CHOOSECOLOR
ZeroMemory(&cc, sizeof(cc));
cc.lStructSize = sizeof(cc);
cc.hwndOwner = hwnd;
cc.lpCustColors = (LPDWORD)acrCustClr;
cc.rgbResult = startcolor;
cc.Flags = CC_FULLOPEN | CC_RGBINIT;

if (ChooseColor(&cc) == TRUE)
{

rgbCurrent = cc.rgbResult;
}
else
{
rgbCurrent = startcolor;
}

return rgbCurrent;

}/* end select_color */
John Z
« Last Edit: December 15, 2023, 11:37:31 AM by John Z »

Offline John Z

  • Member
  • *
  • Posts: 860
Re: TreeView Drawing
« Reply #2 on: December 15, 2023, 12:27:17 PM »
It occurs to me I may have misunderstood.  If you are wanting to color individual treeview items differently then what I posted is not what you want.

This post may help although I have not tried it:
https://stackoverflow.com/questions/62733870/color-specific-treeview-item-using-win32-api-c-c

Be sure to read that last Line in the posting mentioning that SetWindowLongPtr  needs to be added but is not shown in the posted code.

Hopefully more helpful

John Z

Offline WiiLF23

  • Member
  • *
  • Posts: 89
Re: TreeView Drawing
« Reply #3 on: December 16, 2023, 11:54:34 PM »
I appreciate the help!

I have settled for uxtheme on the control handle in the meantime until I can learn to develop a styled TreeView control. This just limits the application visuals for that control for Windows XP+ (which is fine), which i believe falls back to the old style in absence of uxtheme support.

Offline CFred

  • Member
  • *
  • Posts: 36
Re: TreeView Drawing
« Reply #4 on: December 27, 2023, 01:30:00 PM »
Does anyone have a project or link demonstrating padding and selection color for the TreeView control?

I have just produced a program that demonstrates how to colour the individual labels of a TreeView control (attached).
This program demonstrates how to set the background colour and the text colour of the labels in a TreeView control for both when a node is selected and when a node is not selected.

Right-click on a node and select an option from the context menu to change the colour of the label for that node. When you click on another label the chosen colours will be applied.

The colours are stored in a structure associated with the lParam member of the TV_ITEM structure of a TreeView control. When an option is selected from the context menu, a notification message is sent to the TreeView control's parent window containing the code NM_CUSTOMDRAW. The program responds to this code to colour the nodes of the tree.

When the program terminates, the memory used to store the data in the lParam member of the TV_ITEM structure for each node must be freed by calling the recursive function FreeLParam.

Although a global handle for the TreeView control is declared, the functions in this program have an argument for the handle of the TreeView control. This is because these functions form part of a TreeView library I am developing and I will publish the library in this forum when I have completed it.

Each function contains a brief description of its purpose.

Offline Vortex

  • Member
  • *
  • Posts: 864
    • http://www.vortex.masmcode.com
Re: TreeView Drawing
« Reply #5 on: December 27, 2023, 06:33:31 PM »
Hi Fred,

I managed to set the text color of all the nodes to black but not possible to revert back to the original blue color.
Code it... That's all...

Offline CFred

  • Member
  • *
  • Posts: 36
Re: TreeView Drawing
« Reply #6 on: December 28, 2023, 06:16:46 PM »
I managed to set the text color of all the nodes to black but not possible to revert back to the original blue color.

The program will only set the colours of the nodes to the colours that are hard-coded in the function TREE_ShowTreeviewMenu(). To revert the colours back to the original blue, you need to add further options to the context menu to do this. Alternatively, you could write code that enables the the context menu to call the Windows API ChooseColor() function to show a Color common dialog box to allow the user to select any colour they want.

I did not include this possibility in my zip file as my program was only intended to demonstrate how to set the colour of the nodes.

Offline Vortex

  • Member
  • *
  • Posts: 864
    • http://www.vortex.masmcode.com
Re: TreeView Drawing
« Reply #7 on: December 28, 2023, 07:27:26 PM »
Hi Fred,

Thanks for the info, much appreciated.
Code it... That's all...

Offline WiiLF23

  • Member
  • *
  • Posts: 89
Re: TreeView Drawing
« Reply #8 on: January 06, 2024, 08:26:57 PM »
At some point I am forced to produce a custom TreeView control as this is the heart of my application. Not at all easy to do. Source code is on github so maybe it is possible. I need node padding, custom color highlighting, and gradient effects.

UXtheme is just so limited, but looks good.