我只是尝试使用delphi XE,在此之前我一直是Delphi7的忠实粉丝。
我看到新的 dbgrid 允许使用主题和渐变样式。
我正在使用渐变并设置行选择,它具有用于列标题的渐变开始和结束的属性。
但是设置选定颜色
的属性在哪里?
这很奇怪,因为颜色不匹配,选定的颜色
始终是蓝色渐变。
我可以用customdraw
来做到这一点,我只是想知道是否有办法在不自定义绘图的情况下更改它。
最佳答案
所选颜色来自操作系统。
在那里它被编码为clHighlight
。
您不能这样更改它,但您可以子类化 dbgrid 并重写 DrawCell 方法。
或者更容易添加一个 onDrawCell
事件处理程序。
procedure TForm1.DBGrid1DrawCell(Sender: TObject, const Rect: TRect; Field: TField; State: TGridDrawState);
var
index: Integer;
begin
if not(gdSelected in State) then DefaultDrawCell(Rect, Field, State)
else begin
index := ARow * DBGrid1.ColCount + ACol;
DBGrid1.Canvas.Brush.Color := clYellow; <<-- some color
DBGrid1.Canvas.FillRect(Rect);
if (gdFocused in State) then begin
DBGrid1.Canvas.DrawFocusRect(Rect);
end;
ImageList1.Draw(DBGrid1.Canvas,Rect.Left,Rect.Top,index, True);
end;
关于Delphi TDBGrid 当样式为gdsGradient时如何更改选定的颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7536457/