在线等待,急急急!---如何修改listbox中指定项的颜色???? 各位大侠: 小弟想在双击listbox中某项时,修改该项的颜色,而其他项的颜色保持不变,如何做到?多谢指教了! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 //// ListBox1.Style := lbOwnerDrawVariable or lbOwnerDrawFixedunit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TForm1 = class(TForm) ListBox1: TListBox; procedure ListBox1DrawItem(Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState); procedure ListBox1DblClick(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1; G_Index : Integer = -1;implementation{$R *.dfm}procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState);begin else ListBox1.Canvas.Brush.Color := clWhite; ListBox1.Canvas.FillRect(Rect); ListBox1.Canvas.TextOut(Rect.Left,Rect.Top,ListBox1.Items[Index]);end;procedure TForm1.ListBox1DblClick(Sender: TObject);begin G_Index := ListBox1.ItemIndex; ListBox1.Invalidate;end;end.不知对不对.写的不好,接受大家的批评. 对不起漏写了procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer; Rect: TRect; State: TOwnerDrawState);begin if Index = G_Index then ListBox1.Canvas.Brush.Color := clYellow else ListBox1.Canvas.Brush.Color := clWhite; ListBox1.Canvas.FillRect(Rect); ListBox1.Canvas.TextOut(Rect.Left,Rect.Top,ListBox1.Items[Index]);end; // 不需要设置ListBox.Style属性,直接改变选中Item的颜色// 只要在ListBox的OnClick事件中写代码。procedure TForm1.ListBox1Click(Sender: TObject);var Index: Integer; Point: TPoint; DrawRect: TRect;begin GetCursorPos(Point); Point := ListBox1.ScreenToClient(Point); Index := ListBox1.ItemAtPos(Point, True); if Index <> -1 then begin DrawRect.Left := 0 + 1; DrawRect.Top := Index * ListBox1.ItemHeight + 1; DrawRect.Bottom := DrawRect.Top + ListBox1.ItemHeight - 2; DrawRect.Right := ListBox1.ClientWidth - 1; ListBox1.Canvas.Brush.Color := clRed; // 设置选中的Item背景为红色 ListBox1.Canvas.FillRect(DrawRect); ListBox1.Canvas.Font.Color := clBlue; // 设置选中的Item文字为蓝色 ListBox1.Canvas.Brush.Style := bsClear; ListBox1.Canvas.TextOut(2, ListBox1.ItemHeight * Index, ListBox1.Items.Strings[Index]); end;end; 急!如何求一个集合的真子集 delphi调用vc写的dll 关于Delphi对象创建的迷惑! 程序怎么出现这么奇怪的错误,悬赏50分. 问TFilestream的输入? 求助!谁能解决这个小问题?或者谁的计算软件中有这个功能。 时间计算的问题 紧急求教:ScktSrvr(Borland Socket Server)设置问题????????? 一段E的翻译成delphi的 貌似有问题,求大牛解答 真心求教:为什么Delphi的DDE不能想VB的DDE那样发生数据的实时改变! 急寻高手:求教排序方面的问题 请问高手!!
// ListBox1.Style := lbOwnerDrawVariable or lbOwnerDrawFixedunit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
ListBox1: TListBox;
procedure ListBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
procedure ListBox1DblClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;
G_Index : Integer = -1;
implementation{$R *.dfm}procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
Rect: TRect; State: TOwnerDrawState);
begin
else ListBox1.Canvas.Brush.Color := clWhite;
ListBox1.Canvas.FillRect(Rect);
ListBox1.Canvas.TextOut(Rect.Left,Rect.Top,ListBox1.Items[Index]);
end;procedure TForm1.ListBox1DblClick(Sender: TObject);
begin
G_Index := ListBox1.ItemIndex;
ListBox1.Invalidate;
end;end.
不知对不对.写的不好,接受大家的批评.
Rect: TRect; State: TOwnerDrawState);
begin
if Index = G_Index then
ListBox1.Canvas.Brush.Color := clYellow
else ListBox1.Canvas.Brush.Color := clWhite;
ListBox1.Canvas.FillRect(Rect);
ListBox1.Canvas.TextOut(Rect.Left,Rect.Top,ListBox1.Items[Index]);
end;
// 只要在ListBox的OnClick事件中写代码。procedure TForm1.ListBox1Click(Sender: TObject);
var
Index: Integer;
Point: TPoint;
DrawRect: TRect;
begin
GetCursorPos(Point);
Point := ListBox1.ScreenToClient(Point);
Index := ListBox1.ItemAtPos(Point, True);
if Index <> -1 then
begin
DrawRect.Left := 0 + 1;
DrawRect.Top := Index * ListBox1.ItemHeight + 1;
DrawRect.Bottom := DrawRect.Top + ListBox1.ItemHeight - 2;
DrawRect.Right := ListBox1.ClientWidth - 1;
ListBox1.Canvas.Brush.Color := clRed; // 设置选中的Item背景为红色
ListBox1.Canvas.FillRect(DrawRect);
ListBox1.Canvas.Font.Color := clBlue; // 设置选中的Item文字为蓝色
ListBox1.Canvas.Brush.Style := bsClear;
ListBox1.Canvas.TextOut(2, ListBox1.ItemHeight * Index, ListBox1.Items.Strings[Index]);
end;
end;