简体   繁体   English

将开发从 100% 的 Windows 缩放转移到 150% 的 WinForms 窗口

[英]Moving development from 100% Windows scaling to 150% chops WinForms window

I use C# Winforms in Visual Studio 2010. Since I've upgraded my laptop from the old 1366 x 768 resolution laptop to a new laptop with a 1920x1080 resolution, I noticed a problem when continuing development on the new laptop.我在 Visual Studio 2010 中使用 C# Winforms。自从我将笔记本电脑从旧的 1366 x 768 分辨率笔记本电脑升级到分辨率为 1920x1080 的新笔记本电脑后,我在新笔记本电脑上继续开发时发现了一个问题。 I isolated the issue down to the new 150% Windows OS-wide scaling (Windows on the old laptop used 100% scaling).我将问题归结为新的 150% Windows 操作系统范围缩放(旧笔记本电脑上的 Windows 使用 100% 缩放)。 When I run the program, the right side of the window is strangely chopped off.当我运行程序时,窗口的右侧奇怪地被砍掉了。

Here's the working version on the old laptop (1000 pixels wide, or 1500 pixels in 150% scaled Windows mode):这是旧笔记本电脑上的工作版本(1000 像素宽,或 150% 缩放 Windows 模式下的 1500 像素):

老的

And here's the new version on the new laptop with the chopped window (1286 pixels wide, when it should be 1500 due to the 150% scaling).这是带有切碎窗口的新笔记本电脑上的新版本(1286 像素宽,由于 150% 的缩放比例应该是 1500)。

新的

Upon immediately loading the project into the new 150% scaled Windows setup and running the program, it appears to work fine, but as soon as you move one of the buttons so that VS updates the designer code (button 5 was moved as you can see), and then run, the window is chopped.立即将项目加载到新的 150% 缩放的 Windows 设置并运行该程序后,它似乎工作正常,但是只要您移动其中一个按钮以便 VS 更新设计器代码(如您所见,按钮 5 已移动) ),然后运行,窗口被砍。

Even when manually going into the Form1.Designer.cs code, and adjusting the ClientSize to a stupid high horizontal value like ClientSize = new System.Drawing.Size(3000, 709);即使手动进入 Form1.Designer.cs 代码,并将 ClientSize 调整为愚蠢的高水平值,例如ClientSize = new System.Drawing.Size(3000, 709); results in a window width that is limited to 1286 pixels at most.导致窗口宽度最多限制为 1286 像素。 (Not so) coincidentally, 1286 is around 1920 / 1.5. (并非如此)巧合的是,1286 大约是 1920 / 1.5。

Basically, what the heck is going on?基本上,到底发生了什么? This appears like a bug with VS's scaling management.这看起来像是 VS 缩放管理的错误。 To be clear, the problem occurs when moving development from a 100% Windows scaled screen to a 150% Windows scaled screen, on any system.需要明确的是,在任何系统上将开发从 100% Windows 缩放屏幕移动到 150% Windows 缩放屏幕时都会出现问题。 You don't need two laptops to reproduce the problem, it can all be reproduced on a single laptop (but make sure you log out and into Windows after switching from/to 100% or 150%).您不需要两台笔记本电脑来重现该问题,它可以在一台笔记本电脑上重现(但请确保在从 100% 或 150% 切换后退出并登录 Windows)。

Finally, to be clear, I know about setting the DPI-aware setting to true using an app.manifest file.最后,要清楚的是,我知道如何使用app.manifest文件将 DPI 感知设置设置为 true。 Although this allows clearer text and solves the chopped window problem, I do NOT want to go this route due to inconsistent changes in my program's GUI such as overlapping controls and misplaced items on some setups.虽然这允许更清晰的文本并解决了切碎的窗口问题,但我不想走这条路,因为我的程序 GUI 中的变化不一致,例如重叠的控件和某些设置上的错误放置的项目。

I've found two half-solutions so far: 到目前为止,我已经找到了两种解决方案:

First is upgrading to Visual Studio 2017 (which allows you to force 100% scaling in VS Forms designer even in 150% Windows scaling) 首先是升级到Visual Studio 2017(这使您可以在VS Forms Designer中强制进行100%缩放,即使Windows缩放为150%)

Second is keeping with an earlier version of Visual Studio (including VS 2010), but making sure Windows is set to 100% scaling. 其次是保持Visual Studio的早期版本(包括VS 2010),但要确保Windows设置为100%缩放。 If you've only just changed the scaling setting, make sure to log out and in of Windows to ensure the scaling is applied appropriately. 如果仅更改缩放设置,请确保注销并退出Windows,以确保正确应用缩放。

A relatively simple workaround is to create a Panel Control that is the size you desire for the window, send it to the first layer (right click the control in the designer> send to back).一个相对简单的解决方法是创建一个与窗口大小相同的Panel控件,将其发送到第一层(右键单击设计器中的控件> 发送回)。 Then set the form to AutoResize to fit that control.然后将表单设置为AutoResize以适合该控件。 (also set AutoSizeMode to GrowAndShrink in case the form appears larger than it should) (还将AutoSizeMode设置为GrowAndShrink ,以防表单看起来比应有的大)

This works great if you need Fixed forms but for Resizable forms you might want some extra code that resizes this panel when the form's resize event happens.如果您需要Fixed表单,这很有效,但对于Resizable表单,您可能需要一些额外的代码来在表单的调整大小事件发生时调整此面板的大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM