简体   繁体   English

仅使用 GetWindowPlacement 中的 NormalPosition 保存和恢复表格 position 和大小

[英]Save and restore form position and size using only NormalPosition from GetWindowPlacement

In .NET Framework 4.6 and Windows Forms, I have a borderless form, without title bar and without minimize and maximize buttons, resizable (using custom code).在 .NET Framework 4.6 和 Windows Forms 中,我有一个无边框表单,没有标题栏,也没有最小化和最大化按钮,可调整大小(使用自定义代码)。

I'd like to save and restore the form location and size using GetWindowPlacement and SetWindowPlacement .我想使用GetWindowPlacementSetWindowPlacement保存和恢复表单位置和大小。

I'm getting this kind of values from GetWindowPlacement :我从GetWindowPlacement得到这种值:

<WindowPlacement>
  <Length>44</Length>
  <Flags>0</Flags>
  <ShowCmd>1</ShowCmd>
  <MinPosition>
    <X>-1</X>
    <Y>-1</Y>
  </MinPosition>
  <MaxPosition>
    <X>-1</X>
    <Y>-1</Y>
  </MaxPosition>
  <NormalPosition>
    <Left>949</Left>
    <Top>839</Top>
    <Right>1882</Right>
    <Bottom>1054</Bottom>
  </NormalPosition>
</WindowPlacement>

My form/window doesn't have minimize and maximize buttons, for all intended purposes it's not designed to be minimized/maximized, so it should always be restored to its normal window state.我的窗体/窗口没有最小化和最大化按钮,出于所有预期目的,它并非设计为最小化/最大化,因此应始终将其恢复为正常 window state。

Therefore I'm thinking only to save the NormalPosition rectangle from the WindowPlacement to application settings.因此,我只想将WindowPlacement中的NormalPosition矩形保存到应用程序设置中。

When loading the form I intent do create a WindowPlacement structure with the NormalPosition fields loaded from settings, and fill the other values with defaults ( ShowCmd to 1, MinPosition to -1, -1, etc.)加载表单时,我打算创建一个WindowPlacement结构,其中包含从设置中加载的NormalPosition字段,并使用默认值填充其他值( ShowCmd为 1, MinPosition为 -1、-1 等)

What worries me is this quote from the docs :令我担心的是文档中的这句话:

The coordinates used in a WINDOWPLACEMENT structure should be used only by the GetWindowPlacement and SetWindowPlacement functions. WINDOWPLACEMENT 结构中使用的坐标只能由 GetWindowPlacement 和 SetWindowPlacement 函数使用。

Should I be worried?我应该担心吗? Can I get some kind of weird behavior with my plan of only saving the NormalPosition ?仅保存NormalPosition的计划是否会导致某种奇怪的行为?

The coordinates used in a WINDOWPLACEMENT structure should be used only by the GetWindowPlacement and SetWindowPlacement functions. WINDOWPLACEMENT 结构中使用的坐标只能由 GetWindowPlacement 和 SetWindowPlacement 函数使用。

The above sentence in the document explains the reason for this problem.文档中的上面一句话解释了这个问题的原因。 This is due to the different coordinate system used.这是由于使用了不同的坐标系。 If your window does not have the WS_EX_TOOLWINDOW style, rcNormalPosition contains the workspace coordinates.如果您的 window 没有WS_EX_TOOLWINDOW样式, rcNormalPosition包含工作区坐标。

Workspace coordinates differ from screen coordinates in that they take the locations and sizes of application toolbars (including the taskbar) into account.工作区坐标不同于屏幕坐标,因为它们考虑了应用程序工具栏(包括任务栏)的位置和大小。 Workspace coordinate (0,0) is the upper-left corner of the workspace area, the area of the screen not being used by application toolbars.工作区坐标 (0,0) 是工作区的左上角,应用程序工具栏未使用的屏幕区域。

If the taskbar is at the top of the screen, assume the height is 40, the upper left corner of the rcNormalPosition you get is (0,0), then its screen coordinates is (0,0).如果任务栏在屏幕顶部,假设高度为40,得到的rcNormalPosition左上角为(0,0),则其屏幕坐标为(0,0)。 (The taskbar on the left is similar) (左边的任务栏类似)

在此处输入图像描述

If this behavior affects your application, you should consider calling SetWindowPlacement with the WindowPlacement that saved when your form is loaded.如果此行为影响您的应用程序,您应该考虑使用加载窗体时保存的WindowPlacement调用SetWindowPlacement

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

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