开发者

GetWindowPlacement/SetWindowPlacement not working in WinForms for high DPI

开发者 https://www.devze.com 2022-12-26 13:10 出处:网络
I\'ve got a legacy WinForms app and I want to save the window position and size across sessions. I\'ve bee开发者_Go百科n using GetWindowPlacement and SetWindowPlacement during the FormClosing and Load

I've got a legacy WinForms app and I want to save the window position and size across sessions. I've bee开发者_Go百科n using GetWindowPlacement and SetWindowPlacement during the FormClosing and Load events. The problem I'm getting is that at higher DPI settings (Such as Medium, size at 125%) the sizes keep inflating. I'll call SetWindowPlacement on it with a certain size, but when GetWindowPlacement is called, those numbers come back 25% bigger, even though the window was the same size all along. The same sort of problem exists when saving the size of a resizable element within the form.

Now this works fine if I create a new WinForms project: The size stays stable even when running at the higher DPI. I'm guessing there's some legacy setting in the bowels of the project or some arcane Form setting that's messing it up, but I can't find out where.

I've called IsProcessDPIAware on both projects and both are true. Does anyone know what might be causing this?


Sounds like you are somehow triggering scaling, as selected by the AutoScaleMode property of the form. The difference between your two projects would be the AutoScaleDimensions property, visible in the Designer.cs file.

Not sure why this would cause a problem, but the Form class already uses GetWindowPlacement() internally, RecreateHandleCore() and UpdateWindowState() methods. To get real help, I assume you'll need to post a repro project somewhere.


I found this offending setting in the form's .resx file:

<data name="$this.AutoScaleBaseSize" type="System.Drawing.Size, System.Drawing">
  <value>5, 13</value>
</data>

When this was present, VS would automatically change the AutoScaleBaseSize to work for your DPI, but no one else's. For everyone else, the form would constantly grow or shrink.

Choosing AutoScaleMode = Font in the designer properties panel caused VS to kick in and "modernize" the font scaling settings. Now it works for all DPIs.

0

精彩评论

暂无评论...
验证码 换一张
取 消