ToolsBox.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. #include "cdrapi.h"
  2. #include "cdrapp.h"
  3. #include "resource.h"
  4. #include <stdio.h>
  5. #include <windows.h>
  6. corel *cdr = NULL;
  7. static HINSTANCE g_hResource = NULL;
  8. bool debug_flg = false; // 调试->高级模式
  9. char infobuf[256] = {0};
  10. BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {
  11. if (fdwReason == DLL_PROCESS_ATTACH) {
  12. g_hResource = (HINSTANCE)hinstDLL;
  13. }
  14. return TRUE;
  15. }
  16. class ToolsBoxPlugin : public VGCore::IVGAppPlugin {
  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. public:
  22. ToolsBoxPlugin();
  23. VGCore::IVGApplication *m_pApp;
  24. void OpenToolsBox();
  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. ULONG ulCount = --m_ulRefCount;
  31. if (ulCount == 0) {
  32. delete this;
  33. }
  34. return ulCount;
  35. }
  36. // IDispatch
  37. public:
  38. STDMETHOD(GetTypeInfoCount)(UINT *pctinfo) { return E_NOTIMPL; }
  39. STDMETHOD(GetTypeInfo)(UINT iTInfo, LCID lcid, ITypeInfo **ppTInfo) {
  40. return E_NOTIMPL;
  41. }
  42. STDMETHOD(GetIDsOfNames)
  43. (REFIID riid, LPOLESTR *rgszNames, UINT cNames, LCID lcid, DISPID *rgDispId) {
  44. return E_NOTIMPL;
  45. }
  46. STDMETHOD(Invoke)
  47. (DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr);
  48. // IVGAppPlugin
  49. public:
  50. STDMETHOD(raw_OnLoad)(VGCore::IVGApplication *Application);
  51. STDMETHOD(raw_StartSession)();
  52. STDMETHOD(raw_StopSession)();
  53. STDMETHOD(raw_OnUnload)();
  54. };
  55. ToolsBoxPlugin::ToolsBoxPlugin() {
  56. m_pApp = NULL;
  57. m_lCookie = 0;
  58. m_ulRefCount = 1;
  59. }
  60. STDMETHODIMP ToolsBoxPlugin::QueryInterface(REFIID riid, void **ppvObject) {
  61. HRESULT hr = S_OK;
  62. m_ulRefCount++;
  63. if (riid == IID_IUnknown) {
  64. *ppvObject = (IUnknown *)this;
  65. } else if (riid == IID_IDispatch) {
  66. *ppvObject = (IDispatch *)this;
  67. } else if (riid == __uuidof(VGCore::IVGAppPlugin)) {
  68. *ppvObject = (VGCore::IVGAppPlugin *)this;
  69. } else {
  70. m_ulRefCount--;
  71. hr = E_NOINTERFACE;
  72. }
  73. return hr;
  74. }
  75. STDMETHODIMP ToolsBoxPlugin::Invoke(DISPID dispIdMember, REFIID riid, LCID lcid,WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr) {
  76. switch (dispIdMember) {
  77. case 0x0014: // DISPID_APP_ONPLUGINCMD
  78. if (pDispParams != NULL && pDispParams->cArgs == 1) {
  79. _bstr_t strCmd(pDispParams->rgvarg[0].bstrVal);
  80. if (strCmd == _bstr_t("OpenToolsBox")) {
  81. // MessageBox(NULL, _bstr_t("OpenToolsBox"), _bstr_t("OpenToolsBox"),
  82. // MB_ICONSTOP);
  83. OpenToolsBox();
  84. }
  85. }
  86. break;
  87. case 0x0015: // DISPID_APP_ONPLUGINCMDSTATE
  88. if (pDispParams != NULL && pDispParams->cArgs == 3) {
  89. _bstr_t strCmd(pDispParams->rgvarg[2].bstrVal);
  90. if (strCmd == _bstr_t("OpenToolsBox")) {
  91. *pDispParams->rgvarg[1].pboolVal = VARIANT_TRUE;
  92. }
  93. }
  94. break;
  95. }
  96. return S_OK;
  97. }
  98. STDMETHODIMP ToolsBoxPlugin::raw_OnLoad(VGCore::IVGApplication *Application) {
  99. m_pApp = Application;
  100. if (m_pApp) {
  101. m_pApp->AddRef();
  102. }
  103. return S_OK;
  104. }
  105. STDMETHODIMP ToolsBoxPlugin::raw_StartSession() {
  106. // 接口转交给cdr
  107. cdr = m_pApp;
  108. try {
  109. m_pApp->AddPluginCommand(_bstr_t("OpenToolsBox"), _bstr_t("Tools Box"), _bstr_t("打开工具窗口"));
  110. VGCore::ICUIControlPtr ctl = m_pApp->CommandBars->Item[_bstr_t("Standard")]->Controls->AddCustomButton(VGCore::cdrCmdCategoryPlugins, _bstr_t("OpenToolsBox"), 10, VARIANT_FALSE);
  111. ctl->SetIcon2(_bstr_t("guid://d2fdc0d9-09f8-4948-944c-4297395c05b7"));
  112. m_lCookie = m_pApp->AdviseEvents(this);
  113. } catch (_com_error &e) {
  114. MessageBox(NULL, e.Description(), _bstr_t("Error"), MB_ICONSTOP);
  115. }
  116. return S_OK;
  117. }
  118. STDMETHODIMP ToolsBoxPlugin::raw_StopSession() {
  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. if (m_pApp) {
  129. m_pApp->Release();
  130. m_pApp = NULL;
  131. }
  132. return S_OK;
  133. }
  134. void ToolsBoxPlugin::OpenToolsBox() {
  135. m_pApp->StartupMode = VGCore::cdrStartupDoNothing;
  136. intptr_t nHandle = m_pApp->AppWindow->Handle;
  137. HWND hAppWnd = reinterpret_cast<HWND>(nHandle);
  138. // 创建非模态对话框
  139. HWND hDlgWnd = CreateDialogParam(g_hResource, MAKEINTRESOURCE(IDD_TOOLS_BOX), hAppWnd, DlgProc, (LPARAM)m_pApp);
  140. // 在创建对话框之前存储 m_pApp 指针
  141. SetWindowLongPtr(hDlgWnd, DWLP_USER, (LONG_PTR)m_pApp);
  142. // 获取屏幕的宽度和高度
  143. RECT rect;
  144. GetWindowRect(GetDesktopWindow(), &rect);
  145. int screenWidth = rect.right - rect.left;
  146. int screenHeight = rect.bottom - rect.top;
  147. // 计算对话框窗口的宽度和高度
  148. RECT dlgRect;
  149. GetWindowRect(hDlgWnd, &dlgRect);
  150. int dlgWidth = dlgRect.right - dlgRect.left;
  151. int dlgHeight = dlgRect.bottom - dlgRect.top;
  152. // 计算对话框窗口的左上角坐标,使其居中显示
  153. int x = (screenWidth - dlgWidth) / 2;
  154. int y = (screenHeight - dlgHeight) / 2;
  155. // 设置对话框窗口的位置
  156. SetWindowPos(hDlgWnd, NULL, x, y, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
  157. // 设置对话框窗口的父窗口 // #define GWL_HWNDPARENT (-8)
  158. SetWindowLong(hDlgWnd, -8, (LONG)hAppWnd);
  159. // 显示对话框窗口
  160. ShowWindow(hDlgWnd, SW_SHOW);
  161. }
  162. // MessageBox(NULL, "更新提示信息: 激活CorelDRAW窗口", "CPG代码测试", MB_ICONSTOP);
  163. #define UPDATE_INFO_ACTIVE_CDRWND \
  164. PutTextValue(hDlg, INFO_TEXT, infobuf); \
  165. Active_CorelWindows(hDlg);
  166. intptr_t CALLBACK ToolsBoxPlugin::DlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) {
  167. // 从附加数据中获取 m_pApp 指针
  168. VGCore::IVGApplication *cdr = reinterpret_cast<VGCore::IVGApplication *>(
  169. GetWindowLongPtr(hDlg, DWLP_USER));
  170. if (uMsg == WM_COMMAND) {
  171. try {
  172. switch (LOWORD(wParam)) {
  173. case ID_BOUNDARY_GROUP: {
  174. if (BST_CHECKED == IsDlgButtonChecked(hDlg, DEBUG_FLG))
  175. debug_flg = true;
  176. else
  177. debug_flg = false;
  178. double exp = GetTextValue(hDlg, EXP_TEXT);
  179. run_BoundaryGroup(cdr);
  180. UPDATE_INFO_ACTIVE_CDRWND
  181. } break;
  182. case IDC_BOX_GROUP: {
  183. double exp = GetTextValue(hDlg, EXP_TEXT);
  184. AutoMakeSelection(cdr);
  185. Box_AutoGroup(cdr, exp);
  186. UPDATE_INFO_ACTIVE_CDRWND
  187. } break;
  188. case IDC_CQL_OUTLINE:
  189. cql_OutlineColor(cdr);
  190. UPDATE_INFO_ACTIVE_CDRWND
  191. break;
  192. case IDC_CQL_FILL:
  193. cql_FillColor(cdr);
  194. UPDATE_INFO_ACTIVE_CDRWND
  195. break;
  196. case IDC_CQL_SIZE:
  197. cql_SameSize(cdr);
  198. UPDATE_INFO_ACTIVE_CDRWND
  199. break;
  200. case IDC_CLEAR_FILL: {
  201. double exp = GetTextValue(hDlg, EXP_TEXT);
  202. AutoMakeSelection(cdr);
  203. BBox_DrawRectangle(cdr, exp);
  204. UPDATE_INFO_ACTIVE_CDRWND
  205. } break;
  206. case IDC_SR_FLIP:
  207. Shapes_Filp(cdr);
  208. break;
  209. case IDC_CDR2AI: {
  210. CdrCopy_to_AdobeAI(cdr);
  211. sprintf(infobuf, "把CorelDRAW软件中选择物件复制到剪贴板,请切换到AI软件粘贴");
  212. UPDATE_INFO_ACTIVE_CDRWND
  213. } break;
  214. case IDC_AI2CDR: {
  215. AdobeAI_Copy_ImportCdr(cdr);
  216. sprintf(infobuf, "请先在AI软件选择物件复制,再切换到CorelDRAW软件点执行本功能");
  217. UPDATE_INFO_ACTIVE_CDRWND
  218. } break;
  219. case EXPAND_TOOLS: {
  220. // 获取当前窗口的句柄
  221. HWND hwnd = GetActiveWindow();
  222. // 获取当前窗口的矩形区域
  223. RECT rect;
  224. GetWindowRect(hwnd, &rect);
  225. // 计算新的宽度
  226. int newWidth = rect.right - rect.left + 125; // 增加125单位
  227. // 移动窗口到新的大小
  228. SetWindowPos(hwnd, NULL, rect.left, rect.top, newWidth, rect.bottom - rect.top, SWP_NOZORDER | SWP_NOACTIVATE);
  229. // 隐藏按钮 (假设按钮的句柄为 buttonHandle)
  230. ShowWindow(::GetDlgItem(hDlg, EXPAND_TOOLS), SW_HIDE);
  231. } break;
  232. case MIN_TOOLS: {
  233. RECT rect;
  234. GetWindowRect(hDlg, &rect);
  235. int currentWidth = rect.right - rect.left;
  236. int newHeight = 98; // 设置新的高度为
  237. SetWindowPos(hDlg, NULL, rect.left, rect.top, currentWidth, newHeight, SWP_NOZORDER | SWP_NOACTIVATE);
  238. } break;
  239. case IDOK:
  240. case IDCANCEL:
  241. EndDialog(hDlg, 0);
  242. break;
  243. }
  244. } catch (_com_error &e) {
  245. MessageBox(NULL, e.Description(), "Error", MB_ICONSTOP);
  246. EndOpt(cdr);
  247. }
  248. } else if (uMsg == WM_INITDIALOG) {
  249. SetWindowText(::GetDlgItem(hDlg, EXP_TEXT), "0");
  250. return 1;
  251. }
  252. return 0;
  253. }
  254. extern "C" __declspec(dllexport) DWORD APIENTRY AttachPlugin(VGCore::IVGAppPlugin **ppIPlugin) {
  255. *ppIPlugin = new ToolsBoxPlugin;
  256. return 0x100;
  257. }