From d477d634e353e6ac0fdcd0950d03a699a9d8fe72 Mon Sep 17 00:00:00 2001 From: fofolee Date: Mon, 13 Jan 2025 20:44:13 +0800 Subject: [PATCH] =?UTF-8?q?window=E4=B8=8B=E7=B3=BB=E7=BB=9F=E5=BC=B9?= =?UTF-8?q?=E7=AA=97=E8=87=AA=E9=80=82=E5=BA=94=E7=BC=A9=E6=94=BE=E5=92=8C?= =?UTF-8?q?=E5=88=86=E8=BE=A8=E7=8E=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugin/lib/csharp/dialog.cs | 76 ++++++++++++++++++++++++++++++------- 1 file changed, 62 insertions(+), 14 deletions(-) diff --git a/plugin/lib/csharp/dialog.cs b/plugin/lib/csharp/dialog.cs index 96f4140..a1f50aa 100644 --- a/plugin/lib/csharp/dialog.cs +++ b/plugin/lib/csharp/dialog.cs @@ -11,14 +11,55 @@ public class DialogGenerator [DllImport("user32.dll")] private static extern bool SetProcessDPIAware(); - private const int DEFAULT_WIDTH = 900; - private const int DEFAULT_HEIGHT = 350; - private const int PADDING = 30; - private const int BUTTON_HEIGHT = 50; - private const int BUTTON_WIDTH = 140; - private const int INPUT_HEIGHT = 40; - private const int SPACING = 20; - private const int EM_SETRECT = 0xB3; + [DllImport("gdi32.dll")] + private static extern int GetDeviceCaps(IntPtr hdc, int nIndex); + + private const int LOGPIXELSX = 88; + private const int LOGPIXELSY = 90; + + // 基础尺寸常量 (96 DPI下的尺寸,当前尺寸/1.75) + private const int BASE_WIDTH = 515; // 900/1.75 ≈ 515 + private const int BASE_HEIGHT = 200; // 350/1.75 ≈ 200 + private const int BASE_PADDING = 17; // 30/1.75 ≈ 17 + private const int BASE_BUTTON_HEIGHT = 29; // 50/1.75 ≈ 29 + private const int BASE_BUTTON_WIDTH = 80; // 140/1.75 ≈ 80 + private const int BASE_INPUT_HEIGHT = 23; // 40/1.75 ≈ 23 + private const int BASE_SPACING = 11; // 20/1.75 ≈ 11 + + // 实际使用的缩放尺寸 + private static int DEFAULT_WIDTH; + private static int DEFAULT_HEIGHT; + private static int PADDING; + private static int BUTTON_HEIGHT; + private static int BUTTON_WIDTH; + private static int INPUT_HEIGHT; + private static int SPACING; + + // 添加获取DPI缩放比例的方法 + private static float GetScaleFactor() + { + using (Graphics g = Graphics.FromHwnd(IntPtr.Zero)) + { + IntPtr desktop = g.GetHdc(); + int dpiX = GetDeviceCaps(desktop, LOGPIXELSX); + g.ReleaseHdc(desktop); + return dpiX / 96f; + } + } + + // 初始化缩放尺寸的方法 + private static void InitializeScaledSizes() + { + float scaleFactor = GetScaleFactor(); + + DEFAULT_WIDTH = (int)(BASE_WIDTH * scaleFactor); + DEFAULT_HEIGHT = (int)(BASE_HEIGHT * scaleFactor); + PADDING = (int)(BASE_PADDING * scaleFactor); + BUTTON_HEIGHT = (int)(BASE_BUTTON_HEIGHT * scaleFactor); + BUTTON_WIDTH = (int)(BASE_BUTTON_WIDTH * scaleFactor); + INPUT_HEIGHT = (int)(BASE_INPUT_HEIGHT * scaleFactor); + SPACING = (int)(BASE_SPACING * scaleFactor); + } private static void InitializeDPIAwareness() { @@ -30,6 +71,8 @@ public class DialogGenerator public static DialogResult Show(string[] args) { + InitializeScaledSizes(); // 初始化缩放尺寸 + string type = GetArgumentValue(args, "-type"); string title = GetArgumentValue(args, "-title"); string content = GetArgumentValue(args, "-content"); @@ -91,14 +134,16 @@ public class DialogGenerator dialog.Width = DEFAULT_WIDTH; dialog.Height = DEFAULT_HEIGHT; dialog.StartPosition = FormStartPosition.CenterScreen; + dialog.AutoScaleMode = AutoScaleMode.None; // 禁用自动缩放 - // 使用 Microsoft YaHei UI 字体 - dialog.Font = new Font("Microsoft YaHei UI", 9F, FontStyle.Regular, GraphicsUnit.Point); + // 禁止调整窗口大小 + dialog.FormBorderStyle = FormBorderStyle.FixedDialog; + dialog.MaximizeBox = false; + dialog.MinimizeBox = false; - // 设置DPI感知 - dialog.AutoScaleMode = AutoScaleMode.Dpi; + float scaleFactor = GetScaleFactor(); + dialog.Font = new Font("Microsoft YaHei UI", 9F * scaleFactor, FontStyle.Regular, GraphicsUnit.Pixel); - // 设置文本渲染质量 dialog.Paint += delegate(object sender, PaintEventArgs e) { e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.ClearTypeGridFit; }; @@ -719,7 +764,10 @@ public class DialogGenerator public static void Main(string[] args) { - InitializeDPIAwareness(); + if (Environment.OSVersion.Version.Major >= 6) + { + SetProcessDPIAware(); + } Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Show(args);