2024年3月21日发(作者:)
在ListBox中高亮度显示文本
要实现这个例子,你需要在Form上加入一个TListBox和TEditBox构件。
----
在Form的PRIVATE部分输入:
HighlightText : string ;
TextLength : integer ;
Foreground : TColor ;
Background : TColor ;
----
以下是Form1,ListBox1和EditBox1的事件代码:
procedure x1DrawItem(Control: TWinControl; Index:
Integer;
Rect: TRect; State: TOwnerDrawState);
var
Location : integer ;
s : string ;
Text : string;
ax : integer ;
TextWidth : integer ;
OldBrushColor : TColor ;
OldFontColor : TColor ;
const
HighlightColor = clRed ;
begin
with (Control as TListBox) do begin
ct(Rect) ;
Location := Pos(HighlightText, Items[Index]) ;
if Location > 0 then begin
TextWidth := dth(HighlightText) ;
s := Items[Index] ;
ax := 0 ;
while Location > 0 do begin
Text := Copy(s, 1, Location - 1) ;
s := Copy(s, Location + TextLength, Length(s)) ;
t(ax, , Text) ;
Inc(ax, dth(Text)) ;
OldBrushColor := ;
OldFontColor := ;
:= Background ;
:= Foreground ;
t(ax, , HighlightText) ;
:= OldBrushColor ;
:= OldFontColor ;
Inc(ax, TextWidth) ;
Location := Pos(HighlightText, s) ;
end ;
t(ax, , s) ;
end
else
t(, , Items[Index]) ;
end ;
end;
procedure 1Change(Sender: TObject);
begin//如果你想查找文本...
HighlightText := ;
TextLength := Length() ;
h ;
end;
procedure eate(Sender: TObject);
begin
;
omFile('c:');//你可以改变这里
:=lbOwnerDrawFixed;
HighlightText := ;
TextLength := 4 ;
Background := clRed ;
Foreground := clWhite ;
end;


发布评论