Creating and showing a maximized window

This is a discussion on Creating and showing a maximized window within the Windows Programming forums, part of the Platform Specific Boards category; I need a maximized main window in my program. This seems to work but is it the correct or best ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    322

    Creating and showing a maximized window

    I need a maximized main window in my program.

    This seems to work but is it the correct or best way to do it?

    The msdn library says that nCmdShow, passed into WinMain, should be used
    when initially showing the window. Should that first call to ShowWindow
    be left in WinMain, or moved to WndProc?

    Code:
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    {	
    	HWND hWnd;
    	MSG Msg;
    	WNDCLASS wc;
    	wc.style = 0;
    	wc.lpfnWndProc = WndProc;
    	wc.cbClsExtra  = 0;
    	wc.cbWndExtra	  = 0;
    	wc.hInstance = hInstance;
    	wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    	wc.hCursor = LoadCursor(NULL, IDC_CROSS);
    	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    	wc.lpszMenuName  = NULL;
    	wc.lpszClassName = "Main";
    
    	if(!RegisterClass(&wc))
    		return 0;
    
    	hWnd = CreateWindow("Main", "mandelscope", WS_OVERLAPPEDWINDOW,
    		CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
    
    	if(hWnd == NULL)
    		return 0;
    
    	ShowWindow(hWnd, nCmdShow);
    	hDc = GetDC(hWnd);
    
    	initialize(hWnd);
    	
    	while(GetMessage(&Msg, NULL, 0, 0) > 0)
    	{	TranslateMessage(&Msg);
    		DispatchMessage(&Msg);
    	}
    	return 0;
    }
    
    LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {	
    	switch(msg)
    	{	case WM_CREATE:
    			ShowWindow(hWnd, SW_MAXIMIZE);
    			break;
    
    	...
    	...
    	...
    	}
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    The initial show window should be done in wind main, then left to the user to resize and move the window around later (by handling WM_SIZE, etc. in your message loop).

    BTW... many end users do not want maximized windows opening on their screens... It hides their other windows and wastes their time resizing it. Most programs do not need the entire screen so it's kind of selfish to open them in the maximized state. A far better plan is to include a function to store the window size and position in the registry when exiting and then restore that size and position when starting up.
    Last edited by CommonTater; 09-10-2011 at 09:21 PM.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    322
    The program is unusable unless fullsize, or nearly so. I always need it full size immediately
    but good point, others may not.

    Maybe I'll just use the default size window for a sign-on message.

    What about the hWnd handle?
    Is it ok to use the one returned in WinMain by CreateWindow, while inside WinMain,
    and also use the hWnd passed into WndProc for passing on to other functions?

  4. #4
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,855
    Just use ShowWindow( hWnd, SW_SHOWMAXIMIZED) in the winmain

    IME this modifies the windows style to include WS_VISIBLE and then sends a WM_SIZE series of msgs to change the window to be maximised.

    Another way is to set the size in the CreateWindow() call using the screen size from a GetSystemMetrics() call

    Quote Originally Posted by megafiddle View Post
    What about the hWnd handle?
    Is it ok to use the one returned in WinMain by CreateWindow, while inside WinMain,
    and also use the hWnd passed into WndProc for passing on to other functions?
    Yes
    Last edited by novacain; 09-10-2011 at 10:16 PM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    322
    I am now using SW_MAXIMIZE.
    SW_SHOWMAXIMIZED also makes the window active?

    Is it necessary to make sure your window is on top when starting the program?
    Is it possible that your window might not be on top at startup?

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by megafiddle View Post
    The program is unusable unless fullsize, or nearly so. I always need it full size immediately
    but good point, others may not.

    Maybe I'll just use the default size window for a sign-on message.

    What about the hWnd handle?
    Is it ok to use the one returned in WinMain by CreateWindow, while inside WinMain,
    and also use the hWnd passed into WndProc for passing on to other functions?
    About the only variables I make global in my code are A) Window handles, B) Instance handle, C) Settings struct...

    It's just so convenient to have those handles stored rather than having to rediscover them every function.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by megafiddle View Post
    The program is unusable unless fullsize, or nearly so. I always need it full size immediately
    but good point, others may not.
    Your program needs a 1920 x 1080 window? You may want to give that some more thought...
    [/QUOTE]

  8. #8
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,855
    Quote Originally Posted by megafiddle View Post
    SW_SHOWMAXIMIZED also makes the window active?
    Been a while since I wrote in pure WIN32. try it and see, if it does not work try setting the size in your CreateWindow().

    Quote Originally Posted by megafiddle View Post
    Is it necessary to make sure your window is on top when starting the program?
    Is it possible that your window might not be on top at startup?
    Yes it is possible that some other currently running window has the TOPMOST style, probably because it requires it (task manager, error msgs etc).

    So you should not try to over-ride that, especially if you are running full screen.

    Make you app work with the user, not against them (or the user will simple not use your app).
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    322
    Quote Originally Posted by CommonTater View Post
    Your program needs a 1920 x 1080 window? You may want to give that some more thought...
    I have 1000 x 750 pixel image that it the focus of the program. Then I have a 25 x 1000 pixel spectrum,
    and 33 color nodes, 25 x 25 pixels each, and 100 palette colors 25 x25 pixels each, and I plan on adding some other things.

    The problem is that what I am trying to do requires immediate feedback. Opening up another window or dialog box
    to select something would make an already tedious process even more tedious. So I want to have
    everything available on the screen at once. All of the operations will be by single mouse clicks or single keyboard keys.

    I would have liked an even bigger image, 1200 x 900 pixels, but that would have left room for little else. I will have to
    generate that seperately after the smaller image is finalized.

    Quote Originally Posted by novacain View Post
    Been a while since I wrote in pure WIN32. try it and see, if it does not work try setting the size in your CreateWindow().
    Ok.

    Maximizing the window is working. I was just wondering about the difference between SW_SHOWMAXIMIZED and SW_MAXIMIZE.
    Last edited by megafiddle; 09-11-2011 at 02:07 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. show window minimized maximized without focus taken
    By eXistenZ in forum Windows Programming
    Replies: 16
    Last Post: 12-15-2008, 11:38 AM
  2. Window not actually showing.
    By Shamino in forum Windows Programming
    Replies: 3
    Last Post: 02-22-2008, 09:55 AM
  3. Window not showing
    By Rare177 in forum Windows Programming
    Replies: 7
    Last Post: 06-25-2004, 05:38 AM
  4. starting with a maximized window
    By bluehead in forum Windows Programming
    Replies: 4
    Last Post: 05-27-2002, 08:08 PM
  5. How to open window maximized
    By Unregistered in forum Windows Programming
    Replies: 5
    Last Post: 02-04-2002, 03:29 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21