我想利用可變類類型,以儘量減少此代碼,它是從屬性和事件:可變類創建
if ctype='T' then
begin
C:= TTimeEdit.Create(self);
(c as TTimeEdit).OnMouseUp:= Panel2MouseUp;
(c as TTimeEdit).OnMouseDown:= Panel2MouseDown;
(c as TTimeEdit).OnMouseMove:= Panel2MouseMove;
(c as TTimeEdit).PopupMenu:= PopupMenu1;
end;
if ctype='S' then
begin
C:= TTabSheet.Create(self);
(c as TTabSheet).OnMouseUp:= Panel2MouseUp;
(c as TTabSheet).OnMouseDown:= Panel2MouseDown;
(c as TTabSheet).OnMouseMove:= Panel2MouseMove;
(c as TTabSheet).PopupMenu:= PopupMenu1;
end;
看起來像這樣:
VAR VARCLS:TCLASS;
BEGIN
if ctype='S' then
VARCLS:=TTabSheet;
if ctype='T' then
VARCLS:=TTimeEdit;
C:= VARCLS.Create(self);
(c as VARCLS).OnMouseUp:= Panel2MouseUp;
(c as VARCLS).OnMouseDown:= Panel2MouseDown;
(c as VARCLS).OnMouseMove:= Panel2MouseMove;
(c as VARCLS).PopupMenu:= PopupMenu1;
end;
確保代碼要長得多比這個,但我用了一個樣本!
使用RTTI來實現這個 –
我想要一隻小馬:-)你真的需要問一個實際的問題,而不是給出一個需求列表。你試過什麼了?你從哪裏被困住了? –
如果所有類都有一個共同的父類,並且來自該類的所有事件(就像您的情況一樣),只需創建一個'程序AssignEvents(const AObject:TWinControl);'並在其中設置所需的事件。無需單獨投射。對於FMX你可能需要TControl。只是一個樣本。 – Marcodor