【delphi开源代码栏目提醒】:网学会员为广大网友收集整理了,CustomDrawTreeView.pas,希望对大家有所帮助!
unit CustomDrawTreeView;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls, ExtDlgs, StdCtrls, ExtCtrls, ColorGrd, ImgList, Menus;
type
TCustomDrawForm = class(TForm)
TV: TTreeView;
ImageList: TImageList;
MainMenu1: TMainMenu;
Drawing1: TMenuItem;
Font1: TMenuItem;
Background1: TMenuItem;
Color1: TMenuItem;
Bitmap1: TMenuItem;
DefaultDrawing1: TMenuItem;
OnCustomDraw1: TMenuItem;
OnCustomDrawItem1: TMenuItem;
BrushStyle1: TMenuItem;
Solid1: TMenuItem;
Clear1: TMenuItem;
Horizontal1: TMenuItem;
Vertical1: TMenuItem;
FDiagonal1: TMenuItem;
BDiagonal1: TMenuItem;
Cross1: TMenuItem;
DiagCross1: TMenuItem;
File1: TMenuItem;
Exit1: TMenuItem;
N2: TMenuItem;
TVFontDialog: TFontDialog;
Tile1: TMenuItem;
Stretch1: TMenuItem;
None1: TMenuItem;
Selection1: TMenuItem;
SelectedFontDialog: TFontDialog;
BkgColorDialog: TColorDialog;
SelBkgColorDialog: TColorDialog;
SelectionBackground1: TMenuItem;
ButtonColor1: TMenuItem;
ButtonSize1: TMenuItem;
ButtonColorDialog: TColorDialog;
Image1: TImage;
TreeView1: TMenuItem;
Color2: TMenuItem;
TVColorDialog: TColorDialog;
CustomDraw1: TMenuItem;
Font2: TMenuItem;
procedure FormCreate(Sender: TObject);
procedure TVCustomDraw(Sender: TCustomTreeView; const ARect: TRect;
var DefaultDraw: Boolean);
procedure TVCustomDrawItem(Sender: TCustomTreeView; Node: TTreeNode;
State: TCustomDrawState; var DefaultDraw: Boolean);
procedure TVGetImageIndex(Sender: TObject; Node: TTreeNode);
procedure TVGetSelectedIndex(Sender: TObject; Node: TTreeNode);
procedure Exit1Click(Sender: TObject);
procedure Selection1Click(Sender: TObject);
procedure Color1Click(Sender: TObject);
procedure SelectionBackground1Click(Sender: TObject);
procedure Solid1Click(Sender: TObject);
procedure None1Click(Sender: TObject);
procedure OnCustomDraw1Click(Sender: TObject);
procedure OnCustomDrawItem1Click(Sender: TObject);
procedure TVExpanded(Sender: TObject; Node: TTreeNode);
procedure ButtonColor1Click(Sender: TObject);
procedure ButtonSize1Click(Sender: TObject);
procedure Drawing1Click(Sender: TObject);
procedure Color2Click(Sender: TObject);
procedure CustomDraw1Click(Sender: TObject);
procedure Font2Click(Sender: TObject);
private
FButtonSize: Integer;
FDefaultDraw,
FDefaultDrawItem: Boolean;
FBackgroundColor: TColor;
FBrushStyle: TBrushStyle;
procedure DrawButton(ARect: TRect; Node: TTreeNode);
procedure DrawImage(NodeRect: TRect; ImageIndex: Integer);
procedure SetCustomDraw(Value: Boolean);
{ Private declarations }
public
{ Public declarations }
end;
var
CustomDrawForm: TCustomDrawForm;
implementation
{$R *.dfm}
procedure TCustomDrawForm.FormCreate(Sender: TObject);
begin
FBackgroundColor := clWindow;
FDefaultDraw := True;
FDefaultDrawItem := True;
FBrushStyle := bsSolid;
FButtonSize := 5;
BkgColorDialog.Color := clWindow;
SelBkgColorDialog.Color := clHighlight;
TVFontDialog.Font.Assign(TV.Font);
SelectedFontDialog.Font.Assign(TV.Font);
SelectedFontDialog.Font.Color := clHighlightText;
SelBkgColorDialog.Color := clHighlight;
TVColorDialog.Color := TV.Color;
end;
procedure TCustomDrawForm.TVCustomDraw(Sender: TCustomTreeView; const ARect: TRect;
var DefaultDraw: Boolean);
begin
//This event should be used to draw any background colors or images.
//ARect represents the entire client area of the TreeView.
//Use the TreeView's canvas to do the drawing.
//Note that drawing a background bitmap is not really supported by CustomDraw,
//so scrolling can get messy. Best to subclass the TreeView and handle scrolling
//messages.
with TV.Canvas do
begin
if None1.Checked then //no picture
begin
Brush.Color := BkgColorDialog.Color;
Brush.Style := FBrushStyle;
FillRect(ARect);
end else
if Tile1.Checked then //tile bitmap
begin
Brush.Bitmap := Image1.Picture.Bitmap;
FillRect(ARect);
end else //Stretch across the canvas.
StretchDraw(ARect, Image1.Picture.Bitmap);
end;
DefaultDraw := FDefaultDraw;
//setting DefaultDraw to false here prevents all calls to OnCustomDrawItem.
end;
procedure TCustomDrawForm.DrawButton(ARect: TRect; Node: TTreeNode);
var
cx, cy: Integer;
begin
cx := ARect.Left + TV.Indent div 2;
cy := ARect.Top + (ARect.Bottom - ARect.Top) div 2;
with TV.Canvas do
begin
Pen.Color := ButtonColorDialog.Color;
//draw horizontal line.
if Node.HasChildren then
begin
PenPos := Point(cx+FButtonSize, cy);
LineTo(ARect.Left + TV.Indent + FButtonSize, cy);
end else
begin
PenPos := Point(cx, cy);
LineTo(ARect.Left + TV.Indent + FButtonSize, cy);
end;
//draw half vertical line, top portion.
PenPos := Point(cx, cy);
LineTo(cx, ARect.Top-1);
if ((Node.GetNextVisible <> nil) and (Node.GetNextVisible.Level = Node.Level))
or (Node.GetNextSibling <> nil) then
//draw bottom portion of half vertical line.
begin
PenPos := Point(cx, cy);
LineTo(cx, ARect.Bottom+1);
end;
if Node.HasChildren then
begin
//Let's try a circular button instead
Ellipse(cx-FButtonSize, cy-FButtonSize, cx+FButtonSize, cy+FButtonSize);
//draw the horizontal indicator.
PenPos := Point(cx-FButtonSize+2, cy);
LineTo(cx+FButtonSize-2, cy);
//draw the vertical indicator if the node is collapsed
if not Node.Expanded then
begin
PenPos := Point(cx, cy-FButtonSize+2);
LineTo(cx, cy+FButtonSize-2);
end;
end;
//now connect vertical lines of higher level nodes.
Node := Node.Parent;
while Node <> nil do
begin
cx := cx - TV.Indent;
if Node.GetNextSibling <> nil then
begin
PenPos := Point(cx, ARect.Top);
LineTo(cx, ARect.Bottom);
end;
Node := Node.Parent;
end;
end;
end;
procedure TCustomDrawForm.DrawImage(NodeRect: TRect; ImageIndex: Integer);
var
cy: Integer;
begin
cy := NodeRect.Top + (NodeRect.Bottom - NodeRect.Top) div 2;
//center image in NodeRect.
ImageList.Draw(TV.Canvas, NodeRect.Left, cy - TV.Images.Height div 2,
ImageIndex, True);
end;
procedure TCustomDrawForm.TVCustomDrawItem(Sender: TCustomTreeView; Node: TTreeNode;
State: TCustomDrawState; var DefaultDraw: Boolean);
var
NodeRect: TRect;
begin
with TV.Canvas do
begin
//If DefaultDraw it is true, any of the node's font properties can be
//changed. Note also that when DefaultDraw = True, Windows draws the
//buttons and ignores our font background colors, using instead the
//TreeView's Color property.
if cdsSelected in State then
begin
Font.Assign(SelectedFontDialog.Font);
Brush.Color := SelBkgColorDialog.Color;
end;
DefaultDraw := FDefaultDrawItem;
//DefaultDraw = False means you have to handle all the item drawing yourself,
//including the buttons, lines, images, and text.
if not DefaultDraw then
begin
//draw the selection rect.
if cdsSelect
上一篇:
custom.pas
下一篇:
应用数学学报编辑部联系方式