SetWindowPos/MoveWindow 仍然存在问题

发布于 2024-10-15 23:50:17 字数 263 浏览 1 评论 0原文

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

I am using SetWindowPos and MoveWindow to resize and center windows. It works fine, but on several windows like Windows Media Player or Control Panel, when you close the window and open it again, the new resizing/moving is not reflected. When I resize manually, the changes are reflected the next time I open the window. Even if I call UpdateWindow, the changes don't reflect. Is there something I need to send the window so the changes get saved? Would RedrawWindow help? Thanks?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

翻身的咸鱼 2024-10-22 23:50:17

您应该使用 GetWindowPlacementSetWindowPlacement 函数用于检索和更改窗口的恢复、最小化和最大化位置。这可确保应用程序正确保存窗口大小,以便可以在下次启动时恢复它们。

由于您使用的是 C#,因此您需要从 Windows API P/Invoke 这些函数:

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;
}

You should be using the GetWindowPlacement and SetWindowPlacement functions instead to retrieve and alter the restored, minimized, and maximized positions of a window. This ensures that the window sizes are properly saved by the application so that they can be restored on the next launch.

Since you're using C#, you'll need to P/Invoke these functions from the Windows API:

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文