2

I am working on window creation with Win32 API and I'm having a problem with this part:

GetMessage(&message, NULL, 0, 0);

My problem is that when I try to change the second parameter (hwnd) which is going to receive the messages to the window I previously made, it doesn't work; for example, when I try to close the window, it only hides and doesn't close.

Here is the full code:

#include <windows.h>

LRESULT CALLBACK WinProc(HWND window, UINT message, WPARAM wParam, LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow)
{
  WNDCLASS window;
  window.cbClsExtra = NULL;
  window.cbWndExtra = NULL;
  window.hbrBackground = (HBRUSH)COLOR_BACKGROUND;
  window.hCursor = LoadCursor(hInst, IDC_ARROW);
  window.hIcon = NULL;
  window.hInstance = hInst;
  window.lpfnWndProc = WinProc;
  window.lpszClassName = "WINDOW";
  window.lpszMenuName = NULL;
  window.style = CS_HREDRAW | CS_VREDRAW;

  RegisterClass(&window);

  HWND hwnd = CreateWindow("WINDOW", "Win32 Window Application", WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, 200, 200, NULL, NULL, hInst, NULL);

  ShowWindow(hwnd, SW_SHOW);
  UpdateWindow(hwnd);

  MSG message;

  while (GetMessage(&message, NULL, 0, 0))
  {
    TranslateMessage(&message);
    DispatchMessage(&message);
  }

  return 1;
}

LRESULT CALLBACK WinProc(HWND window, UINT message, WPARAM wParam, LPARAM lParam)
{
  switch (message)
  {
  case WM_CLOSE:
    {
      PostQuitMessage(0);
      break;
    }

  default:
    break;
  }

  return DefWindowProc(window, message, wParam, lParam);
}
cf-
  • 8,598
  • 9
  • 36
  • 58
user3407319
  • 227
  • 2
  • 10
  • 1
    Just a couple of things I noticed - `while (GetMessage(&message, NULL, 0, 0))` is typically `while (GetMessage(&message, NULL, 0, 0) > 0)`, so you can filter out messages properly. Also, instead of `return 1`, you'd generally want `return message.wParam` - a proper application shutdown usually returns 0, as your `PostQuitMessage(0)` call does, and then returning message.wParam will properly convey this, while `PostQuitMessage(1)` will return 1, conveying an abnormal shutdown. Your code does appear to work for me, though. – cf- Apr 26 '14 at 11:43
  • 2
    Intentionally not passing NULL is just a bug. – Hans Passant Apr 26 '14 at 11:46
  • @HansPassant +1. in almost every case I've seen that does this, that is in-fact the case. – WhozCraig Apr 26 '14 at 11:48

1 Answers1

6

"... when I try to change the second parameter (hwnd) which is going to receive the messages to the window I previously made, it doesn't work."

Thread messages are not sent to a window; they're posted to the thread message queue with a NULL window handle, and will NOT be picked up with a GetMessage() loop tailored to a specific window handle.

Ex: PostQuitMessage() posts a thread message; not a window message. You need the NULL. From the GetMessage() docs:

If hWnd is NULL, GetMessage retrieves messages for any window that belongs to the current thread, and any messages on the current thread's message queue whose hwnd value is NULL (see the MSG structure). Therefore if hWnd is NULL, both window messages and thread messages are processed.

WhozCraig
  • 65,258
  • 11
  • 75
  • 141