SetWindowPos / MoveWindow持续存在的问题

use*_*240 1 .net c# windows winapi window-position

我正在使用SetWindowPosMoveWindow调整窗口大小和居中。它可以正常工作,但是在Windows Media Player或“控制面板”等几个窗口上,当您关闭窗口并再次打开它时,新的调整大小/移动不会反映出来。手动调整大小时,更改将在下次打开窗口时反映出来。即使我打电话UpdateWindow,更改也不会反映出来。我需要发送窗口以便保存更改吗?有RedrawWindow帮助吗?谢谢?

Cod*_*ray 5

您应该改用GetWindowPlacementSetWindowPlacement函数来检索和更改窗口的还原,最小化和最大化位置。这样可以确保应用程序正确保存了窗口大小,以便下次启动时可以将其还原。

由于使用的是C#,因此需要从Windows API P /调用以下功能:

const int SW_HIDE = 0;
const int SW_SHOWNORMAL = 1;
const int SW_SHOWMINIMIZED = 2;
const int SW_SHOWMAXIMIZED = 3;

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern bool GetWindowPlacement(IntPtr hWnd, out WINDOWPLACEMENT lpwndpl);

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern bool SetWindowPlacement(IntPtr hWnd, [In] ref WINDOWPLACEMENT lpwndpl);

[StructLayout(LayoutKind.Sequential)]
struct RECT
{
    public int left;
    public int top;
    public int right;
    public int bottom;
}

[StructLayout(LayoutKind.Sequential)]
struct WINDOWPLACEMENT
{
    public int length;
    public int flags;
    public int showCmd;
    public Point ptMinPosition;
    public Point ptMaxPosition;
    public RECT rcNormalPosition;
}
Run Code Online (Sandbox Code Playgroud)

  • 好的,我找到了解决方案,谢谢你。我按照您的建议去了 Spy++ 并开始监视我手动移动的窗口,我发现 WM_ENTERSIZEMOVE 和 WM_EXITSIZEMOVE 总是被调用。所以我简单地使用 WM_ENTER... 和 WM_EXIT 执行了两次 SendMesage,并且媒体播放器和控制面板的大小得到了保留。真的感谢你的帮助科迪。 (2认同)