Search code examples
.netwindowswinformsmessage-queueimessagefilter

WM_DESTROY, WM_CLOSE bypassing IMessageFilter


Below is my message filter:

bool MyFilter::PreFilterMessage(Message %m){
    switch(m.Msg){
    case WM_CLOSE:
    case WM_DESTROY:
    case WM_NCDESTROY:
    case WM_QUIT:
        Debug::WriteLine(L"Gone!");
        break;
    case WM_MOUSEMOVE:
        Debug::WriteLine(L"A mouse! Catch! Catch!!! CATCH!!");
        break;
    }
    return false;
}

I verified that I am filtering most messages without a problem. However, I am not receiving any messages dispatched after the window's close button is clicked (WM_CLOSE, WM_DESTROY, WM_NCDESTROY and WM_QUIT). Why is this?


Solution

  • IMessageFilter.PreFilterMessage() is only called for messages in the message queue. Messages like WM_CLOSE are sent directly to WndProc() with SendMessage(), they bypass the queue. You also won't get messages like WM_ACTIVATE, WM_GETTEXT, etc. Input events, that's about it.