ToolsBox.cpp 9.2 KB

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