C# WinForms启动画面导致主窗体尺寸变小控件错位问题
问题原因及解决方案
核心原因
- DPI感知上下文初始化错误:WinForms中,第一个通过
Application.Run启动的窗体决定了整个应用的DPI感知上下文。你当前先启动启动画面,主窗体仅通过form.Show()显示,未作为应用主窗体完成初始化,导致主窗体的DPI适配逻辑未正确触发,出现尺寸缩放异常。 - 启动画面与主窗体的DPI环境不匹配:若启动窗体未启用DPI感知,而主窗体设计时基于高DPI环境,主窗体在启动画面的上下文下显示时会被自动缩放,进而引发尺寸变小、控件错位。
修复步骤
1. 调整应用启动流程
让主窗体成为Application.Run的目标,确保其DPI上下文正确初始化。修改Program.cs:
static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); // 显示启动画面,直到完成后关闭 using (SplashScreenForm splash = new SplashScreenForm()) { splash.ShowDialog(); } // 启动主窗体作为应用核心入口 Application.Run(new ParentForm()); }
2. 修改启动画面结束逻辑
在进度完成时关闭启动画面,而非隐藏后直接显示主窗体:
private void timer1_Tick(object sender, EventArgs e) { progressBar1.Increment(4); if (progressBar1.Value >= 100) { timer1.Enabled = false; this.Close(); // 关闭启动画面,回到Program.cs执行主窗体启动 } }
3. 统一DPI感知设置
在项目中启用Per-Monitor DPI感知,避免跨屏幕缩放问题:
- 右键项目 → 属性 → 应用程序 → 点击「查看Windows设置」(或直接编辑
app.manifest) - 在
<application xmlns="urn:schemas-microsoft-com:asm.v3">节点下添加:<windowsSettings> <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true/PM</dpiAware> </windowsSettings>
内容的提问来源于stack exchange,提问作者sadegh
相关产品推荐
相关产品推荐

