C language > Beginner questions

TreeView Drawing

(1/2) > >>

WiiLF23:
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: ---#include <uxtheme.h>
#pragma comment(lib, "uxtheme.lib")
...
SetWindowTheme(hTreeView, L"Explorer", NULL);

--- End code ---

I tried this, not working

--- Code: ---LONG_PTR style = GetWindowLongPtr(hTreeView, GWL_STYLE);
SetWindowLongPtr(hTreeView, GWL_STYLE, style | TVS_FULLROWSELECT);

--- End code ---

Setting the control background works

--- Code: ---SendMessage(hTreeView, TVM_SETBKCOLOR, 0, RGB(255, 255, 255));

--- End code ---

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!

John Z:
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: ---//-----------------------------------------------------------------------
// 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 */

--- End code ---
John Z

John Z:
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

WiiLF23:
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.

CFred:

--- Quote from: WiiLF23 on December 15, 2023, 02:54:39 AM ---Does anyone have a project or link demonstrating padding and selection color for the TreeView control?

--- End quote ---

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.

Navigation

[0] Message Index

[#] Next page

Go to full version