Delphi に TTreeView があり、ノードは 3 つのレベルにあります。
ノード データを使用して、ノード テキスト以外の別のラベルを格納します。
Type
TNodeData = class
ExtraNodeLabel: WideString;
//... other members
end;
ExtraNodeLabel
ノードテキストの前に
これを表示したい OnAdvancedCustomDrawItem イベントがあります。私はこれを達成したい:
- 青いテキストは追加のラベルです。
- 強調表示された項目: 最初の 2 つの単語も追加のラベルです
私がこれまでに得たものは、これです:
問題:
DrawText
/を使用すると、何らかの理由で異なるスタイルのテキストを描画できませんdrawTextW
(Unicode データのために drawtextW が必要です)- もう 1 つの問題は、点線のフォーカス四角形の外側はクリックできないことです。
解決する必要があるもの:
DrawText
/を使用して異なるスタイルでテキストを描画するにはどうすればよいですかDrawtextW
- テキスト全体をクリック可能にするにはどうすればよいですか?
コード:
procedure TMainForm.TntTreeView1AdvancedCustomDrawItem(
Sender: TCustomTreeView; Node: TTreeNode; State: TCustomDrawState;
Stage: TCustomDrawStage; var PaintImages, DefaultDraw: Boolean);
var
txtrect, fullrect : TRect;
DC: HDC;
fs: integer;
fc: TColor;
ExtralabelRect: TRect;
nData: TNodeData;
begin
nData := nil;
if assigned(Node.Data) then begin
nData := TNodeData(Node.Data);
end;
DC := TntTreeView1.canvas.Handle;
txtRect := Node.DisplayRect(True);
fullrect := Node.DisplayRect(False);
if stage = cdPostPaint then begin
TntTreeView1.Canvas.FillRect(txtRect);
if (cdsFocused In State) And (cdsSelected in State) then begin
DrawFocusRect(DC,txtRect);
end;
txtRect.Left := txtRect.Left + 1;
txtRect.Top := txtRect.Top + 1;
txtRect.Right := txtRect.Right - 1;
txtRect.Bottom := txtRect.Bottom - 1;
ExtralabelRect := txtRect;
fs := TntTreeView1.Canvas.Font.size;
fc := TntTreeView1.Canvas.Font.Color;
if (nData <> nil) And (nData.ExtraNodeLabel <> '') then begin
TntTreeView1.Canvas.Font.Size := 7;
TntTreeView1.Canvas.Font.color := clBlue;
DrawTextW(
DC,
PWideChar(nData.ExtraNodeLabel),
Length(nData.ExtraNodeLabel),
ExtraLabelRect,
DT_LEFT or DT_CALCRECT or DT_VCENTER
);
DrawTextW(
DC,
PWideChar(nData.ExtraNodeLabel),
Length(nData.ExtraNodeLabel),
ExtraLabelRect,
DT_LEFT or DT_VCENTER
);
txtRect.right := txtRect.Right + ExtraLabelRect.Right + 5;
txtRect.Left := ExtraLabelRect.Right + 5;
end;
TntTreeView1.Canvas.Font.Size := fs;
TntTreeView1.Canvas.Font.color := fc;
DrawTextW(
DC,
PWideChar((Node as TTntTreeNode).Text),
-1,
txtRect,
DT_LEFT or DT_VCENTER
);
end;
end;