procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState); begin if Pos('OK',(Control as TComboBox).Items[Index]) > 0 then (Control as TComboBox).Canvas.Font.Color := clRed else (Control as TComboBox).Canvas.Font.Color := clBlack; (Control as TComboBox).Canvas.TextOut(Rect.Left,Rect.Top,(Control as TComboBox).Items[Index]); end;
procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState); begin with Control as TListbox do begin Canvas.FillRect(Rect);//填充以前画的内容 Canvas.Font.Color := clRed ; //显示当前Item的文字 Canvas.TextOut(0, Rect.Top, bsSkinListBox1.Items[Index]);//处理状态为激活或选中时的样子 if (odFocused in State) and (odSelected in State) then Canvas.DrawFocusRect(Rect); // Canvas.ClipRect); // end; end;
忘记改了,bsSkinListBox1换成当前控件名
listbox的style属性设置为lbOwnerDrawVariableprocedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState); var s:string; i:Integer; begin if Index=0 then begin self.ListBox1.Canvas.FillRect(Rect); self.ListBox1.Canvas.Font.Color:=clRed; self.ListBox1.Canvas.TextOut(rect.Left+1,rect.Top+1,'你好'); i:=self.ListBox1.Canvas.TextWidth('你好'); self.ListBox1.Canvas.Font.Color:=clblue; self.ListBox1.Canvas.TextOut(rect.Left+2+i,rect.Top+1,'我是×××'); self.ListBox1.Canvas.DrawFocusRect(Rect); end; end;
procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState); var s,s1,s2:string; i:Integer; left:Integer; begin left:=0; s:=self.ListBox1.Items.Strings[index]; while s<>'' do begin i:=Pos('你好',s); if i=0 then s:='' else begin s1:=Copy(S,1,i-1); s:=Copy(s,i+length('你好'),Length(s)-i-length('你好')+1); self.ListBox1.Canvas.FillRect(Rect); self.ListBox1.Canvas.Font.Color:=clBlack; self.ListBox1.Canvas.TextOut(Left+1,Rect.Top+1,s1); i:=self.ListBox1.Canvas.TextWidth(s1); left:=left+i+1; self.ListBox1.Canvas.Font.Color:=clRed; self.ListBox1.Canvas.TextOut(Left+1,rect.Top+1,'你好'); i:=self.ListBox1.Canvas.TextWidth('你好'); left:=left+i+1; self.ListBox1.Canvas.Font.Color:=clBlack; self.ListBox1.Canvas.TextOut(Left+1,Rect.Top+1,s); self.ListBox1.Canvas.DrawFocusRect(Rect); end; end; end; 这个可能更贴近你的要求
Rect: TRect; State: TOwnerDrawState);
begin
if Pos('OK',(Control as TComboBox).Items[Index]) > 0 then
(Control as TComboBox).Canvas.Font.Color := clRed
else
(Control as TComboBox).Canvas.Font.Color := clBlack; (Control as TComboBox).Canvas.TextOut(Rect.Left,Rect.Top,(Control as TComboBox).Items[Index]);
end;
procedure TForm1.ComboBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
begin
with Control as TListbox do
begin
Canvas.FillRect(Rect);//填充以前画的内容
Canvas.Font.Color := clRed ;
//显示当前Item的文字
Canvas.TextOut(0, Rect.Top,
bsSkinListBox1.Items[Index]);//处理状态为激活或选中时的样子
if (odFocused in State) and (odSelected in State) then
Canvas.DrawFocusRect(Rect); // Canvas.ClipRect); //
end;
end;
Rect: TRect; State: TOwnerDrawState);
var
s:string;
i:Integer;
begin
if Index=0 then
begin
self.ListBox1.Canvas.FillRect(Rect);
self.ListBox1.Canvas.Font.Color:=clRed;
self.ListBox1.Canvas.TextOut(rect.Left+1,rect.Top+1,'你好');
i:=self.ListBox1.Canvas.TextWidth('你好');
self.ListBox1.Canvas.Font.Color:=clblue;
self.ListBox1.Canvas.TextOut(rect.Left+2+i,rect.Top+1,'我是×××');
self.ListBox1.Canvas.DrawFocusRect(Rect);
end;
end;
Rect: TRect; State: TOwnerDrawState);
var
s,s1,s2:string;
i:Integer;
left:Integer;
begin
left:=0;
s:=self.ListBox1.Items.Strings[index];
while s<>'' do
begin
i:=Pos('你好',s);
if i=0 then
s:=''
else
begin
s1:=Copy(S,1,i-1);
s:=Copy(s,i+length('你好'),Length(s)-i-length('你好')+1);
self.ListBox1.Canvas.FillRect(Rect);
self.ListBox1.Canvas.Font.Color:=clBlack;
self.ListBox1.Canvas.TextOut(Left+1,Rect.Top+1,s1);
i:=self.ListBox1.Canvas.TextWidth(s1);
left:=left+i+1;
self.ListBox1.Canvas.Font.Color:=clRed;
self.ListBox1.Canvas.TextOut(Left+1,rect.Top+1,'你好');
i:=self.ListBox1.Canvas.TextWidth('你好');
left:=left+i+1;
self.ListBox1.Canvas.Font.Color:=clBlack;
self.ListBox1.Canvas.TextOut(Left+1,Rect.Top+1,s);
self.ListBox1.Canvas.DrawFocusRect(Rect);
end;
end;
end;
这个可能更贴近你的要求