ToolsBox.cpp 10 KB

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