Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Win32 and catching WM_SIZE message outside WindowProcedure

Tags:

c++

winapi

I have a function which processes messages and there I catch certain messages outside WindowProcedure to trigger wanted behavior.

The problem is that while it seems like other messages are working as needed, I can't catch the WM_SIZE message for some reason. WM_SIZE is seen in the WindowProcedure, but I can't find a reason why it is not seen by my function. Is the problem on my method of reading the current HWND?

The function currently is:

// OS MESSAGES
int OSMessages(void)
{
  MSG msg;
  HWND actwnd = GetActiveWindow();

  if ( PeekMessage(&msg, actwnd, 0, 0, PM_REMOVE) )
  {

    if (msg.message == WM_QUIT)
    {
      printf("QUIT");
      return -1;
    }
    else if (msg.message == WM_SIZE)
    {
      printf("RESIZE");
      return 1;
    }

    TranslateMessage(&msg);
    DispatchMessage(&msg);
  }

  return 0;
}
like image 747
Mikko-Pentti Einari Eronen Avatar asked Dec 14 '22 03:12

Mikko-Pentti Einari Eronen


1 Answers

A message loop can only ever see messages that are posted to the calling thread's message queue. WM_SIZE, however, is not a posted message, it is a sent message. A sent message is delivered directly to a window's message procedure without going through the message queue at all (although a message loop has some influence on when a sent message is delivered to the message procedure).

That is why your message loop is not seeing WM_SIZE. If you need it, you will have to subclass the target window itself using SetWindowsLong/Ptr() or SetWindowsSubclass() to hook in your own window procedure.

See MSDN for more details:

Messages and Message Queues

Subclassing Controls

like image 183
Remy Lebeau Avatar answered Feb 23 '23 21:02

Remy Lebeau