各位大侠:
   小弟想在双击listbox中某项时,修改该项的颜色,而其他项的颜色保持不变,如何做到?多谢指教了!

解决方案 »

  1.   

    //
    // 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.
    不知对不对.写的不好,接受大家的批评.
      

  2.   

    对不起漏写了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;
      

  3.   

    // 不需要设置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;