ToolsBox.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. #include <windows.h>
  2. #include "resource.h"
  3. #import "VGCoreAuto.tlb" \
  4. rename("GetCommandLine", "VGGetCommandLine") \
  5. rename("CopyFile", "VGCore") \
  6. rename("FindWindow", "VGFindWindow")
  7. static HINSTANCE g_hResource = NULL;
  8. BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
  9. {
  10. if(fdwReason == DLL_PROCESS_ATTACH)
  11. {
  12. g_hResource = (HINSTANCE)hinstDLL;
  13. }
  14. return TRUE;
  15. }
  16. class ToolsBoxPlugin : public VGCore::IVGAppPlugin
  17. {
  18. private:
  19. VGCore::IVGApplication *m_pApp;
  20. volatile ULONG m_ulRefCount;
  21. long m_lCookie;
  22. static INT_PTR CALLBACK DlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
  23. void OpenToolsBox();
  24. public:
  25. ToolsBoxPlugin();
  26. // IUnknown
  27. public:
  28. STDMETHOD(QueryInterface)(REFIID riid, void **ppvObject);
  29. STDMETHOD_(ULONG, AddRef)(void) { return ++m_ulRefCount; }
  30. STDMETHOD_(ULONG, Release)(void)
  31. {
  32. ULONG ulCount = --m_ulRefCount;
  33. if(ulCount == 0)
  34. {
  35. delete this;
  36. }
  37. return ulCount;
  38. }
  39. // IDispatch
  40. public:
  41. STDMETHOD(GetTypeInfoCount)(UINT *pctinfo) { return E_NOTIMPL; }
  42. STDMETHOD(GetTypeInfo)(UINT iTInfo, LCID lcid, ITypeInfo **ppTInfo) { return E_NOTIMPL; }
  43. STDMETHOD(GetIDsOfNames)(REFIID riid, LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId) { return E_NOTIMPL; }
  44. STDMETHOD(Invoke)(DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr);
  45. // IVGAppPlugin
  46. public:
  47. STDMETHOD(raw_OnLoad)(VGCore::IVGApplication *Application);
  48. STDMETHOD(raw_StartSession)();
  49. STDMETHOD(raw_StopSession)();
  50. STDMETHOD(raw_OnUnload)();
  51. };
  52. ToolsBoxPlugin::ToolsBoxPlugin()
  53. {
  54. m_pApp = NULL;
  55. m_lCookie = 0;
  56. m_ulRefCount = 1;
  57. }
  58. STDMETHODIMP ToolsBoxPlugin::QueryInterface(REFIID riid, void **ppvObject)
  59. {
  60. HRESULT hr = S_OK;
  61. m_ulRefCount++;
  62. if(riid == IID_IUnknown)
  63. {
  64. *ppvObject = (IUnknown *)this;
  65. }
  66. else if(riid == IID_IDispatch)
  67. {
  68. *ppvObject = (IDispatch *)this;
  69. }
  70. else if(riid == __uuidof(VGCore::IVGAppPlugin))
  71. {
  72. *ppvObject = (VGCore::IVGAppPlugin *)this;
  73. }
  74. else
  75. {
  76. m_ulRefCount--;
  77. hr = E_NOINTERFACE;
  78. }
  79. return hr;
  80. }
  81. STDMETHODIMP ToolsBoxPlugin::Invoke(DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
  82. {
  83. switch(dispIdMember)
  84. {
  85. case 0x0014: // DISPID_APP_ONPLUGINCMD
  86. if(pDispParams != NULL && pDispParams->cArgs == 1)
  87. {
  88. _bstr_t strCmd(pDispParams->rgvarg[0].bstrVal);
  89. if(strCmd == _bstr_t("OpenToolsBox"))
  90. {
  91. // MessageBox(NULL, _bstr_t("OpenToolsBox"), _bstr_t("OpenToolsBox"), MB_ICONSTOP);
  92. OpenToolsBox();
  93. }
  94. }
  95. break;
  96. case 0x0015: // DISPID_APP_ONPLUGINCMDSTATE
  97. if(pDispParams != NULL && pDispParams->cArgs == 3)
  98. {
  99. _bstr_t strCmd(pDispParams->rgvarg[2].bstrVal);
  100. if(strCmd == _bstr_t("OpenToolsBox"))
  101. {
  102. *pDispParams->rgvarg[1].pboolVal = VARIANT_TRUE;
  103. }
  104. }
  105. break;
  106. }
  107. return S_OK;
  108. }
  109. STDMETHODIMP ToolsBoxPlugin::raw_OnLoad(VGCore::IVGApplication *Application)
  110. {
  111. m_pApp = Application;
  112. if(m_pApp)
  113. {
  114. m_pApp->AddRef();
  115. }
  116. return S_OK;
  117. }
  118. STDMETHODIMP ToolsBoxPlugin::raw_StartSession()
  119. {
  120. try
  121. {
  122. m_pApp->AddPluginCommand(_bstr_t("OpenToolsBox"), _bstr_t("Tools Box"), _bstr_t("打开工具窗口"));
  123. VGCore::ICUIControlPtr ctl = m_pApp->CommandBars->Item[_bstr_t("Standard")]->Controls->AddCustomButton(VGCore::cdrCmdCategoryPlugins, _bstr_t("OpenToolsBox"), 10 , VARIANT_FALSE);
  124. ctl->SetIcon2(_bstr_t("guid://d2fdc0d9-09f8-4948-944c-4297395c05b7"));
  125. m_lCookie = m_pApp->AdviseEvents(this);
  126. }
  127. catch(_com_error &e)
  128. {
  129. MessageBox(NULL, e.Description(), _bstr_t("Error"), MB_ICONSTOP);
  130. }
  131. return S_OK;
  132. }
  133. STDMETHODIMP ToolsBoxPlugin::raw_StopSession()
  134. {
  135. try
  136. {
  137. m_pApp->UnadviseEvents(m_lCookie);
  138. m_pApp->RemovePluginCommand(_bstr_t("OpenToolsBox"));
  139. }
  140. catch(_com_error &e)
  141. {
  142. MessageBox(NULL, e.Description(), _bstr_t("Error"), MB_ICONSTOP);
  143. }
  144. return S_OK;
  145. }
  146. STDMETHODIMP ToolsBoxPlugin::raw_OnUnload()
  147. {
  148. if(m_pApp)
  149. {
  150. m_pApp->Release();
  151. m_pApp = NULL;
  152. }
  153. return S_OK;
  154. }
  155. void ToolsBoxPlugin::OpenToolsBox()
  156. {
  157. // MessageBox(NULL, _bstr_t("打开工具箱"), _bstr_t("打开工具箱"), MB_ICONSTOP);
  158. try
  159. {
  160. m_pApp->StartupMode = VGCore::cdrStartupDoNothing;
  161. // To avoid 64 bit portability warning, store the long handle value into an INT_PTR
  162. // before casting it to HWND:
  163. INT_PTR nHandle = m_pApp->AppWindow->Handle;
  164. HWND hAppWnd = reinterpret_cast<HWND>(nHandle);
  165. INT_PTR nRet = DialogBoxParam(g_hResource, MAKEINTRESOURCE(IDD_WELCOME), hAppWnd, DlgProc, (LPARAM)this);
  166. switch(nRet)
  167. {
  168. case IDC_NEWDOC:
  169. m_pApp->CreateDocument();
  170. break;
  171. case IDC_LASTDOC:
  172. if(m_pApp->RecentFiles->Count > 0)
  173. {
  174. m_pApp->OpenDocument(m_pApp->RecentFiles->Item[1]->FullName, 0);
  175. }
  176. else
  177. {
  178. MessageBox(NULL, "No documents were editied yet.", "Error", MB_ICONSTOP);
  179. }
  180. break;
  181. }
  182. }
  183. catch(_com_error &e)
  184. {
  185. MessageBox(NULL, e.Description(), "Error", MB_ICONSTOP);
  186. }
  187. }
  188. INT_PTR CALLBACK ToolsBoxPlugin::DlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  189. {
  190. if(uMsg == WM_COMMAND)
  191. {
  192. switch(LOWORD(wParam))
  193. {
  194. case IDC_NEWDOC:
  195. EndDialog(hDlg, IDC_NEWDOC);
  196. break;
  197. case IDC_LASTDOC:
  198. EndDialog(hDlg, IDC_LASTDOC);
  199. break;
  200. case IDOK:
  201. case IDCANCEL:
  202. EndDialog(hDlg, 0);
  203. break;
  204. }
  205. }
  206. else if(uMsg == WM_INITDIALOG)
  207. {
  208. return 1;
  209. }
  210. return 0;
  211. }
  212. extern "C" __declspec(dllexport) DWORD APIENTRY AttachPlugin(VGCore::IVGAppPlugin **ppIPlugin)
  213. {
  214. *ppIPlugin = new ToolsBoxPlugin;
  215. return 0x100;
  216. }