Windows C++如何最大化启动应用程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8368113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Windows C++ how to startup application maximized?
提问by Phil
I am creating a windows application in C++. I am using SDL (http://www.libsdl.org/) - however this question applies to many other types of windows applications written in C++ as I have had the same problem before in MFC years ago and never solved this issue.
我正在用 C++ 创建一个 Windows 应用程序。我正在使用 SDL ( http://www.libsdl.org/) - 但是这个问题适用于用 C++ 编写的许多其他类型的 Windows 应用程序,因为我多年前在 MFC 中遇到过同样的问题并且从未解决过这个问题。
Now I want my application to start up maximized. Many other applications startup by default in the maximized state, for example when I run firefox, it automatically starts up maximized.
现在我希望我的应用程序启动时最大化。许多其他应用程序默认以最大化状态启动,例如当我运行 Firefox 时,它会自动以最大化状态启动。
Now I read that I can use ShowWindow http://msdn.microsoft.com/en-us/library/ms633548
现在我读到我可以使用 ShowWindow http://msdn.microsoft.com/en-us/library/ms633548
By calling:
通过调用:
ShowWindow(info1.window, SW_MAXIMIZE);
The window then maximized when this is called as if the user clicked on the maximized button. However, the problem is that when my application starts, firstly it starts up in a particular size (700 by 500 just for example) then when the ShowWindow gets called, it then maximizes. This is a big difference between how Firefox starts up, Firefox starts up and just appears maximized without appearing at a small size first.
当调用此方法时,窗口将最大化,就好像用户单击了最大化按钮一样。但是,问题是当我的应用程序启动时,它首先以特定大小(例如 700 x 500)启动,然后当 ShowWindow 被调用时,它会最大化。这是 Firefox 启动方式的一个很大区别,Firefox 启动后只是显示为最大化而不是首先显示为小尺寸。
So my question is, how to start up maximized without this intermediate window size (which is not maximized) before the ShowWindow is called? It is annoying for the user to see a smaller window which then maximizes suddenly upon startup.
所以我的问题是,如何在调用 ShowWindow 之前在没有这个中间窗口大小(未最大化)的情况下启动最大化?用户看到较小的窗口然后在启动时突然最大化是很烦人的。
If you can tell me how to do it in MFC or DOT NET, I can probably work out how to do it in SDL, I guess.
如果你能告诉我如何在 MFC 或 DOT NET 中做到这一点,我想我可能会弄清楚如何在 SDL 中做到这一点。
回答by kol
Call the SystemParametersInfo Windows API function with SPI_GETWORKAREA to get the width and height of the screen without the the taskbar, and set your window size when calling CreateWindow to these values. (You could also call GetSystemMetrics with SM_CXSCREEN and SM_CYSCREEN to get the width and the full height of the screen, but in this case the bottom edge of your window would be hidden by the taskbar.)
使用 SPI_GETWORKAREA 调用 SystemParametersInfo Windows API 函数以获取没有任务栏的屏幕的宽度和高度,并在调用 CreateWindow 时将窗口大小设置为这些值。(您也可以使用 SM_CXSCREEN 和 SM_CYSCREEN 调用 GetSystemMetrics 来获取屏幕的宽度和全高,但在这种情况下,窗口的底部边缘将被任务栏隐藏。)
回答by Bart
Maybe you can use the SDL_VIDEO_WINDOW_POS environment variable (see http://sdl.beuc.net/sdl.wiki/SDL_envvars) to position the window in the top left corner of the screen. Use SystemParametersInfo with SM_CXSCREEN and SM_CYSCREEN from the above post to determine the size of the window (for calling SDL_SetVideoMode). This way the window is maximized across the screen.
也许您可以使用 SDL_VIDEO_WINDOW_POS 环境变量(请参阅http://sdl.beuc.net/sdl.wiki/SDL_envvars)将窗口定位在屏幕的左上角。使用 SystemParametersInfo 和上面帖子中的 SM_CXSCREEN 和 SM_CYSCREEN 来确定窗口的大小(用于调用 SDL_SetVideoMode)。这样窗口在屏幕上最大化。
The alternative is to create the window yourself (using CreateWindow with window style WS_MAXIMIZE) and pass it to SDL through the SDL_WINDOWID environment variable.
另一种方法是自己创建窗口(使用具有窗口样式 WS_MAXIMIZE 的 CreateWindow)并通过 SDL_WINDOWID 环境变量将其传递给 SDL。