能不能改变STRINGGRID等网格控件中网格线的颜色?(100分)

  • 主题发起人 主题发起人 jinghj
  • 开始时间 开始时间
可以的,摘自Grid.pas
加一个属性LineColor,重写下面的过程

procedure TCustomGrid.Paint;
var
LineColor: TColor;
...
begin
...
if ColorToRGB(Color) = clSilver then LineColor := clGray;
DrawLines(goFixedHorzLine in Options, goFixedVertLine in Options,
0, 0, [0, 0, Horz.FixedBoundary, Vert.FixedBoundary], clBlack, FixedColor);
DrawLines(goFixedHorzLine in Options, goFixedVertLine in Options,
LeftCol, 0, [Horz.FixedBoundary, 0, Horz.GridBoundary,
Vert.FixedBoundary], clBlack, FixedColor);
DrawLines(goFixedHorzLine in Options, goFixedVertLine in Options,
0, TopRow, [0, Vert.FixedBoundary, Horz.FixedBoundary,
Vert.GridBoundary], clBlack, FixedColor);
DrawLines(goHorzLine in Options, goVertLine in Options, LeftCol,
TopRow, [Horz.FixedBoundary, Vert.FixedBoundary, Horz.GridBoundary,
Vert.GridBoundary], LineColor, Color);

end;
 
重写控件:
procedure DrawCell(ACol, ARow: Longint; ARect: TRect; AState: TGridDrawState); override;
或响应 OnDrawCell 事件:
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
begin
with StringGrid1 do
begin
Canvas.Pen.Color := clRed;
Canvas.MoveTo(Rect.Left - 1, Rect.Top);
Canvas.LineTo(Rect.Left - 1, Rect.Bottom);
end;
end;

 
直接在PAINT中写也可以吧!
 
在自定义事件中自己画。
 
procedure TfrmOInpt.sgrdTmpDrawCell(Sender: TObject; Col, Row: Integer;
Rect: TRect; State: TGridDrawState);
begin
if not((col=0)or(row=0)) then
begin
sgrdTmp.Canvas.brush.Color:=clred;
rect.Left:=rect.Left-1;
rect.Top:=rect.Top-1;
rect.Right:=rect.Right+1;
rect.Bottom:=rect.Bottom+1;
sgrdTmp.Canvas.FrameRect(rect);
end;
end;
 
多人接受答案了。
 
后退
顶部