2013-04-23 40 views
2

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

事情是这样的:

sample

+1

您可以在您的问题中包含一张图片,以显示您寻找什么外观,并尝试更好地解释。例如,我不清楚的是,如果您希望组件“重复”包含的组件或不。此外,“表格风格”对我来说还不清楚,但那可能是因为我不是母语的人。 – jachguate 2013-04-23 05:27:32

+0

对不起,我的英语不好。我只想要一个像ListView这样的组件,但是每个单元格可以包含另一个组件,例如'GridPanel' – SAMPro 2013-04-23 06:11:09

回答

3

您可以使用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. 
+1

太棒了。非常感谢。 – SAMPro 2013-04-23 08:05:37