ToolsBox.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. #include <cstdio>
  2. #include <windows.h>
  3. #include "resource.h"
  4. #import "VGCoreAuto.tlb" \
  5. rename("GetCommandLine", "VGGetCommandLine") \
  6. rename("CopyFile", "VGCore") \
  7. rename("FindWindow", "VGFindWindow")
  8. using namespace VGCore;
  9. static HINSTANCE g_hResource = NULL;
  10. BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
  11. {
  12. if (fdwReason == DLL_PROCESS_ATTACH) {
  13. g_hResource = (HINSTANCE)hinstDLL;
  14. }
  15. return TRUE;
  16. }
  17. class ToolsBoxPlugin : public VGCore::IVGAppPlugin
  18. {
  19. private:
  20. volatile ULONG m_ulRefCount;
  21. long m_lCookie;
  22. static INT_PTR CALLBACK DlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam);
  23. void OpenToolsBox();
  24. public:
  25. ToolsBoxPlugin();
  26. VGCore::IVGApplication* m_pApp;
  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. {
  33. ULONG ulCount = --m_ulRefCount;
  34. if (ulCount == 0) {
  35. delete this;
  36. }
  37. return ulCount;
  38. }
  39. // IDispatch
  40. public:
  41. STDMETHOD(GetTypeInfoCount)(UINT* pctinfo) { return E_NOTIMPL; }
  42. STDMETHOD(GetTypeInfo)(UINT iTInfo, LCID lcid, ITypeInfo** ppTInfo) { return E_NOTIMPL; }
  43. STDMETHOD(GetIDsOfNames)(REFIID riid, LPOLESTR* rgszNames, UINT cNames, LCID lcid, DISPID* rgDispId) { return E_NOTIMPL; }
  44. STDMETHOD(Invoke)(DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS* pDispParams, VARIANT* pVarResult, EXCEPINFO* pExcepInfo, UINT* puArgErr);
  45. // IVGAppPlugin
  46. public:
  47. STDMETHOD(raw_OnLoad)(VGCore::IVGApplication* Application);
  48. STDMETHOD(raw_StartSession)();
  49. STDMETHOD(raw_StopSession)();
  50. STDMETHOD(raw_OnUnload)();
  51. };
  52. ToolsBoxPlugin::ToolsBoxPlugin()
  53. {
  54. m_pApp = NULL;
  55. m_lCookie = 0;
  56. m_ulRefCount = 1;
  57. }
  58. STDMETHODIMP ToolsBoxPlugin::QueryInterface(REFIID riid, void** ppvObject)
  59. {
  60. HRESULT hr = S_OK;
  61. m_ulRefCount++;
  62. if (riid == IID_IUnknown) {
  63. *ppvObject = (IUnknown*)this;
  64. } else if (riid == IID_IDispatch) {
  65. *ppvObject = (IDispatch*)this;
  66. } else if (riid == __uuidof(VGCore::IVGAppPlugin)) {
  67. *ppvObject = (VGCore::IVGAppPlugin*)this;
  68. } else {
  69. m_ulRefCount--;
  70. hr = E_NOINTERFACE;
  71. }
  72. return hr;
  73. }
  74. STDMETHODIMP ToolsBoxPlugin::Invoke(DISPID dispIdMember, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS* pDispParams, VARIANT* pVarResult, EXCEPINFO* pExcepInfo, UINT* puArgErr)
  75. {
  76. switch (dispIdMember) {
  77. case 0x0014: // DISPID_APP_ONPLUGINCMD
  78. if (pDispParams != NULL && pDispParams->cArgs == 1) {
  79. _bstr_t strCmd(pDispParams->rgvarg[0].bstrVal);
  80. if (strCmd == _bstr_t("OpenToolsBox")) {
  81. // MessageBox(NULL, _bstr_t("OpenToolsBox"), _bstr_t("OpenToolsBox"), MB_ICONSTOP);
  82. OpenToolsBox();
  83. }
  84. }
  85. break;
  86. case 0x0015: // DISPID_APP_ONPLUGINCMDSTATE
  87. if (pDispParams != NULL && pDispParams->cArgs == 3) {
  88. _bstr_t strCmd(pDispParams->rgvarg[2].bstrVal);
  89. if (strCmd == _bstr_t("OpenToolsBox")) {
  90. *pDispParams->rgvarg[1].pboolVal = VARIANT_TRUE;
  91. }
  92. }
  93. break;
  94. }
  95. return S_OK;
  96. }
  97. STDMETHODIMP ToolsBoxPlugin::raw_OnLoad(VGCore::IVGApplication* Application)
  98. {
  99. m_pApp = Application;
  100. if (m_pApp) {
  101. m_pApp->AddRef();
  102. }
  103. return S_OK;
  104. }
  105. STDMETHODIMP ToolsBoxPlugin::raw_StartSession()
  106. {
  107. try {
  108. m_pApp->AddPluginCommand(_bstr_t("OpenToolsBox"), _bstr_t("Tools Box"), _bstr_t("打开工具窗口"));
  109. VGCore::ICUIControlPtr ctl = m_pApp->CommandBars->Item[_bstr_t("Standard")]->Controls->AddCustomButton(VGCore::cdrCmdCategoryPlugins, _bstr_t("OpenToolsBox"), 10, VARIANT_FALSE);
  110. ctl->SetIcon2(_bstr_t("guid://d2fdc0d9-09f8-4948-944c-4297395c05b7"));
  111. m_lCookie = m_pApp->AdviseEvents(this);
  112. } catch (_com_error &e) {
  113. MessageBox(NULL, e.Description(), _bstr_t("Error"), MB_ICONSTOP);
  114. }
  115. return S_OK;
  116. }
  117. STDMETHODIMP ToolsBoxPlugin::raw_StopSession()
  118. {
  119. try {
  120. m_pApp->UnadviseEvents(m_lCookie);
  121. m_pApp->RemovePluginCommand(_bstr_t("OpenToolsBox"));
  122. } catch (_com_error &e) {
  123. MessageBox(NULL, e.Description(), _bstr_t("Error"), MB_ICONSTOP);
  124. }
  125. return S_OK;
  126. }
  127. STDMETHODIMP ToolsBoxPlugin::raw_OnUnload()
  128. {
  129. if (m_pApp) {
  130. m_pApp->Release();
  131. m_pApp = NULL;
  132. }
  133. return S_OK;
  134. }
  135. void ToolsBoxPlugin::OpenToolsBox()
  136. {
  137. m_pApp->StartupMode = VGCore::cdrStartupDoNothing;
  138. INT_PTR nHandle = m_pApp->AppWindow->Handle;
  139. HWND hAppWnd = reinterpret_cast<HWND>(nHandle);
  140. // 创建非模态对话框
  141. HWND hDlgWnd = CreateDialogParam(g_hResource, MAKEINTRESOURCE(IDD_TOOLS_BOX), hAppWnd, DlgProc, (LPARAM)m_pApp);
  142. // 在创建对话框之前存储 m_pApp 指针
  143. SetWindowLongPtr(hDlgWnd, DWLP_USER, (LONG_PTR)m_pApp);
  144. // 获取屏幕的宽度和高度
  145. RECT rect;
  146. GetWindowRect(GetDesktopWindow(), &rect);
  147. int screenWidth = rect.right - rect.left;
  148. int screenHeight = rect.bottom - rect.top;
  149. // 计算对话框窗口的宽度和高度
  150. RECT dlgRect;
  151. GetWindowRect(hDlgWnd, &dlgRect);
  152. int dlgWidth = dlgRect.right - dlgRect.left;
  153. int dlgHeight = dlgRect.bottom - dlgRect.top;
  154. // 计算对话框窗口的左上角坐标,使其居中显示
  155. int x = (screenWidth - dlgWidth) / 2;
  156. int y = (screenHeight - dlgHeight) / 2;
  157. // 设置对话框窗口的位置
  158. SetWindowPos(hDlgWnd, NULL, x, y, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
  159. // 设置对话框窗口的父窗口 // #define GWL_HWNDPARENT (-8)
  160. SetWindowLong(hDlgWnd, -8, (LONG)hAppWnd);
  161. // 显示对话框窗口
  162. ShowWindow(hDlgWnd, SW_SHOW);
  163. }
  164. INT_PTR CALLBACK ToolsBoxPlugin::DlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  165. {
  166. // 从附加数据中获取 m_pApp 指针
  167. VGCore::IVGApplication* cdr = reinterpret_cast<VGCore::IVGApplication*>(GetWindowLongPtr(hDlg, DWLP_USER));
  168. if (uMsg == WM_COMMAND) {
  169. try {
  170. switch (LOWORD(wParam)) {
  171. case IDC_RED : {
  172. // sr.ApplyUniformFill CreateCMYKColor(0, 100, 100, 0)
  173. auto sr = cdr->ActiveSelectionRange;
  174. auto red = cdr->CreateCMYKColor(0, 100, 100, 0);
  175. sr->ApplyUniformFill(red);
  176. MessageBox(NULL, "选择物件填充红色", "填充颜色测试", MB_ICONSTOP);
  177. }
  178. break;
  179. case IDC_CQL_OUTLINE: {
  180. auto col = cdr->CreateCMYKColor(0, 100, 100, 0);
  181. auto s = cdr->ActiveShape;
  182. col-> CopyAssign (s->Outline->Color);
  183. col->ConvertToRGB();
  184. auto r = col->RGBRed;
  185. auto g = col->RGBGreen;
  186. auto b = col->RGBBlue;
  187. char buf[256] = { 0 };
  188. sprintf(buf, "@Outline.Color.rgb[.r='%d' And .g='%d' And .b='%d']", r, g, b);
  189. auto cql = _bstr_t(buf);
  190. // MessageBox(NULL, cql, "cql 轮廓颜色", MB_ICONSTOP);
  191. auto sr = cdr->ActivePage->Shapes->FindShapes(_bstr_t(), cdrNoShape, VARIANT_TRUE, cql);
  192. sr->CreateSelection();
  193. }
  194. // 将焦点返回到父窗口 关闭对话框窗口
  195. SetFocus(GetParent(hDlg));
  196. break;
  197. case IDC_CQL_FILL: {
  198. auto col = cdr->CreateCMYKColor(0, 100, 100, 0);
  199. auto s = cdr->ActiveShape;
  200. col-> CopyAssign (s->Fill->UniformColor);
  201. col->ConvertToRGB();
  202. auto r = col->RGBRed;
  203. auto g = col->RGBGreen;
  204. auto b = col->RGBBlue;
  205. char buf[256] = { 0 };
  206. sprintf(buf, "@Fill.Color.rgb[.r='%d' And .g='%d' And .b='%d']", r, g, b);
  207. auto cql = _bstr_t(buf);
  208. auto sr = cdr->ActivePage->Shapes->FindShapes(_bstr_t(), cdrNoShape, VARIANT_TRUE, cql);
  209. sr->CreateSelection();
  210. }
  211. // 将焦点返回到父窗口 关闭对话框窗口
  212. SetFocus(GetParent(hDlg));
  213. break;
  214. case IDC_CQL_SIZE: {
  215. cdr->ActiveDocument->Unit = cdrMillimeter;
  216. auto s = cdr->ActiveShape;
  217. char buf[256] = { 0 };
  218. sprintf(buf, "@width = {%lf mm} and @height = {%lf mm}", s->SizeWidth, s->SizeHeight);
  219. auto cql = _bstr_t(buf);
  220. // MessageBox(NULL, cql, "cql 尺寸相同", MB_ICONSTOP);
  221. auto sr = cdr->ActivePage->Shapes->FindShapes(_bstr_t(), cdrNoShape, VARIANT_TRUE, cql);
  222. sr->CreateSelection();
  223. }
  224. // 复制dll改名到CorelDRAW 插件目录
  225. // copy /y lycpg64.dll "C:\Program Files\Corel\CorelDRAW Technical Suite X6\Draw\Plugins64\lycpg64.cpg"
  226. SetFocus(GetParent(hDlg));
  227. break;
  228. case IDC_CLEAR_FILL:
  229. cdr->ActiveSelection->Fill->ApplyNoFill();
  230. break;
  231. case IDC_SR_FLIP: {
  232. auto sr = cdr->ActiveSelectionRange;
  233. // CorelDRAW Shapes 物件 Item 编号从1开始
  234. for (auto i = 0; i != sr->Count; i++)
  235. sr->Shapes->Item[i + 1]->Flip(VGCore::cdrFlipHorizontal);
  236. }
  237. break;
  238. case IDOK:
  239. case IDCANCEL:
  240. EndDialog(hDlg, 0);
  241. break;
  242. }
  243. } catch (_com_error &e) {
  244. MessageBox(NULL, e.Description(), "Error", MB_ICONSTOP);
  245. }
  246. } else if (uMsg == WM_INITDIALOG) {
  247. return 1;
  248. }
  249. return 0;
  250. }
  251. extern "C" __declspec(dllexport) DWORD APIENTRY AttachPlugin(VGCore::IVGAppPlugin** ppIPlugin)
  252. {
  253. *ppIPlugin = new ToolsBoxPlugin;
  254. return 0x100;
  255. }