cpg_icon01.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #import "VGCoreAuto.tlb" // 导入VGCoreAuto类型库: 这个文件在CorelDRAW 软件目录下可以找到
  2. #define corel VGCore::IVGApplication
  3. using namespace VGCore;
  4. bool cql_OutlineColor(corel *cdr);
  5. int cpg_main() {
  6. // 初始化COM库,使用多线程公寓模型
  7. HRESULT hr = CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
  8. if (SUCCEEDED(hr)) {
  9. // 创建一个指向CorelDRAW应用程序的指针,经测试 CorelDRAW 16 可以正常绑定, 安装毛子版的 CorelDRAW 2020 无法正常绑定
  10. VGCore::IVGApplicationPtr app(L"CorelDRAW.Application.16");
  11. // 设置应用程序为可见状态
  12. app->Visible = VARIANT_TRUE;
  13. // 获取当前活动的文档,如果没有则创建一个新文档
  14. auto doc = app->ActiveDocument;
  15. if (!doc)
  16. doc = app->CreateDocument();
  17. cql_OutlineColor(app);
  18. // 清理COM库的初始化
  19. CoUninitialize();
  20. }
  21. return 0;
  22. }
  23. bool cql_OutlineColor(corel *cdr)
  24. {
  25. auto col = cdr->CreateCMYKColor(0, 100, 100, 0);
  26. auto s = cdr->ActiveShape;
  27. col-> CopyAssign(s->Outline->Color);
  28. col->ConvertToRGB();
  29. auto r = col->RGBRed;
  30. auto g = col->RGBGreen;
  31. auto b = col->RGBBlue;
  32. char buf[256] = { 0 };
  33. sprintf(buf, "@Outline.Color.rgb[.r='%d' And .g='%d' And .b='%d']", r, g, b);
  34. auto cql = _bstr_t(buf);
  35. auto sr = cdr->ActivePage->Shapes->FindShapes(_bstr_t(), cdrNoShape, VARIANT_TRUE, cql);
  36. sr->CreateSelection();
  37. return true;
  38. }
  39. #include <windows.h>
  40. // 定义按钮的ID
  41. #define ID_BUTTON_TOOLS 1
  42. #define ID_BUTTON_MOVE 2
  43. #define ID_BUTTON_CLOSE 3
  44. // 声明cpg_main函数
  45. int cpg_main();
  46. // 窗口过程函数,处理窗口消息
  47. LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  48. {
  49. // 根据消息类型处理不同的消息
  50. switch (uMsg) {
  51. case WM_COMMAND:
  52. // 处理按钮点击事件
  53. switch (LOWORD(wParam)) {
  54. case ID_BUTTON_TOOLS:{
  55. // 显示信息对话框
  56. // MessageBox(hwnd, "LYCPG CQL_OutlineColor!", "Info", MB_OK);
  57. // 调用app.cpp文件中的main函数
  58. cpg_main();
  59. }
  60. break;
  61. case ID_BUTTON_MOVE: {
  62. // 移动窗口到屏幕右上角
  63. MoveWindow(hwnd, GetSystemMetrics(SM_CXSCREEN) / 2, 0, 90, 30, TRUE);
  64. break;
  65. }
  66. case ID_BUTTON_CLOSE:
  67. // 发送关闭消息,关闭窗口
  68. PostMessage(hwnd, WM_CLOSE, 0, 0);
  69. break;
  70. }
  71. break;
  72. case WM_DESTROY:
  73. // 当窗口销毁时,发送退出消息
  74. PostQuitMessage(0);
  75. return 0;
  76. default:
  77. // 调用默认的窗口过程函数处理其他消息
  78. return DefWindowProc(hwnd, uMsg, wParam, lParam);
  79. }
  80. return 0;
  81. }
  82. HICON LoadSystemIcon(LPCTSTR iconName) { return LoadIcon(NULL, iconName); }
  83. // Windows程序的入口点
  84. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
  85. {
  86. // 窗口类名
  87. const char CLASS_NAME[] = "NoTitleWindow";
  88. // 初始化窗口类结构体
  89. WNDCLASS wc = {};
  90. // 设置窗口过程函数
  91. wc.lpfnWndProc = WindowProc;
  92. // 设置实例句柄
  93. wc.hInstance = hInstance;
  94. // 设置类名为常量定义的名称
  95. wc.lpszClassName = CLASS_NAME;
  96. // 设置背景刷子为窗口默认颜色
  97. wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  98. // 设置窗口风格为水平和垂直重绘
  99. wc.style = CS_HREDRAW | CS_VREDRAW;
  100. // 注册窗口类
  101. RegisterClass(&wc);
  102. // 窗口宽度和高度
  103. int windowWidth = 90; int windowHeight = 30;
  104. // 计算窗口显示位置
  105. int x = (GetSystemMetrics(SM_CXSCREEN) - windowWidth) / 2;
  106. int y = (GetSystemMetrics(SM_CYSCREEN) - windowHeight) / 2;
  107. // 创建主窗口
  108. HWND hwnd = CreateWindowEx(WS_EX_TOPMOST, CLASS_NAME, NULL, WS_POPUP, x, y, windowWidth, windowHeight, NULL, NULL, hInstance, NULL);
  109. if (hwnd == NULL) { return 0; }
  110. // 加载系统图标
  111. HICON hIconTools = LoadSystemIcon(IDI_APPLICATION);
  112. HICON hIconMove = LoadSystemIcon(IDI_ASTERISK);
  113. HICON hIconClose = LoadSystemIcon(IDI_ERROR);
  114. // 创建工具按钮
  115. HWND hButtonTools = CreateWindow("BUTTON", "LYCPG", WS_VISIBLE | WS_CHILD | BS_FLAT, 0, 0, 60, 30, hwnd,
  116. (HMENU)ID_BUTTON_TOOLS, (HINSTANCE)GetWindowLongPtr(hwnd, GWLP_HINSTANCE), NULL);
  117. // 创建移动按钮
  118. HWND hButtonMove = CreateWindow("BUTTON", NULL, WS_VISIBLE | WS_CHILD | BS_FLAT | BS_ICON, 60, 15, 30, 15, hwnd,
  119. (HMENU)ID_BUTTON_MOVE, (HINSTANCE)GetWindowLongPtr(hwnd, GWLP_HINSTANCE), NULL);
  120. SendMessage(hButtonMove, BM_SETIMAGE, IMAGE_ICON, (LPARAM)hIconMove);
  121. // 创建“X”按钮
  122. HWND hButtonClose = CreateWindow("BUTTON", NULL, WS_VISIBLE | WS_CHILD | BS_FLAT | BS_ICON, 60, 0, 30, 15, hwnd,
  123. (HMENU)ID_BUTTON_CLOSE, (HINSTANCE)GetWindowLongPtr(hwnd, GWLP_HINSTANCE), NULL);
  124. SendMessage(hButtonClose, BM_SETIMAGE, IMAGE_ICON, (LPARAM)hIconClose);
  125. // 显示和更新窗口
  126. ShowWindow(hwnd, nShowCmd);
  127. UpdateWindow(hwnd);
  128. // 消息循环
  129. MSG msg;
  130. while (GetMessage(&msg, NULL, 0, 0)) {
  131. TranslateMessage(&msg);
  132. DispatchMessage(&msg);
  133. }
  134. return 0;
  135. }