ToolsBox.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. double exp = 0.0; // 默认初始容差值
  159. // 创建窗口数据实例 // 从文件加载
  160. WinData win = {x, y, w, h, exp};
  161. win = loadWinData("window.dat", win);
  162. // 设置对话框窗口的位置
  163. SetWindowPos(hDlgWnd, NULL, win.x, win.y, win.w, win.h, SWP_NOZORDER | SWP_NOACTIVATE);
  164. // 设置对话框窗口的父窗口 // #define GWL_HWNDPARENT (-8)
  165. SetWindowLong(hDlgWnd, -8, (LONG)hAppWnd);
  166. // 设置容差值 文本框的数值
  167. char expbuf[64] = {0};
  168. sprintf(expbuf, "%.1f", win.exp);
  169. SetWindowText(::GetDlgItem(hDlgWnd, EXP_TEXT), expbuf); // 设置为需要的数值
  170. // 显示对话框窗口
  171. ShowWindow(hDlgWnd, SW_SHOW);
  172. // 使用 g_hResource 作为 HINSTANCE
  173. g_hIcon = ::LoadIcon(g_hResource, MAKEINTRESOURCE(IDI_ICON1));
  174. // 小图标:就是窗口左上角对应的那个图标
  175. ::SendMessage(hDlgWnd, WM_SETICON, ICON_SMALL, (LPARAM)g_hIcon);
  176. }
  177. // MessageBox(NULL, "更新提示信息: 激活CorelDRAW窗口", "CPG代码测试", MB_ICONSTOP);
  178. #define UPDATE_INFO_ACTIVE_CDRWND \
  179. PutTextValue(hDlg, INFO_TEXT, infobuf); \
  180. Active_CorelWindows(hDlg);
  181. intptr_t CALLBACK ToolsBoxPlugin::DlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) {
  182. // 从附加数据中获取 m_pApp 指针
  183. VGCore::IVGApplication *cdr = reinterpret_cast<VGCore::IVGApplication *>(
  184. GetWindowLongPtr(hDlg, DWLP_USER));
  185. if (uMsg == WM_COMMAND) {
  186. try {
  187. switch (LOWORD(wParam)) {
  188. case ID_BOUNDARY_GROUP: {
  189. if (BST_CHECKED == IsDlgButtonChecked(hDlg, DEBUG_FLG))
  190. debug_flg = true;
  191. else
  192. debug_flg = false;
  193. double exp = GetTextValue(hDlg, EXP_TEXT);
  194. run_BoundaryGroup(cdr);
  195. UPDATE_INFO_ACTIVE_CDRWND
  196. } break;
  197. case IDC_BOX_GROUP: {
  198. double exp = GetTextValue(hDlg, EXP_TEXT);
  199. AutoMakeSelection(cdr);
  200. Box_AutoGroup(cdr, exp);
  201. UPDATE_INFO_ACTIVE_CDRWND
  202. } break;
  203. case IDC_CQL_OUTLINE:
  204. cql_OutlineColor(cdr);
  205. UPDATE_INFO_ACTIVE_CDRWND
  206. break;
  207. case IDC_CQL_FILL:
  208. cql_FillColor(cdr);
  209. UPDATE_INFO_ACTIVE_CDRWND
  210. break;
  211. case IDC_CQL_SIZE:
  212. cql_SameSize(cdr);
  213. UPDATE_INFO_ACTIVE_CDRWND
  214. break;
  215. case IDC_CLEAR_FILL: {
  216. double exp = GetTextValue(hDlg, EXP_TEXT);
  217. AutoMakeSelection(cdr);
  218. BBox_DrawRectangle(cdr, exp);
  219. UPDATE_INFO_ACTIVE_CDRWND
  220. } break;
  221. case IDC_SR_FLIP:
  222. Shapes_Filp(cdr);
  223. break;
  224. case IDC_CDR2AI: {
  225. CdrCopy_to_AdobeAI(cdr);
  226. sprintf(infobuf, "把CorelDRAW软件中选择物件复制到剪贴板,请切换到AI软件粘贴");
  227. UPDATE_INFO_ACTIVE_CDRWND
  228. } break;
  229. case IDC_AI2CDR: {
  230. AdobeAI_Copy_ImportCdr(cdr);
  231. sprintf(infobuf, "请先在AI软件选择物件复制,再切换到CorelDRAW软件点执行本功能");
  232. UPDATE_INFO_ACTIVE_CDRWND
  233. } break;
  234. //////////// 窗口扩展、最小化、恢复按钮按钮////////////////////////////////////////////////
  235. case EXPAND_TOOLS: {
  236. // 获取当前窗口的句柄
  237. HWND hwnd = GetActiveWindow();
  238. // 获取当前窗口的矩形区域
  239. RECT rect;
  240. GetWindowRect(hwnd, &rect);
  241. // 计算新的宽度
  242. int newWidth = rect.right - rect.left + 120; // 增加120单位
  243. // 移动窗口到新的大小
  244. SetWindowPos(hwnd, NULL, rect.left, rect.top, newWidth, rect.bottom - rect.top, SWP_NOZORDER | SWP_NOACTIVATE);
  245. // 隐藏按钮 (假设按钮的句柄为 buttonHandle)
  246. ShowWindow(::GetDlgItem(hDlg, EXPAND_TOOLS), SW_HIDE);
  247. } break;
  248. case MIN_TOOLS: {
  249. RECT rect;
  250. GetWindowRect(hDlg, &rect);
  251. int currentWidth = rect.right - rect.left;
  252. int currentHeight = rect.bottom - rect.top;
  253. int h = 98;
  254. SetWindowPos(hDlg, NULL, rect.left, rect.top, currentWidth, h, SWP_NOZORDER | SWP_NOACTIVATE);
  255. ShowWindow(::GetDlgItem(hDlg, MIN_TOOLS), SW_HIDE);
  256. double exp = GetTextValue(hDlg, EXP_TEXT);
  257. int x = rect.left;
  258. int y = rect.top;
  259. int w = currentWidth;
  260. // 保存窗口位置
  261. WinData win = {x, y, w, h, exp};
  262. saveWinData("window.dat", &win);
  263. } break;
  264. case RENEW_TOOLS: {
  265. RECT rect;
  266. GetWindowRect(hDlg, &rect);
  267. int x = rect.left;
  268. int y = rect.top;
  269. int h = 240; // 恢复宽高
  270. int w = 220;
  271. SetWindowPos(hDlg, NULL, x, y, w, h, SWP_NOZORDER | SWP_NOACTIVATE);
  272. ShowWindow(::GetDlgItem(hDlg, MIN_TOOLS), !SW_HIDE);
  273. ShowWindow(::GetDlgItem(hDlg, EXPAND_TOOLS), !SW_HIDE);
  274. double exp = GetTextValue(hDlg, EXP_TEXT);
  275. // 保存窗口位置
  276. WinData win = {x, y, w, h, exp};
  277. saveWinData("window.dat", &win);
  278. } break;
  279. case IDOK:
  280. break;
  281. // 关闭CPG插件,保存窗口位置
  282. case IDCANCEL:{
  283. RECT rect;
  284. GetWindowRect(hDlg, &rect);
  285. int w = rect.right - rect.left;
  286. int h = rect.bottom - rect.top;
  287. int x = rect.left;
  288. int y = rect.top;
  289. double exp = GetTextValue(hDlg, EXP_TEXT);
  290. // 保存窗口位置
  291. WinData win = {x, y, w, h, exp};
  292. saveWinData("window.dat", &win);
  293. EndDialog(hDlg, 0);
  294. } break;
  295. }
  296. } catch (_com_error &e) {
  297. MessageBox(NULL, e.Description(), "Error", MB_ICONSTOP);
  298. EndOpt(cdr);
  299. }
  300. } else if (uMsg == WM_INITDIALOG) {
  301. SetWindowText(::GetDlgItem(hDlg, EXP_TEXT), "0");
  302. return 1;
  303. }
  304. return 0;
  305. }
  306. extern "C" __declspec(dllexport) DWORD APIENTRY AttachPlugin(VGCore::IVGAppPlugin **ppIPlugin) {
  307. *ppIPlugin = new ToolsBoxPlugin;
  308. return 0x100;
  309. }
  310. void open_lycpg() {
  311. if (lycpg) {
  312. lycpg->OpenToolsBox(); // 使用类的实例调用成员函数
  313. }
  314. }