1
0

ToolsBox.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. #include "cdrapp.h"
  2. #include <stdio.h>
  3. #include <windows.h>
  4. #include "resource.h"
  5. corel *cdr = NULL;
  6. static HINSTANCE g_hResource = NULL;
  7. char infobuf[256] = {0};
  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 intptr_t 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. intptr_t 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. // MessageBox(NULL, "更新提示信息: 激活CorelDRAW窗口", "CPG代码测试", MB_ICONSTOP);
  165. #define UPDATE_INFO_ACTIVE_CDRWND \
  166. PutTextValue(hDlg, INFO_TEXT, infobuf); Active_CorelWindows(hDlg);
  167. intptr_t CALLBACK ToolsBoxPlugin::DlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  168. {
  169. // 从附加数据中获取 m_pApp 指针
  170. VGCore::IVGApplication* cdr = reinterpret_cast<VGCore::IVGApplication*>(GetWindowLongPtr(hDlg, DWLP_USER));
  171. if (uMsg == WM_COMMAND) {
  172. try {
  173. switch (LOWORD(wParam)) {
  174. case IDC_RED :{
  175. double exp = GetTextValue(hDlg, EXP_TEXT);
  176. AutoMakeSelection(cdr);
  177. Box_AutoGroup(cdr, exp);
  178. UPDATE_INFO_ACTIVE_CDRWND
  179. }
  180. break;
  181. case IDC_CQL_OUTLINE:
  182. cql_OutlineColor(cdr);
  183. UPDATE_INFO_ACTIVE_CDRWND
  184. break;
  185. case IDC_CQL_FILL:
  186. cql_FillColor(cdr);
  187. UPDATE_INFO_ACTIVE_CDRWND
  188. break;
  189. case IDC_CQL_SIZE:
  190. cql_SameSize(cdr);
  191. UPDATE_INFO_ACTIVE_CDRWND
  192. break;
  193. case IDC_CLEAR_FILL:{
  194. double exp = GetTextValue(hDlg, EXP_TEXT);
  195. AutoMakeSelection(cdr);
  196. BBox_DrawRectangle(cdr, exp);
  197. UPDATE_INFO_ACTIVE_CDRWND
  198. }
  199. break;
  200. case IDC_SR_FLIP:
  201. Shapes_Filp(cdr);
  202. break;
  203. case IDC_CDR2AI:{
  204. CdrCopy_to_AdobeAI(cdr);
  205. sprintf(infobuf, "把CorelDRAW软件中选择物件复制到剪贴板,请切换到AI软件粘贴");
  206. UPDATE_INFO_ACTIVE_CDRWND
  207. }
  208. break;
  209. case IDC_AI2CDR:{
  210. AdobeAI_Copy_ImportCdr(cdr);
  211. sprintf(infobuf, "请先在AI软件选择物件复制,再切换到CorelDRAW软件点执行本功能");
  212. UPDATE_INFO_ACTIVE_CDRWND
  213. }
  214. break;
  215. case IDOK:
  216. case IDCANCEL:
  217. EndDialog(hDlg, 0);
  218. break;
  219. }
  220. } catch (_com_error &e) {
  221. MessageBox(NULL, e.Description(), "Error", MB_ICONSTOP);
  222. }
  223. } else if (uMsg == WM_INITDIALOG) {
  224. SetWindowText(::GetDlgItem(hDlg, EXP_TEXT), "0");
  225. return 1;
  226. }
  227. return 0;
  228. }
  229. extern "C" __declspec(dllexport) DWORD APIENTRY AttachPlugin(VGCore::IVGAppPlugin** ppIPlugin)
  230. {
  231. *ppIPlugin = new ToolsBoxPlugin;
  232. return 0x100;
  233. }