如何改变tListBox中某一行的底色呀?是不是应在DrawItem事件中写代码呀,该如何写呢?

解决方案 »

  1.   

    procedure TForm1.ListBox1DrawItem(Control: TWinControl; Index: Integer;
      Rect: TRect; State: TOwnerDrawState);
    var
      dc:HDC;
      br:HBRUSH;
      str:String;
      bk:TColor;
    begin
      if (Index Mod 2)=0 then
        bk:=RGB(0,255,0)
      else
        bk:=RGB(0,0,255);
      dc:=GetDC(Control.Handle);
      br:=CreateSolidBrush(bk);
      SetBKMode(dc,TRANSPARENT);
      SetTextColor(dc,clWhite);
      str:=TListBox(Control).Items.Strings[Index];
      try
        FillRect(dc,Rect,br);
        DrawText(dc,Pchar(str),Length(str),Rect,DT_SINGLELINE or DT_VCENTER);
      finally
        DeleteObject(br);
        ReleaseDC(Control.Handle,dc);
      end;end;