自己写了一个动态创建数组控件的类,但无法显示控件(无报错)望高人指点,谢谢(100)

C

cfj521

Unregistered / Unconfirmed
GUEST, unregistred user!
以下是 类源码:unit uMatrixControls;interfaceuses Windows, Classes, Controls, StdCtrls;type Tmca = (mcaTopLeft, mcaTopRight, mcaBottomLeft, mcaBottomRight); Tmcpo = (mcpoHorizontal, mcpoVertical); Tct = (ctButton, ctCheckBox, ctStaticText, ctEdit, ctRadioButton, ctComboBox); //all inherite from TWinControl TAryTStrs = array of TStrings; TAryCBS = array of TCheckBoxState; TAryBool = array of Boolean; TAryComponent = array of TComponent; TmcPattern = record Order: Tmcpo; BasePoint: TPoint; CountPerRow: Integer; CountPerColumn: Integer; SpcVer: Integer; SpcHor: Integer; end; //record TmcProps = record Parant: TComponent; UniformWidth: Integer; UniformHeight: Integer; aStrings: TStrings; cmbStrings: TAryTStrs; cbStates: TAryCBS; rbCheckeds: TAryBool; Enableds: TAryBool; Visibles: TAryBool; end; //record TMatrixControls = Class private pCount: Integer; pControlType: Tct; pItems: TAryComponent; pRect: TRect; pRows: Integer; pCols: Integer; function GetMax(const NumA, NumB: Integer): Integer; public constructor Create(const ControlType: Tct; const Align: Tmca; const Pattern: TmcPattern; const Count: Integer; const Props: TmcProps ); procedure Free; property Items: TAryComponent read pItems write pItems; property Rect: TRect read pRect; property Rows: Integer read pRows; property Cols: Integer read pCols; end; //classimplementationconstructor TMatrixControls.Create(const ControlType: Tct; const Align: Tmca; const Pattern: TmcPattern; const Count: Integer; const Props: TmcProps); procedure Snap(const sAlign: Tmca; const sPattern: TmcPattern; const sCount: Integer); var n: Integer; swTLR, swBLR: Integer; begin case sAlign of mcaTopLeft: begin swTLR := 0; swBLR := 0; end; mcaTopRight: begin swTLR := 1; swBLR := 0; end; mcaBottomLeft: begin swTLR := 0; swBLR := 1; end; mcaBottomRight: begin swTLR := 1; swBLR := 1; end; end; //case for n := 0 to sCount - 1 do begin with pItems[n] as TWinControl do begin case sPattern.Order of mcpoHorizontal: begin Left := (sPattern.BasePoint.X - Width*swTLR) * (n mod sPattern.CountPerRow + 1) + sPattern.SpcHor * (n mod sPattern.CountPerRow); Top := (sPattern.BasePoint.Y - Height*swBLR) * (n div sPattern.CountPerRow + 1) + sPattern.SpcVer * (n div sPattern.CountPerRow); pRows := GetMax(pRows, n div sPattern.CountPerRow + 1); pCols := GetMax(pCols, n mod sPattern.CountPerRow + 1); pRect.Left := sPattern.BasePoint.X - (Width + Pattern.SpcHor) * pCols * swTLR; pRect.Top := sPattern.BasePoint.Y - (Height + Pattern.SpcVer) * pRows * swBLR; pRect.Right := sPattern.BasePoint.X + (Width + Pattern.SpcHor) * pCols * (not swTLR); pRect.Bottom := sPattern.BasePoint.Y + (Height + Pattern.SpcVer) * pRows * (not swBLR); end; mcpoVertical: begin Top := (sPattern.BasePoint.Y - Height*swBLR) * (n mod sPattern.CountPerColumn + 1) + sPattern.SpcVer * (n mod sPattern.CountPerColumn); Left := (sPattern.BasePoint.X - Width*swTLR) * (n div sPattern.CountPerColumn + 1) + sPattern.SpcHor * (n div sPattern.CountPerColumn); pRows := GetMax(pRows, n mod sPattern.CountPerColumn + 1); pCols := GetMax(pCols, n div sPattern.CountPerColumn + 1); pRect.Left := sPattern.BasePoint.X - (Width + Pattern.SpcHor) * pRows * swTLR; pRect.Top := sPattern.BasePoint.Y - (Height + Pattern.SpcVer) * pCols * swBLR; pRect.Right := sPattern.BasePoint.X + (Width + Pattern.SpcHor) * pRows * (not swTLR); pRect.Bottom := sPattern.BasePoint.Y + (Height + Pattern.SpcVer) * pCols * (not swBLR); end; end; //case end; //with end; //for end; //procedure snapvar i: Integer;begin inherited Create(); pCount := Count; pRows := 0; pCols := 0; pRect.Left := 0; pRect.Right := 0; pRect.Top := 0; pRect.Bottom := 0; SetLength(pItems, Count); case ControlType of ctButton: begin for i := 0 to Count - 1 do begin pItems := TButton.Create(Props.Parant); with pItems as TButton do begin Caption := Props.aStrings; Width := Props.UniformWidth; Height := Props.UniformHeight; Enabled := Props.Enableds; Visible := Props.Visibles; end; //with end; //for Snap(Align, Pattern, Count); end; //ctButton ctCheckBox: begin for i := 0 to Count - 1 do begin pItems := TCheckBox.Create(Props.Parant); with pItems as TCheckBox do begin Caption := Props.aStrings; Width := Props.UniformWidth; Height := Props.UniformHeight; State := Props.cbStates; Enabled := Props.Enableds; Visible := Props.Visibles; end; //with end; //for end; //ctCheckBox ctStaticText: begin for i := 0 to Count - 1 do begin pItems := TStaticText.Create(Props.Parant); with pItems as TStaticText do begin Caption := Props.aStrings; Width := Props.UniformWidth; Height := Props.UniformHeight; Enabled := Props.Enableds; Visible := Props.Visibles; end; //with end; //for Snap(Align, Pattern, Count); end; //ctStaticText ctEdit: begin for i := 0 to Count - 1 do begin pItems := TEdit.Create(Props.Parant); with pItems as TEdit do begin Text := Props.aStrings; Width := Props.UniformWidth; Height := Props.UniformHeight; Enabled := Props.Enableds; Visible := Props.Visibles; end; //with end; //for Snap(Align, Pattern, Count); end; //ctEdit ctRadioButton: begin for i := 0 to Count - 1 do begin pItems := TRadioButton.Create(Props.Parant); with pItems as TRadioButton do begin Caption := Props.aStrings; Width := Props.UniformWidth; Height := Props.UniformHeight; Checked := Props.rbCheckeds; Enabled := Props.Enableds; Visible := Props.Visibles; end; //with end; //for Snap(Align, Pattern, Count); end; //ctRadioButton ctComboBox: begin for i := 0 to Count - 1 do begin pItems := TComboBox.Create(Props.Parant); with pItems as TComboBox do begin Text := Props.aStrings; Items := Props.cmbStrings; Width := Props.UniformWidth; Height := Props.UniformHeight; Enabled := Props.Enableds; Visible := Props.Visibles; end; //with end; //for Snap(Align, Pattern, Count); end; //ctComboBox end; //caseend;procedure TMatrixControls.Free;var i: Integer;begin for i := 0 to pCount - 1 do begin Items.Free; end; //for inherited;end;function TMatrixControls.GetMax(const NumA: Integer; const NumB: Integer): Integer;begin if NumA > NumB then Result := NumA else Result := NumB; //end if elseend;end.
 
新增控件的parent属性没有设置
 
谢谢,自己已经查出问题谢谢 znxia
 

Similar threads

I
回复
0
查看
578
import
I
I
回复
0
查看
2K
import
I
I
回复
0
查看
1K
import
I
I
回复
0
查看
2K
import
I
顶部