简体   繁体   English

如何在 Win32 C++ 程序中以编程方式最大化顶级 window?

[英]How do I programmatically maximize a top-level window in a Win32 C++ program?

I'm trying to programmatically maximize my top-level window in my Win32 C++ program.我试图在我的 Win32 C++ 程序中以编程方式最大化我的顶级 window 。 I'm using code similar to the following in the code that handles the WM_CREATE message:我在处理WM_CREATE消息的代码中使用了类似于以下的代码:

        WINDOWPLACEMENT windowPlacement = {};
        windowPlacement.length = sizeof(WINDOWPLACEMENT);
        windowPlacement.rcNormalPosition = newWindowRect;
        windowPlacement.showCmd = SW_NORMAL;

        if (maximized == TRUE)
        {
            windowPlacement.showCmd = SW_SHOWMAXIMIZED;
        }
        SetWindowPlacement(hwnd, &windowPlacement);

When this code executes, if maximized is TRUE , the window is set to the size of a maximized window, but the maximize/restore button is still a maximize button, not a restore button.当此代码执行时,如果maximizedTRUE ,则 window 设置为最大化 window 的大小,但最大化/恢复按钮仍然是最大化按钮,而不是恢复按钮。

窗口截图

I've tried using the ShowWindow() function with the same results.我尝试使用ShowWindow() function 得到相同的结果。

It turns out the WM_CREATE message handler was the wrong place to do this.事实证明WM_CREATE消息处理程序是执行此操作的错误位置。 After moving the code out of the window procedure and into the wWinMain() function after the call to CreateWindow() but before the call to ShowWindow() , it works as expected.在调用CreateWindow()之后但在调用ShowWindow()之前将代码移出 window 过程并进入wWinMain() function 后,它按预期工作。

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

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