NM_CUSTOMDRAW is handled in WM_NOFITY
If your TreeView control is inherited from standard TreeView, use WM_REFLECT + WM_NOFITY
Something like this =>
public class TreeViewCustom : TreeView
{
private const int NM_FIRST = 0;
private const int NM_CLICK = NM_FIRST - 2;
private const int NM_CUSTOMDRAW = NM_FIRST - 12;
private const int WM_REFLECT = 0x2000;
private const int WM_NOFITY = 0x004E;
[StructLayout(LayoutKind.Sequential)]
private struct NMHDR
{
public IntPtr hwndFrom;
public IntPtr idFrom;
public int code;
}
[StructLayout(LayoutKind.Sequential)]
private struct NMCUSTOMDRAW
{
public NMHDR hdr;
public int dwDrawStage;
public IntPtr hdc;
public RECT rc;
public IntPtr dwItemSpec;
public uint uItemState;
public IntPtr lItemlParam;
}
[StructLayout(LayoutKind.Sequential)]
private class NMTVCUSTOMDRAW
{
public NMCUSTOMDRAW nmcd;
public int clrText;
public int clrTextBk;
public int iLevel;
}
[FlagsAttribute]
internal enum CDRF
{
CDRF_DODEFAULT = 0x00000000,
CDRF_NEWFONT = 0x00000002,
CDRF_SKIPDEFAULT = 0x00000004,
CDRF_DOERASE = 0x00000008,
CDRF_SKIPPOSTPAINT = 0x00000100,
CDRF_NOTIFYPOSTPAINT = 0x00000010,
CDRF_NOTIFYITEMDRAW = 0x00000020,
CDRF_NOTIFYSUBITEMDRAW = 0x00000020,
CDRF_NOTIFYPOSTERASE = 0x00000040
}
[FlagsAttribute]
internal enum CDDS
{
CDDS_PREPAINT = 0x00000001,
CDDS_POSTPAINT = 0x00000002,
CDDS_PREERASE = 0x00000003,
CDDS_POSTERASE = 0x00000004,
CDDS_ITEM = 0x00010000,
CDDS_ITEMPREPAINT = (CDDS_ITEM | CDDS_PREPAINT),
CDDS_ITEMPOSTPAINT = (CDDS_ITEM | CDDS_POSTPAINT),
CDDS_ITEMPREERASE = (CDDS_ITEM | CDDS_PREERASE),
CDDS_ITEMPOSTERASE = (CDDS_ITEM | CDDS_POSTERASE),
CDDS_SUBITEM = 0x00020000
}
[StructLayout(LayoutKind.Sequential)]
public struct RECT
{
public int left;
public int top;
public int right;
public int bottom;
}
public TreeViewCustom()
{
DrawMode = TreeViewDrawMode.OwnerDrawAll;
// Code...
}
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_REFLECT + WM_NOFITY)
{
var pnmhdr = (NMHDR)m.GetLParam(typeof(NMHDR));
if (pnmhdr.code == NM_CUSTOMDRAW)
{
try
{
// Test code...
var pnmtv = (NMTVCUSTOMDRAW)m.GetLParam(typeof(NMTVCUSTOMDRAW));
switch (pnmtv.nmcd.dwDrawStage)
{
case (int)CDDS.CDDS_PREPAINT:
{
m.Result = new IntPtr((int)CDRF.CDRF_NOTIFYITEMDRAW);
}
break;
case (int)CDDS.CDDS_ITEMPREPAINT:
{
pnmtv.clrText = ColorTranslator.ToWin32(System.Drawing.Color.Red);
//pnmtv.clrTextBk = ColorTranslator.ToWin32(System.Drawing.Color.Lime);
Marshal.StructureToPtr(pnmtv, m.LParam, false);
m.Result = new IntPtr((int)(CDRF.CDRF_NEWFONT | CDRF.CDRF_NOTIFYPOSTPAINT));
}
break;
case (int)CDDS.CDDS_ITEMPOSTPAINT:
{
// Code...
}
break;
}
}
catch (NullReferenceException)
{
// Code...
}
}
return;
}
else
base.WndProc(ref m);
}
}