当 BorderWidth 很大时,如何从 TGridPanel 的单元格中获取真正的 ClientRect?

How can i get real ClientRect from TGridPanel's cell when it have large BorderWidth?

我试图在覆盖 Paint 事件的 TGridPanel 的每个单元格中绘制。我通过 CellRect [Row, Col] 获得每个单元格的 Rect。这一直有效,直到报告边缘。在这种情况下,即使设计时的设计也是错误的:单元格的 'ClientRect' 不对应 CellRect.

的 return

我试图调整从CellRect获得的rect,但是考虑每个rect的位移率非常复杂。在下图中,我有一个边框为 3px 的 TGripanel,每个面板的 AlignwithMargins = true,所有 Margins = 3px.

有没有人遇到过这种情况?

原生油漆: BorderWidth = 3 BorderStyle = bsNone (每个面板是 align = alclientAlignWithMargins = True

我获取单元格的代码'ClientRect':

procedure TMyCustomGridPanel.paint;
var
  Row, Col: Integer;
  rctCell: TRect;

  function GetColor(C, R: Integer): TColor;
  begin
    if odd(C + R) then
      Result:= clblack
    else
      Result:= clWhite;
  end;
begin
  inherited;

 for Row := 0 to RowCollection.Count -1 do
  begin
    for Col := 0 to ColumnCollection.Count -1 do
    begin
      Canvas.Brush.Color := GetColor(Col, Row);
      if Canvas.Brush.Color <> clDefault then
      begin
        rctCell := CellRect[Col, Row];

        {$REGION 'Adjust first col an row'}
        if Col = 0 then
          rctCell.SetLocation(rctCell.Location.X + BorderWidth, rctCell.Location.Y);

        if Row = 0 then
          rctCell.SetLocation(rctCell.Location.X, rctCell.Location.Y + BorderWidth);
        {$ENDREGION}

        {$REGION 'ajust last cells'}
        if Col = (ColumnCollection.Count -1) then
        begin
          if Col > 0 then // tem mais de uma coluna
            rctCell.SetLocation(rctCell.Location.X - BorderWidth, rctCell.Location.Y);
          rctCell.Right := ClientRect.Right;
        end;

        if Row = (RowCollection.Count -1) then
        begin
          if Row > 0 then
            rctCell.SetLocation(rctCell.Location.X, rctCell.Location.Y - BorderWidth);
          rctCell.Bottom := ClientRect.Bottom;
        end;

        {$ENDREGION}
        Canvas.Pen.Style := psClear;
        Canvas.FillRect(rctCell);
      end;
    end;
  end;

end;

我的代码的结果: (面板只是为了表明 CellRect 不是放置控件的'ClientRect')

GridPanel
BorderWidth = 10
BorderStyle = bsNone
Color = clmarron

Panels
Align = alClient
Color = clgray
AlignWithMargins = true

TGridPanel 的单元格边框(虚线)的设计时渲染未考虑面板的边框。因此,它们在视觉上与您放置在网格单元格中的组件不一致。这是最明显的,例如Align 属性 设置为 alClient.

的面板

要知道单元格的实际矩形,在 TGridPanel 的坐标中,您可以使用 OffsetRect 来调整边框宽度。

var
  row, col: integer;
  r: TRect;
begin
  ...
  r := CellRect[Col, Row];
  OffsetRect(r, BorderWidth, BorderWidth);