ToolsBox.cpp 6.7 KB

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