gpt4 book ai didi

delphi - 以表格样式保存控件的组件?

转载 作者:行者123 更新时间:2023-12-03 18:32:20 24 4
gpt4 key购买 nike

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

像这样的东西:

sample

最佳答案

您可以使用 TGridpanel 并通过覆盖 Paint 方法实现您自己的绘画逻辑。
附加的图像显示了它的样子,要达到预期的结果,需要添加一些代码。 enter image description here

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.

关于delphi - 以表格样式保存控件的组件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16160708/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com