MyBeanDLL带包运行时,DLL窗体不会出现在任务栏上,但如果不带包运行时,则DLL窗体则也会出现在任务栏上,这是因为DLL里的Application和主程序的Application不是同一个。
按以往DLL窗体的方法,可以把主窗体的Application传给DLL;MyBean则要在DLL里主动去获取,方法如下:把主程序也做为插件,并增加获取Application的接口,DLL里用今天(2014-09-18)新增的BeforeGetBean事件,在这个事件中去获取主程序的Application。
BCB的代码如下:
EXE主程序:
int WINAPI _tWinMain(HINSTANCE, HINSTANCE, LPTSTR, int) { try { Application->Initialize(); applicationContextInitialize(); registerFactoryObject(getBeanFactory(),"MainApp"); Application->MainFormOnTaskBar = true; TMyBeanFactoryTools::getBean("MainApp"); // Application->CreateForm(__classid(TForm1), &Form1); Application->Run(); } catch (Exception &exception) { Application->ShowException(&exception); } catch (...) { try { throw Exception(""); } catch (Exception &exception) { Application->ShowException(&exception); } } return 0; } //--------------------------------------------------------------------------- void __fastcall PACKAGE RegisterUI() { #pragma startup RegisterUI (beanFactory())->RegisterBean("MainApp",__classid(TForm1),true)->IsMainForm=true; } //--------------------------------------------------------------------------- #define INTF_IMP_REFCOUNT(BASE) \ ULONG __stdcall AddRef() { return BASE::_AddRef();} \ ULONG __stdcall Release(){ return BASE::_Release();}\ HRESULT __stdcall QueryInterface(REFIID iid, void*p) { return BASE::QueryInterface(iid, p);} __interface IMainApp; typedef System::DelphiInterface<IMainApp> _di_IMainApp; __interface __declspec(uuid("{0DAA1A1E-CB13-46E1-BF9F-D54D8B117F25}")) IMainApp: public IInterface { public: virtual TApplication * __stdcall GetHandle(void) = 0 ; }; //--------------------------------------------------------------------------- class TMainApp : public Vcl::Forms::TForm,IMainApp { typedef Vcl::Forms::TForm inherited; private: System::UnicodeString FInstanceID; protected: TApplication * __stdcall GetHandle(void); public: __fastcall virtual TMainApp(System::Classes::TComponent* AOwner); __fastcall virtual ~TMainApp(void); INTF_IMP_REFCOUNT(TForm); }; //--------------------------------------------------------------------------- TApplication * __stdcall TMainApp::GetHandle(void) { return Application; }
DLL:
TApplication * SaveApp=NULL; //--------------------------------------------------------------------------- void __stdcall MyPluginInfoProc(TPluginInfo* pvObject) { if(!SaveApp) { _di_IMainApp MainApp; MainApp=TMyBeanFactoryTools::getBean("MainApp"); if(((int)MainApp->GetHandle()->Handle)>0) { SaveApp=Application; Application=MainApp->GetHandle(); } } } //--------------------------------------------------------------------------- int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void* lpReserved) { if(reason==DLL_PROCESS_ATTACH) { (beanFactory())->BeforeGetBean=MyPluginInfoProc; (beanFactory())->RegisterBean("Test",__classid(TForm1),true); } else if(reason==DLL_PROCESS_DETACH&&SaveApp) { Application=SaveApp; } return 1; }