tags:

views:

45

answers:

3

The way I see it, one use of a Window Procedure's WM_CREATE message is to relieve the caller of the burden of executing static code at window initialization. My window is to execute some code in the WM_CREATE message, including the ShowWindow function. I also want the ShowWindow to behave properly according to the nCmdShow parameter in WinMain. So here is pseudo-code to show how I have things set up:

int g_nCmdShow;

WinMain(..., int nCmdShow)
{
  g_nCmdShow = nCmdShow;
  ...
  CreateWindow(..., WM_OVERLAPPEDWINDOW, ...)
  ...
}

WndProc()
{
  ...
  WM_CREATE:
    ...
    ShowWindow(hWnd, g_nCmdShow);
    ...
  ...
}

So I set up the program to run Minimized (using Windows XP I created a shortcut to the .exe, and set up the properties of it accordingly), and it displays on the taskbar minimized but it does not restore when I click on it. Likewise, if I run it Maximized, it does not behave correctly when I click the maximize button (to un-maximize it).

What is the correct way to use nCmdShow-compatible ShowWindow within the WM_CREATE message?

+1  A: 

The problem is that the window's restore bounds get affected by this. They become the size of the window after WM_CREATE returns. You would have to modify your code to re-establish those restore bounds:

hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
    CW_USEDEFAULT, 0, 300, 200, NULL, NULL, hInstance, NULL);
WINDOWPLACEMENT wp;
GetWindowPlacement(hWnd, &wp);     // <= Note wp.rcNormalPosition after this call!
RECT rc = {100, 100, 400, 300};
wp.rcNormalPosition = rc;
SetWindowPlacement(hWnd, &wp);

You're not ahead by doing it this way.

Hans Passant
As stated, I want the all the code to exist in the WM_CREATE message. I could easily just call ShowWindow in WinMain, but I don't want that. After reproducing your code in WM_CREATE, it did not fix my problem.
kaykun
As stated, this cannot work. Waiting for, say, WM_PAINT to repair the restore bounds is a possible hack. Not explaining why this is a requirement prevents me (us) from coming up with a better approach.
Hans Passant
A: 

Can you handle WM_WINDOWPOSCHANGED and override the window size the first time the window is restored? Use GetWindowPlacement to find out if the window got restored.

Ben Voigt
+2  A: 

If you absolutely have to keep it in the WndProc, try

case WM_CREATE:
    PostMessage(hwnd,WM_APP,0,0);
    break;
case WM_APP:
    ShowWindow(hwnd,SW_SHOW);
    break;

But if this is so important, why not just have a helper function that creates the window and calls ShowWindow? MyWindowType_Create(...) etc

Anders