4

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.

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.

Now I read that I can use 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.

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.


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.

Phil
  • 46,436
  • 33
  • 110
  • 175
  • i don't think C++ handles windows at all,so what framework you are using ??? – Qchmqs Dec 03 '11 at 13:57
  • How do you initially create your window? – Mat Dec 03 '11 at 14:04
  • I am writing a windows application in the C++ language using SDL, however this problem is common for any windows application I have written in the past, I have never solved this particular problem. I programmed Windows MFC applications way way back in the past. You can see one of my apps in 2001 http://www.codeproject.com/KB/edit/hexedit.aspx – Phil Dec 03 '11 at 14:05
  • I updated it to show the link to SDL. – Phil Dec 03 '11 at 14:05
  • Note the WinMain() entry point declaration, class libraries honor the value of the passed nCmdShow argument when they call ShowWindow(). You'll have to doctor the SDL version of it. http://msdn.microsoft.com/en-us/library/windows/desktop/ms633559%28v=vs.85%29.aspx – Hans Passant Dec 03 '11 at 15:55
  • Don't show your window twice (once with `SW_NORMAL` and then a second time with `SW_MAXIMIZE`). Just show it once with `SW_MAXIMIZE`. – Raymond Chen Dec 03 '11 at 17:55
  • Don't create the window with the WS_VISIBLE flag. Create it invisible then call ShowWindow. – selbie Dec 03 '11 at 19:55

2 Answers2

6

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.)

kol
  • 27,881
  • 12
  • 83
  • 120
  • However this won't actually be a maximized window will it? This kind of maximize you suggest will just adjust the size of a window to fit the screen. This is not the same as clicking on the maximize button. A really maximized window cannot be dragged by the title bar. Do you agree? – Phil Dec 03 '11 at 14:15
  • Yes, but if you set the window size to the workarea size in CreateWindow, and then call ShowWindow with SW_MAXIMIZE, then the window will appear as maximized first, and then will become maximized. It won't appear at a small size first, which was your problem - if I understand sou correctly. – kol Dec 03 '11 at 14:21
  • Ahh I see. so the user won't notice that it went maximized because it started at a larger size already. But is the window positioned correctly in the top left? – Phil Dec 03 '11 at 14:25
  • Yes, if you set both the x and y parameters of CreateWindow to 0. – kol Dec 03 '11 at 14:26
3

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.

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.

Bart
  • 31
  • 2