1

我想模拟 TDBGrid 选定行 (dgRowSelect) 的资源管理器主题,而不是那种蓝色。我怎样才能做到这一点?

以下是预期结果的示例:

在此处输入图像描述

4

3 回答 3

1

当您说“模拟”时,我不清楚您打算如何选择选定的行背景颜色,但下面应该在标准 TDBGrid 中绘制它。

procedure TForm1.FormCreate(Sender: TObject);
begin
  DBGrid1.DefaultDrawing := False;
  DBGrid1.Options := DBGrid1.Options + [dgRowSelect];
end;

procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
  Field: TField; State: TGridDrawState);
var
  Grid : TDBGrid;
  BackColor : TColor;
begin
  Grid := Sender as TDBGrid;
  if gdSelected in State then begin
    BackColor := clYellow;  // or whatever
    Grid.Canvas.Brush.Color := BackColor;
    Grid.Canvas.Font.Color := Grid.Font.Color;
  end;
  Grid.Canvas.FillRect(Rect);
  Grid.Canvas.TextOut(Rect.Left, Rect.Top, Field.DisplayText);
end;
于 2013-12-27T16:55:56.550 回答
1

您可以使用 OnDrawColumnCell 事件这是一个简单的示例:

procedure TForm4.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
  if mydataSet.FieldByName('Age').AsInteger > 18 then
    DBGrid1.Canvas.Brush.Color:= clRed;
  DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
end;

希望这可以帮助

于 2013-12-27T16:56:00.613 回答
0

将 TDBGrid 的“SelectedBackColor”更改为您想要的颜色。

于 2014-06-19T16:54:55.083 回答