ToolsBox.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. m_lCookie = m_pApp->AdviseEvents(this);
  114. } catch (_com_error &e) {
  115. MessageBox(NULL, e.Description(), _bstr_t("Error"), MB_ICONSTOP);
  116. }
  117. return S_OK;
  118. }
  119. STDMETHODIMP ToolsBoxPlugin::raw_StopSession() {
  120. try {
  121. m_pApp->UnadviseEvents(m_lCookie);
  122. m_pApp->RemovePluginCommand(_bstr_t("OpenToolsBox"));
  123. } catch (_com_error &e) {
  124. MessageBox(NULL, e.Description(), _bstr_t("Error"), MB_ICONSTOP);
  125. }
  126. return S_OK;
  127. }
  128. STDMETHODIMP ToolsBoxPlugin::raw_OnUnload() {
  129. if (m_pApp) {
  130. m_pApp->Release();
  131. m_pApp = NULL;
  132. }
  133. return S_OK;
  134. }
  135. void ToolsBoxPlugin::OpenToolsBox() {
  136. m_pApp->StartupMode = VGCore::cdrStartupDoNothing;
  137. intptr_t nHandle = m_pApp->AppWindow->Handle;
  138. HWND hAppWnd = reinterpret_cast<HWND>(nHandle);
  139. // 创建非模态对话框
  140. HWND hDlgWnd = CreateDialogParam(g_hResource, MAKEINTRESOURCE(IDD_TOOLS_BOX), hAppWnd, DlgProc, (LPARAM)m_pApp);
  141. // 在创建对话框之前存储 m_pApp 指针
  142. SetWindowLongPtr(hDlgWnd, DWLP_USER, (LONG_PTR)m_pApp);
  143. // 获取屏幕的宽度和高度
  144. RECT rect;
  145. GetWindowRect(GetDesktopWindow(), &rect);
  146. int screenWidth = rect.right - rect.left;
  147. int screenHeight = rect.bottom - rect.top;
  148. // 计算对话框窗口的宽度和高度
  149. RECT dlgRect;
  150. GetWindowRect(hDlgWnd, &dlgRect);
  151. int w = dlgRect.right - dlgRect.left;
  152. int h = dlgRect.bottom - dlgRect.top;
  153. // 计算对话框窗口的左上角坐标,使其居中显示
  154. int x = (screenWidth - w) / 2;
  155. int y = (screenHeight - h) / 2;
  156. // 创建窗口数据实例 // 从文件加载
  157. WinData win = {x, y, w, h};
  158. win = loadWinData("window.dat", win);
  159. // 设置对话框窗口的位置
  160. SetWindowPos(hDlgWnd, NULL, win.x, win.y, win.w, win.h, SWP_NOZORDER | SWP_NOACTIVATE);
  161. // 设置对话框窗口的父窗口 // #define GWL_HWNDPARENT (-8)
  162. SetWindowLong(hDlgWnd, -8, (LONG)hAppWnd);
  163. // 显示对话框窗口
  164. ShowWindow(hDlgWnd, SW_SHOW);
  165. // 使用 g_hResource 作为 HINSTANCE
  166. g_hIcon = ::LoadIcon(g_hResource, MAKEINTRESOURCE(IDI_ICON1));
  167. // 小图标:就是窗口左上角对应的那个图标
  168. ::SendMessage(hDlgWnd, WM_SETICON, ICON_SMALL, (LPARAM)g_hIcon);
  169. }
  170. // MessageBox(NULL, "更新提示信息: 激活CorelDRAW窗口", "CPG代码测试", MB_ICONSTOP);
  171. #define UPDATE_INFO_ACTIVE_CDRWND \
  172. PutTextValue(hDlg, INFO_TEXT, infobuf); \
  173. Active_CorelWindows(hDlg);
  174. intptr_t CALLBACK ToolsBoxPlugin::DlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam) {
  175. // 从附加数据中获取 m_pApp 指针
  176. VGCore::IVGApplication *cdr = reinterpret_cast<VGCore::IVGApplication *>(
  177. GetWindowLongPtr(hDlg, DWLP_USER));
  178. if (uMsg == WM_COMMAND) {
  179. try {
  180. switch (LOWORD(wParam)) {
  181. case ID_BOUNDARY_GROUP: {
  182. if (BST_CHECKED == IsDlgButtonChecked(hDlg, DEBUG_FLG))
  183. debug_flg = true;
  184. else
  185. debug_flg = false;
  186. double exp = GetTextValue(hDlg, EXP_TEXT);
  187. run_BoundaryGroup(cdr);
  188. UPDATE_INFO_ACTIVE_CDRWND
  189. } break;
  190. case IDC_BOX_GROUP: {
  191. double exp = GetTextValue(hDlg, EXP_TEXT);
  192. AutoMakeSelection(cdr);
  193. Box_AutoGroup(cdr, exp);
  194. UPDATE_INFO_ACTIVE_CDRWND
  195. } break;
  196. case IDC_CQL_OUTLINE:
  197. cql_OutlineColor(cdr);
  198. UPDATE_INFO_ACTIVE_CDRWND
  199. break;
  200. case IDC_CQL_FILL:
  201. cql_FillColor(cdr);
  202. UPDATE_INFO_ACTIVE_CDRWND
  203. break;
  204. case IDC_CQL_SIZE:
  205. cql_SameSize(cdr);
  206. UPDATE_INFO_ACTIVE_CDRWND
  207. break;
  208. case IDC_CLEAR_FILL: {
  209. double exp = GetTextValue(hDlg, EXP_TEXT);
  210. AutoMakeSelection(cdr);
  211. BBox_DrawRectangle(cdr, exp);
  212. UPDATE_INFO_ACTIVE_CDRWND
  213. } break;
  214. case IDC_SR_FLIP:
  215. Shapes_Filp(cdr);
  216. break;
  217. case IDC_CDR2AI: {
  218. CdrCopy_to_AdobeAI(cdr);
  219. sprintf(infobuf, "把CorelDRAW软件中选择物件复制到剪贴板,请切换到AI软件粘贴");
  220. UPDATE_INFO_ACTIVE_CDRWND
  221. } break;
  222. case IDC_AI2CDR: {
  223. AdobeAI_Copy_ImportCdr(cdr);
  224. sprintf(infobuf, "请先在AI软件选择物件复制,再切换到CorelDRAW软件点执行本功能");
  225. UPDATE_INFO_ACTIVE_CDRWND
  226. } break;
  227. //////////// 窗口扩展、最小化、恢复按钮按钮////////////////////////////////////////////////
  228. case EXPAND_TOOLS: {
  229. // 获取当前窗口的句柄
  230. HWND hwnd = GetActiveWindow();
  231. // 获取当前窗口的矩形区域
  232. RECT rect;
  233. GetWindowRect(hwnd, &rect);
  234. // 计算新的宽度
  235. int newWidth = rect.right - rect.left + 100; // 增加100单位
  236. // 移动窗口到新的大小
  237. SetWindowPos(hwnd, NULL, rect.left, rect.top, newWidth, rect.bottom - rect.top, SWP_NOZORDER | SWP_NOACTIVATE);
  238. // 隐藏按钮 (假设按钮的句柄为 buttonHandle)
  239. ShowWindow(::GetDlgItem(hDlg, EXPAND_TOOLS), SW_HIDE);
  240. } break;
  241. case MIN_TOOLS: {
  242. RECT rect;
  243. GetWindowRect(hDlg, &rect);
  244. int currentWidth = rect.right - rect.left;
  245. int currentHeight = rect.bottom - rect.top;
  246. int h = 98;
  247. SetWindowPos(hDlg, NULL, rect.left, rect.top, currentWidth, h, SWP_NOZORDER | SWP_NOACTIVATE);
  248. ShowWindow(::GetDlgItem(hDlg, MIN_TOOLS), SW_HIDE);
  249. int x = rect.left;
  250. int y = rect.top;
  251. int w = currentWidth;
  252. // 保存窗口位置
  253. WinData win = {x, y, w, h};
  254. saveWinData("window.dat", &win);
  255. } break;
  256. case RENEW_TOOLS: {
  257. RECT rect;
  258. GetWindowRect(hDlg, &rect);
  259. int x = rect.left;
  260. int y = rect.top;
  261. int h = 232; // 恢复宽高
  262. int w = 207;
  263. SetWindowPos(hDlg, NULL, x, y, w, h, SWP_NOZORDER | SWP_NOACTIVATE);
  264. ShowWindow(::GetDlgItem(hDlg, MIN_TOOLS), !SW_HIDE);
  265. ShowWindow(::GetDlgItem(hDlg, EXPAND_TOOLS), !SW_HIDE);
  266. // 保存窗口位置
  267. WinData win = {x, y, w, h};
  268. saveWinData("window.dat", &win);
  269. } break;
  270. case IDOK:
  271. break;
  272. case IDCANCEL:
  273. EndDialog(hDlg, 0);
  274. break;
  275. }
  276. } catch (_com_error &e) {
  277. MessageBox(NULL, e.Description(), "Error", MB_ICONSTOP);
  278. EndOpt(cdr);
  279. }
  280. } else if (uMsg == WM_INITDIALOG) {
  281. SetWindowText(::GetDlgItem(hDlg, EXP_TEXT), "0");
  282. return 1;
  283. }
  284. return 0;
  285. }
  286. extern "C" __declspec(dllexport) DWORD APIENTRY AttachPlugin(VGCore::IVGAppPlugin **ppIPlugin) {
  287. *ppIPlugin = new ToolsBoxPlugin;
  288. return 0x100;
  289. }
  290. void open_lycpg() {
  291. if (lycpg) {
  292. lycpg->OpenToolsBox(); // 使用类的实例调用成员函数
  293. }
  294. }