2

我正在寻找一个可以容纳另一个组件(像按钮)并以表格样式显示它们的组件。GridPanel是一个这样的组件,但不显示那些网格运行时。

像这样的东西:

样本

4

1 回答 1

3

您可以使用 TGridpanel 并通过覆盖 Paint 方法来实现您自己的绘画逻辑。
附加的图像显示了它的外观,为了达到您的预期结果,需要添加一些代码。在此处输入图像描述

unit Unit6;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls;

type

  TGridPanel = Class(ExtCtrls.TGridPanel)
  protected
    procedure Paint; override;
  end;

  TCellItem = Class(ExtCtrls.TCellItem)
    Property Size;  // make protected Size accessable
  End;

  TForm6 = class(TForm)
    GridPanel1: TGridPanel;
    Button6: TButton;
    Button7: TButton;
    Button8: TButton;
    Button10: TButton;
    Button11: TButton;
    Button12: TButton;
    Button14: TButton;
    Button15: TButton;
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form6: TForm6;

implementation

{$R *.dfm}

uses TypInfo, Rtti;

Function GetSize(B: TComponent): Integer;
var
  c: TRttiContext;
  t: TRttiInstanceType;
begin
  c := TRttiContext.Create;
  try
    t := c.GetType(B.ClassInfo) as TRttiInstanceType;
    Result := t.GetProperty('Width').GetValue(B).AsInteger;
  finally
    c.Free;
  end;
end;

procedure TGridPanel.Paint;
var
  I: Integer;
  LinePos, Size: Integer;
  ClientRect: TRect;
begin
  inherited;
  begin
    LinePos := 0;
    Canvas.Pen.Style := psSolid;
    Canvas.Pen.Color := clBlack;
    ClientRect := GetClientRect;
    Canvas.Rectangle(ClientRect);
    for I := 0 to ColumnCollection.Count - 2 do
    begin     // cast to "own" TCellItem to access size
      Size := TCellItem(ColumnCollection[I]).Size;

      if I = 0 then
        Canvas.MoveTo(LinePos + Size, ClientRect.Top)
      else   // "keep cells together"
        Canvas.MoveTo(LinePos + Size, ClientRect.Top + TCellItem(RowCollection[0]).Size);

      Canvas.LineTo(LinePos + Size, ClientRect.Bottom);
      Inc(LinePos, Size);
    end;

    Canvas.Font.Size := 12;
    Canvas.TextOut(TCellItem(ColumnCollection[0]).Size + 20,
      (TCellItem(RowCollection[0]).Size - Canvas.TextHeight('X')) div 2,
      'a longer caption text to be displayed');

    LinePos := 0;
    for I := 0 to RowCollection.Count - 2 do
    begin
      Size := TCellItem(RowCollection[I]).Size;
      Canvas.MoveTo(ClientRect.Left, LinePos + Size);
      Canvas.LineTo(ClientRect.Right, LinePos + Size);
      Inc(LinePos, Size);
    end;
  end;
end;

end.
于 2013-04-23T07:24:04.523 回答