我正在寻找一个可以容纳其他组件(如按钮)的组件,并以表格形式显示它们。 GridPanel
是一个这样的组件,但不显示那些网格运行时。
这样的事情:
答案 0 :(得分: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.