其实也没有什么指点的,我在VCL里也没有找到代码,呵呵! 看看TCustomTreeView的CreateParam方法如何作的就知道了 VCL代码 procedure TCustomTreeView.CreateParams(var Params: TCreateParams); const BorderStyles: array[TBorderStyle] of DWORD = (0, WS_BORDER); LineStyles: array[Boolean] of DWORD = (0, TVS_HASLINES); RootStyles: array[Boolean] of DWORD = (0, TVS_LINESATROOT); ButtonStyles: array[Boolean] of DWORD = (0, TVS_HASBUTTONS); EditStyles: array[Boolean] of DWORD = (TVS_EDITLABELS, 0); HideSelections: array[Boolean] of DWORD = (TVS_SHOWSELALWAYS, 0); DragStyles: array[TDragMode] of DWORD = (TVS_DISABLEDRAGDROP, 0); RTLStyles: array[Boolean] of DWORD = (0, TVS_RTLREADING); ToolTipStyles: array[Boolean] of DWORD = (TVS_NOTOOLTIPS, 0); AutoExpandStyles: array[Boolean] of DWORD = (0, TVS_SINGLEEXPAND); HotTrackStyles: array[Boolean] of DWORD = (0, TVS_TRACKSELECT); RowSelectStyles: array[Boolean] of DWORD = (0, TVS_FULLROWSELECT); begin //调用Windows方法来创建一个TreeView组件,其它的绘制动作其实也是Windows实现的 InitCommonControl(ICC_TREEVIEW_CLASSES); inherited CreateParams(Params); CreateSubClass(Params, WC_TREEVIEW); with Params do begin Style := Style or LineStyles[FShowLines] or BorderStyles[FBorderStyle] or RootStyles[FShowRoot] or ButtonStyles[FShowButtons] or EditStyles[FReadOnly] or HideSelections[FHideSelection] or DragStyles[DragMode] or RTLStyles[UseRightToLeftReading] or ToolTipStyles[FToolTips] or AutoExpandStyles[FAutoExpand] or HotTrackStyles[FHotTrack] or RowSelectStyles[FRowSelect]; if Ctl3D and NewStyleControls and (FBorderStyle = bsSingle) then begin Style := Style and not WS_BORDER; ExStyle := Params.ExStyle or WS_EX_CLIENTEDGE; end; WindowClass.style := WindowClass.style and not (CS_HREDRAW or CS_VREDRAW); end; end;其实TButton,TEdit也是这样实现的,Delphi没有绘制过程
你可以定义节点的 OnCustomDrawItem 事件,实现节点绘制方面的特殊要求。
看看TCustomTreeView的CreateParam方法如何作的就知道了
VCL代码
procedure TCustomTreeView.CreateParams(var Params: TCreateParams);
const
BorderStyles: array[TBorderStyle] of DWORD = (0, WS_BORDER);
LineStyles: array[Boolean] of DWORD = (0, TVS_HASLINES);
RootStyles: array[Boolean] of DWORD = (0, TVS_LINESATROOT);
ButtonStyles: array[Boolean] of DWORD = (0, TVS_HASBUTTONS);
EditStyles: array[Boolean] of DWORD = (TVS_EDITLABELS, 0);
HideSelections: array[Boolean] of DWORD = (TVS_SHOWSELALWAYS, 0);
DragStyles: array[TDragMode] of DWORD = (TVS_DISABLEDRAGDROP, 0);
RTLStyles: array[Boolean] of DWORD = (0, TVS_RTLREADING);
ToolTipStyles: array[Boolean] of DWORD = (TVS_NOTOOLTIPS, 0);
AutoExpandStyles: array[Boolean] of DWORD = (0, TVS_SINGLEEXPAND);
HotTrackStyles: array[Boolean] of DWORD = (0, TVS_TRACKSELECT);
RowSelectStyles: array[Boolean] of DWORD = (0, TVS_FULLROWSELECT);
begin
//调用Windows方法来创建一个TreeView组件,其它的绘制动作其实也是Windows实现的
InitCommonControl(ICC_TREEVIEW_CLASSES);
inherited CreateParams(Params);
CreateSubClass(Params, WC_TREEVIEW);
with Params do
begin
Style := Style or LineStyles[FShowLines] or BorderStyles[FBorderStyle] or
RootStyles[FShowRoot] or ButtonStyles[FShowButtons] or
EditStyles[FReadOnly] or HideSelections[FHideSelection] or
DragStyles[DragMode] or RTLStyles[UseRightToLeftReading] or
ToolTipStyles[FToolTips] or AutoExpandStyles[FAutoExpand] or
HotTrackStyles[FHotTrack] or RowSelectStyles[FRowSelect];
if Ctl3D and NewStyleControls and (FBorderStyle = bsSingle) then
begin
Style := Style and not WS_BORDER;
ExStyle := Params.ExStyle or WS_EX_CLIENTEDGE;
end;
WindowClass.style := WindowClass.style and not (CS_HREDRAW or CS_VREDRAW);
end;
end;其实TButton,TEdit也是这样实现的,Delphi没有绘制过程
procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
//ListBox.OnDrawItem事件
var
ImgRect, MemoRect: TRect;
Cnvs: TCanvas;
B: TBitmap;
S, Memo: string;
iPos: Integer;
begin
Cnvs := ListBox1.Canvas;
ImgRect := Rect;
ImgRect.Right := ImgRect.Left + (ImgRect.Bottom - ImgRect.Top); //画图区域 if odSelected in State then
begin
Cnvs.Brush.Color := clSkyBlue;
Cnvs.Pen.Color := clNavy;
end
else
begin
Cnvs.Brush.Color := ListBox1.Color;
Cnvs.Pen.Color := ListBox1.Color;
end;
Cnvs.Brush.Style := bsSolid;
Cnvs.Rectangle(Rect); Cnvs.Brush.Style := bsClear;
if ImageList.Count > 0 then
begin
B := TBitmap.Create;
try
ImageList.GetBitmap(0, B);
B.PixelFormat := pf24bit;
B.Transparent := True;
InflateRect(ImgRect, -2, -2);
Cnvs.StretchDraw(ImgRect, B);
finally
B.Free;
end;
end; Rect.Left := ImgRect.Right;
MemoRect := Rect;
MemoRect.Top := (Rect.Top + Rect.Bottom) div 2;
Rect.Bottom := MemoRect.Top;
S := ListBox1.Items[Index]; iPos := Pos(#9, S);
Memo := Copy(S, iPos + 1, Length(S)); //提取备注
S := Copy(S, 1, iPos - 1); //提取名称
Cnvs.Font.Assign(ListBox1.Font);
DrawText(Cnvs.Handle, PChar(S), -1,
Rect, DT_SingleLine or DT_VCENTER); //垂直、水平居中 画名字 Cnvs.Font.Color := clGrayText;
DrawText(Cnvs.Handle, PChar(Memo), -1,
MemoRect, DT_SingleLine or DT_VCENTER); //垂直、水平居中 画名字
end;procedure TForm1.FormCreate(Sender: TObject);
begin
ListBox1.Style := lbOwnerDrawVariable; //自己画每一项,项高度可调
ListBox1.ItemHeight := 30;
ListBox1.Clear;
//名称+#9+备注
ListBox1.Items.Add('yao518'#9 + '一剑飘雪');
ListBox1.Items.Add('blazingfire'#9 + '...该充电了...');
ListBox1.Items.Add('yao518'#9 + '一剑飘雪');
ListBox1.Items.Add('blazingfire'#9 + '...该充电了...');
end;