dllMDI窗体有没有办法调用父窗体的方法我想把所有子窗体都创建在DLL中,父窗体只向DLL传如APPLICATION对象有没有办法在MDI子窗体内调用父窗体的自定义方法,通过父窗体的句柄对象能反射调用吗?typedef int(*func)(int);
func sum;
sum = (func)Application->MainForm->MethodAddress("GetSum");
return sum(10);这是我在BCB代码里写的代码,到行不通,sum 等于 NULL,
在设置子窗体的Parent = Application->MainForm运行时发生错误Cannot focus a disabled or invisible window所有改用Application->MainForm我还尝试了另外一种方法,但也不行
PMethodInfoHeader mih
mih = GetMethodInfo(Application->MainForm,"GetSum");
int p1 = 1,p2 =2;
Variant *v1;
int result = ObjectInvoke(Application->MainForm,mih, &p1,p2,v1,1);
return result;

解决方案 »

  1.   

    下面代码可以试试。function GetInstanceFromhWnd(const HWND: Cardinal): TWinControl;
    type
      PObjectInstance = ^TObjectInstance;  TObjectInstance = packed record
        Code: Byte; { 短跳转 $E8 }
        Offset: Integer; { CalcJmpOffset(Instance, @Block^.Code); }
        Next: PObjectInstance; { MainWndProc 地址 }
        Self: Pointer; { 控件对象地址 }
      end;
    var
      wc: PObjectInstance;
    begin
      Result := nil;
      wc := Pointer(GetWindowLong(HWND, GWL_WNDPROC));
      if wc <> nil then
      begin
        Result := wc.Self;
      end;
    end; //dll窗体程序
    procedure TfrmAttRecord.actCloseExecute(Sender: TObject);
    var
      tbs: TRzTabSheet;
    begin
      //获取主窗体的控件
      tbs := TRzTabSheet(GetInstanceFromhWnd(Self.ParentWindow));
      if tbs <> nil then
      begin
        //操作主窗体控件
        tbs.PageControl.CloseActiveTab;
      end;
    end;
      

  2.   

    结贴:在其他帖子看到的方法:http://topic.csdn.net/t/20050826/15/4233405.html #include <ObjAuto.hpp>template<typename T>
    bool __fastcall GetPublishMethodPtr(T &EV, TObject *Object, String MothedName) {
    void **Fun;
    (void*)Fun = &EV;
    *Fun = Object->MethodAddress(MothedName);
    *(Fun + 1) = Object;
    return(*Fun != NULL);
    }int __fastcall TBaseForm::GetSum(int num) {
    typedef int __fastcall(__closure * func)(int);
    func test;
    if (GetPublishMethodPtr(test, Application->MainForm, "GetSum")) {
    int i = 0;
    i = test(111);
    ShowMessage(IntToStr(i + 1));
    }
    else {
    ShowMessage("對象myForm中沒定義MyFunName方法或該方法沒在published部分定義 ");
    } return 0;
    }//主窗体
    int __fastcall TMaster::GetSum(int num) {
    return 100 + num;
    }