ToolsBox.cpp 11 KB

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