在TListView中,系统默认选中某一项时,背景色是蓝色,字为白色,
我想把它的颜色改掉,但总是不行
我在CustomDrawItem事件里写
  if Item.Selected then
  begin
    lvTests.Canvas.Brush.Color := clYellow
    lvTests.Canvas.Font.Color := clBlack;
  end
  else begin
    lvTests.Canvas.Brush.Color := clWindow;
    lvTests.Canvas.Font.Color := clBlack;
  end;
未选中的行可以更换颜色,而选项中的行的颜色始终为蓝色.
请大家帮忙

解决方案 »

  1.   

    procedure TForm1.TreeView1CustomDrawItem(Sender: TCustomTreeView;
      Node: TTreeNode; State: TCustomDrawState; var DefaultDraw: Boolean);
    begin
      if (cdsSelected in State) then
      begin
        Sender.Canvas.Brush.Color :=clred;
        Sender.Canvas.Font.Color :=clBlue;
      end;
    end;
    刚写的,调试成功D7,看看是不是你要的??
      

  2.   

    procedure TfrmBaseForm.ReadOnlyListViewCustomDrawItem(
      Sender: TCustomListView; Item: TListItem; State: TCustomDrawState;
      var DefaultDraw: Boolean);
    var
      R: TRect;
      S: String;
      x, y, i, j, m , n: Integer;
      B: TBitmap;
    begin
      if Item = nil then Exit;
      with Sender as TListView do
        //只对选中的条目进行重画,其余用默认的
        if (ViewStyle = vsReport) and Item.Selected and (Columns.Count > 0) then
        begin
          B := TBitmap.Create;
          try
            Canvas.Brush.Color := TD_COLOR_LISTVIEW_SELECTED;
            Canvas.Font.Color := TD_COLOR_TEXT_NORMAL;
            DefaultDraw := False;
            //Canvas.Font.Style := [fsBold];
            R.Top := Item.Position.Y;
            R.Left := Item.Position.X;
            //换成StateImages
            //if (SmallImages <> nil) and (SmallImages.Count > 0) then
            //begin
            //  SmallImages.GetBitmap(0, B);
            //  R.Left := R.Left + B.Width;
            //end;
            R.Bottom := R.Top + 20;
            //最后一个Item高度减4, why?
            if Item.Index = Items.Count - 1 then
              R.Bottom := R.Bottom - 4;
            //画左边的图标
            R.Right := 0;
            for i := 0 to Columns.Count - 1 do
              R.Right := R.Right + Columns[i].Width;
            //if (SmallImages <> nil) and (Item.ImageIndex <> -1) then
            if (StateImages <> nil) and (Item.StateIndex <> -1) then
            begin
              //SmallImages.GetBitmap(Item.ImageIndex, B);
              StateImages.GetBitmap(Item.StateIndex, B);
              Canvas.Draw(2, R.Top, B);
            end;
            //填充整个Item的颜色
            Canvas.FillRect(R);
            x := R.Left + 2;
            y := R.Top + 2;
            //画Item的Caption
            S := Item.Caption;
            if S <> '' then Canvas.TextOut(x , y, S);
            //画Item的所有SubItem
            j := Columns[0].Width;
            for i := 1 to Columns.Count - 1 do
            begin
              m := Columns[i].Width;
              n := 0;
              //画SubItem的图
              if (SmallImages <> nil) and (Item.SubItemImages[i - 1] <> -1) then
              begin
                SmallImages.GetBitmap(Item.SubItemImages[i - 1], B);
                Canvas.Draw(j, R.Top, B);
                n := B.Width;
              end;
              S := Item.SubItems[i - 1];
              if S <> '' then
              begin
                //居左
                if Columns[i].Alignment = taLeftJustify then
                begin
                  x := j + n + 6;
                end
                //居中
                else if Columns[i].Alignment = taCenter then
                begin
                  x := j + n + (m - Canvas.TextWidth(S)) div 2
                end
                //居右
                else begin
                  x := j + n + m - Canvas.TextWidth(S)- 6;
                end;
                Canvas.TextOut(x, y, S);
              end;
              j := j + m;
            end;
          finally
            B.Free;
          end;
        end
        else begin
          Canvas.Brush.Color := TD_COLOR_NORMAL;
          Canvas.Font.Color := TD_COLOR_TEXT_NORMAL;
          DefaultDraw := True;
        end;
    end;
      

  3.   

    dxxb(天下无雪) 说的是对的