在 TListView 中突出显示单词 Delphi 7
Highlight word in TListView Delphi 7
我正在尝试突出显示 TListView 中的一个词,但我无法让它工作。我的第一次尝试是突出显示每一行的第一个字母,但没有用。 TListView 不会显示任何内容。这是我的代码:
procedure TfrmMain.lvMainDrawItem(Sender: TCustomListView; Item: TListItem; Rect: TRect; State: TOwnerDrawState);
var
r : TRect;
c : TCanvas;
begin
r := Item.DisplayRect(drBounds);
c := Sender.Canvas;
c.Brush.Color := clWindow;
c.Font.Color := clWindowText;
c.TextRect(r, 1, 1, 'a');
end;
我的代码基于 但它是用 XE10.4 编写的。我正在使用 delphi 7. 如何在 TListView 中突出显示每一行的 word/first 字母?
您正在项目的边界矩形之外绘图,这就是您看不到任何东西的原因。您为 TextRect()
指定的 X,Y 坐标是相对于 ListView 客户区的 top/left 角,但被指定的 TRect
.
裁剪
试试这个:
c.TextRect(r, r.Left+1, r.Top+1, 'a');
这是我的工作代码:
设置和填充列表视图
procedure TForm1.Button1Click(Sender: TObject);
begin
ListView1.Clear;
ListView1.ViewStyle := vsReport;
ListView1.RowSelect := True;
ListView1.Items.Add.Caption := 'banana';
ListView1.Items.Add.Caption := 'apple and banana';
ListView1.Items.Add.Caption := 'orange apple and banana';
ListView1.Items.Add.Caption := 'banana and orange';
ListView1.Items.Add.Caption := 'banana orange and apple';
ListView1.Items.Add.Caption := 'appleXandXbanana';
ListView1.Items.Add.Caption := 'orangeXappleXand banana';
ListView1.Items.Add.Caption := 'bananaXandXorange';
ListView1.Items.Add.Caption := 'bananaXorangeXandXapple';
end;
绘画
procedure TForm1.ListView1DrawItem(Sender: TCustomListView;Item: TListItem; Rect: TRect; State: TOwnerDrawState);
var
r : TRect;
c : TCanvas;
sText : string;
sTemp : string;
nPos : Integer;
nLen : Integer;
lv : TListView;
begin
if item = nil then
begin
Exit;
end;
r := Item.DisplayRect(drBounds);
c := Sender.Canvas;
lv := TListView(Sender);
// fKeyword := 'apple';
sText := Item.Caption;
nLen := Length(fKeyword);
nPos := AnsiPos(fKeyword, sText);
// first part : before match
sTemp := Copy(sText, 1, nPos - 1);
if sTemp <> '' then
begin
c.Brush.Color := clWindow;
c.Font.Color := clWindowText;
c.TextRect(r, r.Left, r.Top, sTemp);
Inc(r.Left, c.TextWidth(sTemp));
end;
// second part: match
sTemp := Copy(sText, nPos, nLen);
if (nPos > 0) and (sTemp <> '') then
begin
c.Brush.Color := clRed;
c.Font.Color := clBlue;
c.TextRect(r, r.Left + 1, r.Top, sTemp);
Inc(r.Left, c.TextWidth(sTemp));
end;
// third part : after match
if nPos = 0 then
begin
sTemp := sText;
end
else
begin
sTemp := Copy(sText, nPos + nLen, Length(sText) - nPos - nLen + 1);
end;
c.Brush.Color := clWindow;
c.Font.Color := clWindowText;
c.TextRect(r, r.Left + 1, r.Top, sTemp);
Inc(r.Left, c.TextWidth(sTemp));
if odFocused in State then
begin
lv.Canvas.Brush.Style := bsSolid;
lv.Canvas.Brush.Color := clBlack;
lv.Canvas.Font.Color := clWhite;
DrawFocusRect(lv.Canvas.Handle, Rect);
end;
end;
我正在尝试突出显示 TListView 中的一个词,但我无法让它工作。我的第一次尝试是突出显示每一行的第一个字母,但没有用。 TListView 不会显示任何内容。这是我的代码:
procedure TfrmMain.lvMainDrawItem(Sender: TCustomListView; Item: TListItem; Rect: TRect; State: TOwnerDrawState);
var
r : TRect;
c : TCanvas;
begin
r := Item.DisplayRect(drBounds);
c := Sender.Canvas;
c.Brush.Color := clWindow;
c.Font.Color := clWindowText;
c.TextRect(r, 1, 1, 'a');
end;
我的代码基于
您正在项目的边界矩形之外绘图,这就是您看不到任何东西的原因。您为 TextRect()
指定的 X,Y 坐标是相对于 ListView 客户区的 top/left 角,但被指定的 TRect
.
试试这个:
c.TextRect(r, r.Left+1, r.Top+1, 'a');
这是我的工作代码:
设置和填充列表视图
procedure TForm1.Button1Click(Sender: TObject);
begin
ListView1.Clear;
ListView1.ViewStyle := vsReport;
ListView1.RowSelect := True;
ListView1.Items.Add.Caption := 'banana';
ListView1.Items.Add.Caption := 'apple and banana';
ListView1.Items.Add.Caption := 'orange apple and banana';
ListView1.Items.Add.Caption := 'banana and orange';
ListView1.Items.Add.Caption := 'banana orange and apple';
ListView1.Items.Add.Caption := 'appleXandXbanana';
ListView1.Items.Add.Caption := 'orangeXappleXand banana';
ListView1.Items.Add.Caption := 'bananaXandXorange';
ListView1.Items.Add.Caption := 'bananaXorangeXandXapple';
end;
绘画
procedure TForm1.ListView1DrawItem(Sender: TCustomListView;Item: TListItem; Rect: TRect; State: TOwnerDrawState);
var
r : TRect;
c : TCanvas;
sText : string;
sTemp : string;
nPos : Integer;
nLen : Integer;
lv : TListView;
begin
if item = nil then
begin
Exit;
end;
r := Item.DisplayRect(drBounds);
c := Sender.Canvas;
lv := TListView(Sender);
// fKeyword := 'apple';
sText := Item.Caption;
nLen := Length(fKeyword);
nPos := AnsiPos(fKeyword, sText);
// first part : before match
sTemp := Copy(sText, 1, nPos - 1);
if sTemp <> '' then
begin
c.Brush.Color := clWindow;
c.Font.Color := clWindowText;
c.TextRect(r, r.Left, r.Top, sTemp);
Inc(r.Left, c.TextWidth(sTemp));
end;
// second part: match
sTemp := Copy(sText, nPos, nLen);
if (nPos > 0) and (sTemp <> '') then
begin
c.Brush.Color := clRed;
c.Font.Color := clBlue;
c.TextRect(r, r.Left + 1, r.Top, sTemp);
Inc(r.Left, c.TextWidth(sTemp));
end;
// third part : after match
if nPos = 0 then
begin
sTemp := sText;
end
else
begin
sTemp := Copy(sText, nPos + nLen, Length(sText) - nPos - nLen + 1);
end;
c.Brush.Color := clWindow;
c.Font.Color := clWindowText;
c.TextRect(r, r.Left + 1, r.Top, sTemp);
Inc(r.Left, c.TextWidth(sTemp));
if odFocused in State then
begin
lv.Canvas.Brush.Style := bsSolid;
lv.Canvas.Brush.Color := clBlack;
lv.Canvas.Font.Color := clWhite;
DrawFocusRect(lv.Canvas.Handle, Rect);
end;
end;