1
0
Эх сурвалжийг харах

窗口扩展、最小化、恢复按钮按钮 保存窗口和VBA调用打开窗口

蘭雅sRGB 2 сар өмнө
parent
commit
a8a6048a03

BIN
09_BoundaryGroup/Makefile


+ 56 - 8
09_BoundaryGroup/ToolsBox.cpp

@@ -7,6 +7,7 @@
 
 corel *cdr = NULL;
 static HINSTANCE g_hResource = NULL;
+HICON  g_hIcon;         // 窗口图标句柄
 bool debug_flg = false; // 调试->高级模式
 char infobuf[256] = {0};
 BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {
@@ -116,9 +117,11 @@ STDMETHODIMP ToolsBoxPlugin::raw_OnLoad(VGCore::IVGApplication *Application) {
   return S_OK;
 }
 
+ToolsBoxPlugin* lycpg = nullptr;
 STDMETHODIMP ToolsBoxPlugin::raw_StartSession() {
   // 接口转交给cdr
   cdr = m_pApp;
+  lycpg = this;
 
   try {
     m_pApp->AddPluginCommand(_bstr_t("OpenToolsBox"), _bstr_t("Tools Box"), _bstr_t("打开工具窗口"));
@@ -170,19 +173,30 @@ void ToolsBoxPlugin::OpenToolsBox() {
   // 计算对话框窗口的宽度和高度
   RECT dlgRect;
   GetWindowRect(hDlgWnd, &dlgRect);
-  int dlgWidth = dlgRect.right - dlgRect.left;
-  int dlgHeight = dlgRect.bottom - dlgRect.top;
+  int w = dlgRect.right - dlgRect.left;
+  int h = dlgRect.bottom - dlgRect.top;
 
   // 计算对话框窗口的左上角坐标,使其居中显示
-  int x = (screenWidth - dlgWidth) / 2;
-  int y = (screenHeight - dlgHeight) / 2;
+  int x = (screenWidth - w) / 2;
+  int y = (screenHeight - h) / 2;
+
+  // 创建窗口数据实例      // 从文件加载
+  WinData win = {x, y, w, h};
+  win = loadWinData("window.dat", win);
 
   // 设置对话框窗口的位置
-  SetWindowPos(hDlgWnd, NULL, x, y, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
+  SetWindowPos(hDlgWnd, NULL, win.x, win.y, win.w, win.h, SWP_NOZORDER | SWP_NOACTIVATE);
   // 设置对话框窗口的父窗口  // #define GWL_HWNDPARENT      (-8)
   SetWindowLong(hDlgWnd, -8, (LONG)hAppWnd);
+
   // 显示对话框窗口
   ShowWindow(hDlgWnd, SW_SHOW);
+
+  // 使用 g_hResource 作为 HINSTANCE
+  g_hIcon = ::LoadIcon(g_hResource, MAKEINTRESOURCE(IDI_ICON1));
+  // 小图标:就是窗口左上角对应的那个图标
+  ::SendMessage(hDlgWnd, WM_SETICON, ICON_SMALL, (LPARAM)g_hIcon);
+
 }
 
 // MessageBox(NULL, "更新提示信息: 激活CorelDRAW窗口", "CPG代码测试", MB_ICONSTOP);
@@ -260,6 +274,7 @@ intptr_t CALLBACK ToolsBoxPlugin::DlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, L
         UPDATE_INFO_ACTIVE_CDRWND
       } break;
 
+//////////// 窗口扩展、最小化、恢复按钮按钮////////////////////////////////////////////////
       case EXPAND_TOOLS: {
         // 获取当前窗口的句柄
         HWND hwnd = GetActiveWindow();
@@ -267,7 +282,7 @@ intptr_t CALLBACK ToolsBoxPlugin::DlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, L
         RECT rect;
         GetWindowRect(hwnd, &rect);
         // 计算新的宽度
-        int newWidth = rect.right - rect.left + 125; // 增加125单位
+        int newWidth = rect.right - rect.left + 100; // 增加100单位
         // 移动窗口到新的大小
         SetWindowPos(hwnd, NULL, rect.left, rect.top, newWidth, rect.bottom - rect.top, SWP_NOZORDER | SWP_NOACTIVATE);
         // 隐藏按钮 (假设按钮的句柄为 buttonHandle)
@@ -278,11 +293,38 @@ intptr_t CALLBACK ToolsBoxPlugin::DlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, L
         RECT rect;
         GetWindowRect(hDlg, &rect);
         int currentWidth = rect.right - rect.left;
-        int newHeight = 98; // 设置新的高度为
-        SetWindowPos(hDlg, NULL, rect.left, rect.top, currentWidth, newHeight, SWP_NOZORDER | SWP_NOACTIVATE);
+        int currentHeight = rect.bottom - rect.top;
+        int h = 98;
+        SetWindowPos(hDlg, NULL, rect.left, rect.top, currentWidth, h, SWP_NOZORDER | SWP_NOACTIVATE);
+        ShowWindow(::GetDlgItem(hDlg, MIN_TOOLS), SW_HIDE);
+        
+        int x = rect.left;
+        int y = rect.top;
+        int w = currentWidth;
+        // 保存窗口位置
+        WinData win = {x, y, w, h};
+        saveWinData("window.dat", &win);
+      } break;
+
+      case RENEW_TOOLS: {
+        RECT rect;
+        GetWindowRect(hDlg, &rect);
+        int x = rect.left;
+        int y = rect.top;
+        int h = 232; // 恢复宽高
+        int w = 207;
+        SetWindowPos(hDlg, NULL, x, y, w, h, SWP_NOZORDER | SWP_NOACTIVATE);
+        ShowWindow(::GetDlgItem(hDlg, MIN_TOOLS), !SW_HIDE);
+        ShowWindow(::GetDlgItem(hDlg, EXPAND_TOOLS), !SW_HIDE);
+
+        // 保存窗口位置
+        WinData win = {x, y, w, h};
+        saveWinData("window.dat", &win);
+
       } break;
 
       case IDOK:
+        break;
       case IDCANCEL:
         EndDialog(hDlg, 0);
         break;
@@ -304,3 +346,9 @@ extern "C" __declspec(dllexport) DWORD APIENTRY AttachPlugin(VGCore::IVGAppPlugi
   *ppIPlugin = new ToolsBoxPlugin;
   return 0x100;
 }
+
+void open_lycpg() {
+    if (lycpg) {
+        lycpg->OpenToolsBox(); // 使用类的实例调用成员函数
+    }
+}

+ 14 - 13
09_BoundaryGroup/ToolsBox.rc

@@ -14,26 +14,27 @@
 // Dialog resources
 //
 LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
-IDD_TOOLS_BOX DIALOGEX 0, 0, 135, 128
+IDD_TOOLS_BOX DIALOGEX 0, 0, 134, 125
 STYLE DS_MODALFRAME | DS_SHELLFONT | WS_CAPTION | WS_POPUP | WS_SYSMENU
 CAPTION "蘭雅 CPG 插件 福利群版"
 FONT 8, "MS Shell Dlg", 135, 0, 1
 {
     AUTOCHECKBOX    "调试->高级模式", DEBUG_FLG, 142, 8, 70, 8, 0, WS_EX_LEFT
     CTEXT           "蘭雅 CorelDRAW CPG 插件\n2024.8.30 异形群组版", INFO_TEXT, 5, 104, 124, 18, SS_CENTER, WS_EX_LEFT
-    PUSHBUTTON      "方框智能群组", IDC_BOX_GROUP, 69, 2, 59, 20, 0, WS_EX_LEFT
-    PUSHBUTTON      "CQL轮廓色相同", IDC_CQL_OUTLINE, 4, 61, 59, 20, 0, WS_EX_LEFT
-    PUSHBUTTON      "边界画矩形", IDC_CLEAR_FILL, 69, 39, 59, 20, 0, WS_EX_LEFT
-    PUSHBUTTON      "批量镜像", IDC_SR_FLIP, 69, 60, 59, 20, 0, WS_EX_LEFT
-    PUSHBUTTON      "CQL颜色相同", IDC_CQL_FILL, 4, 81, 59, 20, 0, WS_EX_LEFT
-    PUSHBUTTON      "CQL尺寸相同", IDC_CQL_SIZE, 69, 81, 59, 20, 0, WS_EX_LEFT
-    PUSHBUTTON      "CDR复制到AI", IDC_CDR2AI, 4, 21, 59, 20, 0, WS_EX_LEFT
-    PUSHBUTTON      "AI粘贴到CDR", IDC_AI2CDR, 4, 41, 59, 20, 0, WS_EX_LEFT
-    LTEXT           "容差:(mm)", EXP_LT, 66, 27, 41, 8, SS_LEFT, WS_EX_LEFT
-    EDITTEXT        EXP_TEXT, 103, 24, 22, 14, NOT WS_TABSTOP | ES_AUTOHSCROLL, WS_EX_LEFT
+    PUSHBUTTON      "方框智能群组", IDC_BOX_GROUP, 69, 2, 56, 20, 0, WS_EX_LEFT
+    PUSHBUTTON      "CQL轮廓色相同", IDC_CQL_OUTLINE, 4, 62, 59, 19, 0, WS_EX_LEFT
+    PUSHBUTTON      "边界画矩形", IDC_CLEAR_FILL, 69, 42, 56, 19, 0, WS_EX_LEFT
+    PUSHBUTTON      "批量镜像", IDC_SR_FLIP, 69, 62, 56, 19, 0, WS_EX_LEFT
+    PUSHBUTTON      "CQL颜色相同", IDC_CQL_FILL, 4, 82, 59, 19, 0, WS_EX_LEFT
+    PUSHBUTTON      "CQL尺寸相同", IDC_CQL_SIZE, 69, 82, 56, 19, 0, WS_EX_LEFT
+    PUSHBUTTON      "CDR复制到AI", IDC_CDR2AI, 4, 22, 59, 19, 0, WS_EX_LEFT
+    PUSHBUTTON      "AI粘贴到CDR", IDC_AI2CDR, 4, 42, 59, 19, 0, WS_EX_LEFT
+    LTEXT           "容差:(mm)", EXP_LT, 66, 28, 41, 8, SS_LEFT, WS_EX_LEFT
+    EDITTEXT        EXP_TEXT, 102, 25, 22, 14, NOT WS_TABSTOP | ES_AUTOHSCROLL, WS_EX_LEFT
     PUSHBUTTON      "异形群组", ID_BOUNDARY_GROUP, 4, 2, 59, 20, 0, WS_EX_LEFT
-    PUSHBUTTON      "∧", MIN_TOOLS, 125, 23, 12, 17, BS_NOTIFY, WS_EX_LEFT
-    PUSHBUTTON      ">>", EXPAND_TOOLS, 129, 53, 13, 39, BS_NOTIFY, WS_EX_LEFT
+    PUSHBUTTON      "∨", RENEW_TOOLS, 126, 23, 12, 17, BS_NOTIFY, WS_EX_LEFT
+    PUSHBUTTON      "∧", MIN_TOOLS, 126, 6, 12, 17, BS_NOTIFY, WS_EX_LEFT
+    PUSHBUTTON      ">>", EXPAND_TOOLS, 127, 53, 13, 39, BS_NOTIFY, WS_EX_LEFT
 }
 
 

+ 4 - 0
09_BoundaryGroup/VBA_GMS/VbaCallCPG.bas

@@ -56,3 +56,7 @@ End Sub
 Sub BoundaryGroup()
  ret = vbadll(11, 0)
 End Sub
+
+Sub Open_LYCPG()
+ ret = vbadll(888, 8)
+End Sub

+ 57 - 0
09_BoundaryGroup/WinData.cpp

@@ -0,0 +1,57 @@
+// WinData.c
+#include "WinData.h"
+#include <stdlib.h>
+#include <string.h>
+#include <windows.h>
+
+#define MAX_PATH_LENGTH 260
+
+char dirPath[MAX_PATH_LENGTH];
+char* GetUserDir(const char* dirName) {
+  char userProfile[MAX_PATH_LENGTH];
+
+  if (GetEnvironmentVariable("USERPROFILE", userProfile, MAX_PATH_LENGTH) == 0) {
+    return NULL;
+  }
+  // 构建指定目录的完整路径
+  snprintf(dirPath, sizeof(dirPath), "%s\\%s", userProfile, dirName);
+
+  // 检查目录是否存在
+  DWORD ftyp = GetFileAttributes(dirPath);
+  if (ftyp == INVALID_FILE_ATTRIBUTES) {
+    if (CreateDirectory(dirPath, NULL) ||
+        GetLastError() == ERROR_ALREADY_EXISTS) {
+      printf("目录 '%s' 创建成功或已经存在。\n", dirPath);
+    } else {
+      return NULL;
+    }
+  }
+  return dirPath;
+}
+
+void saveWinData(const char *filename, const WinData *win) {
+  char* dir = GetUserDir("lyvba");
+  char bufile[MAX_PATH_LENGTH];
+  sprintf(bufile, "%s\\%s", dir, filename);
+  FILE *file = fopen(bufile, "wb");
+  if (!file) {
+    //  perror("无法打开文件进行写入");
+    return;
+  }
+  fwrite(win, sizeof(WinData), 1, file);
+  fclose(file);
+}
+
+WinData loadWinData(const char *filename, WinData win) {
+  char* dir = GetUserDir("lyvba");
+  char bufile[MAX_PATH_LENGTH];
+  sprintf(bufile, "%s\\%s", dir, filename);
+
+  FILE *file = fopen(bufile, "rb");
+  if (!file) {
+    return win; 
+  }
+  fread(&win, sizeof(WinData), 1, file);
+  fclose(file);
+  return win;
+}

+ 18 - 0
09_BoundaryGroup/WinData.h

@@ -0,0 +1,18 @@
+// WinData.h
+#ifndef WINDATA_H
+#define WINDATA_H
+
+#include <stdio.h>
+
+typedef struct {
+    int x;  // x 坐标
+    int y;  // y 坐标
+    int w;  // 宽度
+    int h;  // 高度
+} WinData;
+
+// 函数声明
+void saveWinData(const char* filename, const WinData* win);
+WinData loadWinData(const char *filename, WinData win);
+
+#endif // WINDATA_H

+ 1 - 0
09_BoundaryGroup/cdrapi.h

@@ -11,6 +11,7 @@
 #include <fstream>
 #include <chrono>
 #include <math.h>
+#include "WinData.h"
 
 #import "VGCoreAuto.tlb" \
 rename("GetCommandLine", "VGGetCommandLine") \

+ 1 - 0
09_BoundaryGroup/cdrapp.h

@@ -2,6 +2,7 @@
 #define CDRAPP_H_INCLUDED
 #include "cdrapi.h"
 
+
 bool fill_red(corel *cdr);
 bool cql_OutlineColor(corel *cdr);
 bool cql_FillColor(corel *cdr);

+ 2 - 1
09_BoundaryGroup/resource.h

@@ -4,7 +4,6 @@
 
 #define IDD_TOOLS_BOX                           100
 #define IDI_ICON1                               101
-#define EXPAND_TOOLS                            40015
 #define IDC_CQL_FILL                            40001
 #define IDC_CQL_OUTLINE                         40002
 #define IDC_CQL_SIZE                            40003
@@ -19,3 +18,5 @@
 #define ID_BOUNDARY_GROUP                       40012
 #define DEBUG_FLG                               40013
 #define MIN_TOOLS                               40014
+#define EXPAND_TOOLS                            40015
+#define RENEW_TOOLS                             40016

+ 4 - 0
09_BoundaryGroup/vbadll.cpp

@@ -3,6 +3,7 @@
 #include "cdrapp.h"
 
 extern corel *cdr;
+void open_lycpg();
 extern "C" __declspec(dllexport)
 int __stdcall vbadll(int code, double x =0.0){
 
@@ -52,6 +53,9 @@ try {
             run_BoundaryGroup(cdr);
         break;
 
+        case 888:
+            open_lycpg();
+        break;
 
         default:
             return 0;