แต่สิ่งที่สำคัญจริงๆ จะอยู่ใน Unit ที่เป็น MainForm ของระบบงานของเรานะครับ ตัวอย่าง
unit HOSxPDeveloperTestUnit;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ExtCtrls;
type
THOSxPDeveloperTestForm = class(TForm)
Panel1: TPanel;
private
{ Private declarations }
public
{ Public declarations }
class procedure MenuButtonClick(Sender:TObject);
end;
var
HOSxPDeveloperTestForm: THOSxPDeveloperTestForm;
implementation
uses RTTI;
var MenuButtonClickEvent : TNotifyEvent;
{$R *.dfm}
function ExecuteRTTIFunction(UnitClassname, FunctionName: String;
Args: Array of Tvalue): Tvalue;
var
c: TRttiContext;
m: TRttiMethod;
t: TRttiInstanceType;
begin
c := TRttiContext.Create;
t := (c.FindType(UnitClassname) as TRttiInstanceType);
c.Free;
if assigned(t) then
begin
m := t.GetMethod(FunctionName);
if assigned(m) then
begin
Result := m.Invoke(t.MetaclassType, Args);
end;
end
else
begin
showmessage('Type not found : ' + UnitClassname);
end;
end;
function ExecuteRTTIObjectMethod(Obj: TObject; MName: String;
Args: Array of Tvalue): Tvalue;
var
c: TRttiContext;
r: TRttiType;
t: TRttiInstanceType;
Method: TRttiMethod;
begin
c := TRttiContext.Create;
try
r := c.FindType(Obj.UnitName + '.' + Obj.ClassName);
for Method in r.GetDeclaredMethods do
begin
if SameText(Method.Name, MName) then
begin
Result := Method.Invoke(Obj, Args);
break;
end;
end;
finally
c.Free;
end;
end;
{ THOSxPDeveloperTestForm }
class procedure THOSxPDeveloperTestForm.MenuButtonClick(Sender: TObject);
begin
showmessage('Click');
end;
var Mainform:TForm;
initialization
MainForm:= TForm(ExecuteRTTIFunction('MainFormUnit.TMainForm','GetMainForm',[]).AsObject);
{ ExecuteRTTIObjectMethod(Mainform, 'AddLargeMenuButton',
['TabName', 'GroupName', 'ButtonCaption','HOSxPDeveloperTestUnit.THOSxPDeveloperTestForm',
true, // showmodal or show
false // reuse old form case mdichild
,-1]); }
ExecuteRTTIObjectMethod(Mainform, 'AddLargeMenuButton',
['TabName', 'GroupName', 'ButtonCaption','HOSxPDeveloperTestUnit.THOSxPDeveloperTestForm',
false, // showmodal or show
true // reuse old form case mdichild
,-1]);
end.