Custom draw treeview. Really don't understand what happens


2007-03-06 05:07:54 AM
cppbuilder44
Hi,
I'm trying to draw additional information next to tree nodes of a tree view.
So I'm using POSTPAINT processing, and I have copied most of the code
from this NG.
The result is a little bit disturbing ...
The 8888 string (see code below) does not appear initially !! But it
appears as soon as I change the size of the TV (there's a splitter next
to the TV) and it disappears again (but not for all nodes) as soon as I
selects a node !!!
I'm stuck on this for 3 days, I must have missed a stupid thing but I
can not see what.
(I'm using BCB 6 on Win XP)
I've tried using windows messages and vcl methods, nothing works ?
the first way of doing it
MESSAGE void __fastcall TSmCustomTreeView::CNNotify(TMessage& Msg)
{
// grab a pointer to the NM_TREEVIEW structure
LPNM_TREEVIEW lpnm = (NM_TREEVIEW *)Msg.LParam;
if (lpnm->hdr.hwndFrom == this->Handle)
{
if (lpnm->hdr.code == NM_CUSTOMDRAW)
{
LPNMCUSTOMDRAW lpcd = (NMCUSTOMDRAW *)Msg.LParam;
LPNMTVCUSTOMDRAW lptvcd = (NMTVCUSTOMDRAW *)Msg.LParam;
const HTREEITEM hItem = reinterpret_cast<const
HTREEITEM>(lptvcd->nmcd.dwItemSpec);
// check the drawing stage
switch (lptvcd->nmcd.dwDrawStage)
{
// prior to painting...
case CDDS_PREPAINT:
Msg.Result = CDRF_NOTIFYITEMDRAW;
break;
// notification of each item...
case CDDS_ITEMPREPAINT:
{
Msg.Result = CDRF_NOTIFYPOSTPAINT;
break;
}
case CDDS_ITEMPOSTPAINT: //(CDDS_ITEM | CDDS_POSTPAINT):
{
RECT rcItem = lpcd->rc;
RECT rcLabel;
TreeView_GetItemRect(Handle,hItem,&rcLabel,true);
COLORREF crTextBk = lptvcd->clrTextBk;
COLORREF crText = lptvcd->clrText;
COLORREF crWnd = GetSysColor(COLOR_WINDOW);
RECT rcCols = rcItem;
rcCols.left=rcLabel.right + 1;
if (rcCols.left < rcCols.right)
{
Canvas->TextRect(rcCols, rcCols.left, rcCols.top, "8888888");
}
Msg.Result = CDRF_SKIPDEFAULT;
break;
}
default:
{
}
return;
}
}
else
{
// Default processing
TTreeView::Dispatch(&Msg);
}
}
the second way
bool _fastcall TSmCustomTreeView::IsCustomDrawn
(TCustomDrawTarget Target
, TCustomDrawStage Stage
)
{
if( (Target == dtItem) || (Target == dtControl) )
return true;
return TTreeView::IsCustomDrawn(Target, Stage);
}
bool __fastcall TSmCustomTreeView::CustomDrawItem
(TTreeNode *Node
, TCustomDrawState State
, TCustomDrawStage Stage
, bool &PaintImages
)
{
switch( Stage )
{
case cdPrePaint:
{
PaintImages = true;
return true;
}
break;
case cdPostPaint:
{
if( true /*Node has unread messages*/ )
{
Canvas->Brush->Color = Color;
Canvas->Font->Color = clBlue;
Canvas->Font->Style = Canvas->Font->Style>>fsBold;
AnsiString Str = "88888";
TSize s = Canvas->TextExtent(Str);
TRect R = Node->DisplayRect(true);
int offsetY = ((R.Height() - s.cy) / 2);
R.Top += offsetY;
R.Bottom += offsetY;
R.Left = (R.Right + 2);
R.Right = (R.Left + s.cx);
Canvas->TextRect(R, R.Left, R.Top, Str);
// drawNodeColumn(Node, state);
}
return false;
break;
}
}
return true;
}
};