0
votes

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?

3
I think you're a bit early to be doing such work in WM_CREATE. - user82238
Elaborate please? Is there another later message I should be handling other than WM_CREATE? - kaykun

3 Answers

2
votes

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.

1
votes

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

0
votes

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.