ToolsBox.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. #include <stdio.h>
  2. #include <windows.h>
  3. #include "cdrapi.h"
  4. #include "resource.h"
  5. #include "cdrapp.h"
  6. corel *cdr = NULL;
  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. // 接口转交给cdr
  106. cdr = m_pApp;
  107. try {
  108. m_pApp->AddPluginCommand(_bstr_t("OpenToolsBox"), _bstr_t("Tools Box"), _bstr_t("打开工具窗口"));
  109. VGCore::ICUIControlPtr ctl = m_pApp->CommandBars->Item[_bstr_t("Standard")]->Controls->AddCustomButton(VGCore::cdrCmdCategoryPlugins, _bstr_t("OpenToolsBox"), 10, VARIANT_FALSE);
  110. ctl->SetIcon2(_bstr_t("guid://d2fdc0d9-09f8-4948-944c-4297395c05b7"));
  111. m_lCookie = m_pApp->AdviseEvents(this);
  112. } catch (_com_error &e) {
  113. MessageBox(NULL, e.Description(), _bstr_t("Error"), MB_ICONSTOP);
  114. }
  115. return S_OK;
  116. }
  117. STDMETHODIMP ToolsBoxPlugin::raw_StopSession()
  118. {
  119. try {
  120. m_pApp->UnadviseEvents(m_lCookie);
  121. m_pApp->RemovePluginCommand(_bstr_t("OpenToolsBox"));
  122. } catch (_com_error &e) {
  123. MessageBox(NULL, e.Description(), _bstr_t("Error"), MB_ICONSTOP);
  124. }
  125. return S_OK;
  126. }
  127. STDMETHODIMP ToolsBoxPlugin::raw_OnUnload()
  128. {
  129. if (m_pApp) {
  130. m_pApp->Release();
  131. m_pApp = NULL;
  132. }
  133. return S_OK;
  134. }
  135. void ToolsBoxPlugin::OpenToolsBox()
  136. {
  137. m_pApp->StartupMode = VGCore::cdrStartupDoNothing;
  138. INT_PTR nHandle = m_pApp->AppWindow->Handle;
  139. HWND hAppWnd = reinterpret_cast<HWND>(nHandle);
  140. // 创建非模态对话框
  141. HWND hDlgWnd = CreateDialogParam(g_hResource, MAKEINTRESOURCE(IDD_TOOLS_BOX), hAppWnd, DlgProc, (LPARAM)m_pApp);
  142. // 在创建对话框之前存储 m_pApp 指针
  143. SetWindowLongPtr(hDlgWnd, DWLP_USER, (LONG_PTR)m_pApp);
  144. // 获取屏幕的宽度和高度
  145. RECT rect;
  146. GetWindowRect(GetDesktopWindow(), &rect);
  147. int screenWidth = rect.right - rect.left;
  148. int screenHeight = rect.bottom - rect.top;
  149. // 计算对话框窗口的宽度和高度
  150. RECT dlgRect;
  151. GetWindowRect(hDlgWnd, &dlgRect);
  152. int dlgWidth = dlgRect.right - dlgRect.left;
  153. int dlgHeight = dlgRect.bottom - dlgRect.top;
  154. // 计算对话框窗口的左上角坐标,使其居中显示
  155. int x = (screenWidth - dlgWidth) / 2;
  156. int y = (screenHeight - dlgHeight) / 2;
  157. // 设置对话框窗口的位置
  158. SetWindowPos(hDlgWnd, NULL, x, y, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
  159. // 设置对话框窗口的父窗口 // #define GWL_HWNDPARENT (-8)
  160. SetWindowLong(hDlgWnd, -8, (LONG)hAppWnd);
  161. // 显示对话框窗口
  162. ShowWindow(hDlgWnd, SW_SHOW);
  163. }
  164. INT_PTR CALLBACK ToolsBoxPlugin::DlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  165. {
  166. // 从附加数据中获取 m_pApp 指针
  167. VGCore::IVGApplication* cdr = reinterpret_cast<VGCore::IVGApplication*>(GetWindowLongPtr(hDlg, DWLP_USER));
  168. if (uMsg == WM_COMMAND) {
  169. try {
  170. switch (LOWORD(wParam)) {
  171. case IDC_RED :
  172. AdobeAI_Copy_ImportCdr(cdr);
  173. // fill_red(cdr);
  174. MessageBox(NULL, "AdobeAI_Copy_ImportCdr", "CPG代码测试", MB_ICONSTOP);
  175. break;
  176. case IDC_CQL_OUTLINE:
  177. cql_OutlineColor(cdr);
  178. Active_CorelWindows(hDlg);
  179. break;
  180. case IDC_CQL_FILL:
  181. cql_FillColor(cdr);
  182. Active_CorelWindows(hDlg);
  183. break;
  184. case IDC_CQL_SIZE:
  185. cql_SameSize(cdr);
  186. Active_CorelWindows(hDlg);
  187. break;
  188. case IDC_CLEAR_FILL:
  189. Clear_Fill(cdr);
  190. break;
  191. case IDC_SR_FLIP:
  192. Shapes_Filp(cdr);
  193. break;
  194. case IDC_CDR2AI:
  195. CdrCopy_to_AdobeAI(cdr);
  196. break;
  197. case IDC_AI2CDR:
  198. AdobeAI_Copy_ImportCdr(cdr);
  199. break;
  200. case IDOK:
  201. case IDCANCEL:
  202. EndDialog(hDlg, 0);
  203. break;
  204. }
  205. } catch (_com_error &e) {
  206. MessageBox(NULL, e.Description(), "Error", MB_ICONSTOP);
  207. }
  208. } else if (uMsg == WM_INITDIALOG) {
  209. return 1;
  210. }
  211. return 0;
  212. }
  213. extern "C" __declspec(dllexport) DWORD APIENTRY AttachPlugin(VGCore::IVGAppPlugin** ppIPlugin)
  214. {
  215. *ppIPlugin = new ToolsBoxPlugin;
  216. return 0x100;
  217. }