What kind of messages can a message-only window receive?


There's this thing called a message-only window, which is a very misleading name because receiving messages is the thing a window spends all its time doing anyway. It's like calling something a water-only swimming pool or a heat-only oven.

It's also a very misleading name because it doesn't receive all messages. "Wait, the name of this window says that the only thing it can do is receive messages, and now you're saying it can't do even that!"

The point of a message-only window is that it receives only messages sent or posted specifically to it. You use it to set up a private channel between the sender and the window. After creating a message-only window, you can put messages in the window's queue by calling Post­Message and passing that window handle, or you can send a non-queued message by calling Send­Message and passing that window handle.

What makes a message-only window interesting is that it doesn't particpate in broadcast messages.

Many window messages are sent to all top-level windows. WM_QUERY­END­SESSION, WM_SETTING­CHANGE, WM_DDE_INITIATE. and anything sent with HWND_BROADCAST. These messages don't reach message-only windows.

Internally, message-only windows are treated as child windows of a system-managed common parent window called HWND_MESSAGE. This system-managed common parent window is permanently invisible, which results in message-only windows being permanently invisible. And that's also how message-only windows are invisible to enumeration and broadcasts: Enumeration and broadcasting is done to top-level windows, but message-only windows are internally treated as child windows of HWND_MESSAGE and therefore are not considered top-level.

Comments (22)
  1. JAS says:

    Does a message-only window actually allocate fewer resources than a normal hidden child window?

  2. kantos says:

    What I’m reading between the lines is that you should never do anything in such a window that would require overly graceful exit behavior. I’m assuming this because you don’t control the parent window, and it might get destroyed prior to your main window being able to send a window message telling your hidden window to gracefully die.

    1. The system-managed parent window is never destroyed. (Because if it were destroyed, then the problem you describe would occur.)

      1. kantos says:

        I was assuming on shutdown. But in all honesty.. if the house is coming down, trying to save the furniture is dubious at best.

        1. Rick C says:

          Indeed, as the Klingons say, only a fool tries to save the furniture in a burning house.

          1. smf says:

            It depends on how much the furniture is worth.

  3. Ben Voigt says:

    So can you intentionally enumerate message-only windows by asking for the collection of child windows of HWND_MESSAGE?

    1. skSdnW says:

      FindWindowEx documents that as a valid parameter while EnumChildWindows does not. I assume it still works even if not documented.

  4. Piotr says:

    Why would I need a window that doesn’t receive specific messages? Can’t the window just ignore them?

    1. skSdnW says:

      There is some overhead with the broadcast messages, wndprocs might have to be paged in etc. and hung windows can make things very slow.

  5. Peter Doubleday says:

    Reading between the lines, a message-only window appears to be an implementation choice for parent windows that want to delegate the processing of (non-broadcast) WM_ type messages to a child object that just happens to be (in a limited sense) a “window.” In other words, it’s an invisible abstract artefact that just happens to feature a filtered message pump, and nothing much else.

    I know that Windows, since pre-NT days (as far as I am aware), has been built around the twin notions of “everything is an HWND” and “Feel the power of the Message Pump, Young Jedi,” but isn’t this just taking direct message-passing a little too far?

  6. DWalker07 says:

    These windows don’t even get the message that says “Windows is about to shut down”?

    1. IanBoyd says:

      They would – when *you* Post a message to the window after *you* received the broadcast that Windows is about to shut down.

  7. @JAS, @Peter Doubleday, @DWalker07:

    I’d bet message-only windows were implemented as a form of IPC. Before NT, Windows did not have pipes, so you needed another way to communicate between two processes. The main window can be handy, except when a process can be windowless, or when it has one window for each document or session (which one should you send messages to?).

    In the 80s, PARC ideas, and in particular SmallTalk’s object model, influenced many pioneer Apple and Microsoft engineers (some of came from Xerox PARC, which is where SmallTalk was developed, but I digress). In the SmallTalk model, a set of objects interacted purely by message posting. No method calls, no public properties, no getters or setters. Just messages. I bet whoever devised message-only windows had SmallTalk in mind.

    Maybe message-only windows weren’t much lighter than regular ones (Raymond tells us their main difference was their parent). But I guess most tasks (processes) had a limited number of them (if any), so it doesn’t matter too much how many resources they allocate.

    1. Matteo Italia says:

      This is how I always understood message-only windows, for those times when you just want a thread-safe, even cross process event queue part of windows, but you don’t give a damn about them being graphical objects.

    2. laonianren says:

      16-bit Windows didn’t have message-only windows. They seem to have been introduced in Windows 2000.

      1. Joshua says:

        Check your work.

  8. cheong00 says:

    [There’s this thing called a message-only window, which is a very misleading name because receiving messages is the thing a window spends all its time doing anyway. ]

    I don’t find it misleading at all. For example “passenger-only ferry” only carries passengers and not vehicle, and “food-only elevator” carries food but not people. That’s just another way to address things that “does this, and only does this and nothing else that it’s type normally could also do”.

    1. McBucket says:

      “I don’t find it misleading at all. For example “passenger-only ferry” only carries passengers and not vehicle, and “food-only elevator” carries food but not people. That’s just another way to address things that “does this, and only does this and nothing else that its type normally could also do.”

      Your analogy is flawed. In your analogy, there is an X-only thing that deals in X’s but not Y’s. But in this case, where X = message and thing = window, what is the Y? There is no such entity, because all windows deal in messages.

      What really distinguishes message-only windows from other types of windows is that message-only windows don’t receive broadcast messages. So a more proper term would be ‘non-broadcast-message-only window’. That’s pretty unwieldy; if there were a term that encapsulated ‘non-broadcast-message’, say ‘xyzzy’, then you could call these types of windows “xyzzy-only windows’. But the term ‘message-only window’ doesn’t mean what it implies, and hence it’s misleading.

      1. Steve says:

        Isn’t the argument that one of the typical behaviours of a window is graphical display (hence the name), and that with a message-only window, it is this behaviour of the general type that is being excluded (hence why HWND_MESSAGE is “permanently invisible”)?

        It’s rather like an old VCR, which had a television tuner for recording and playback purposes, but no television display of its own.

      2. cheong00 says:

        [Your analogy is flawed. In your analogy, there is an X-only thing that deals in X’s but not Y’s. But in this case, where X = message and thing = window, what is the Y? There is no such entity, because all windows deal in messages. ]

        Y is the implicit “other things that windows can do”. Which part do you not understand?

  9. smf says:

    IMO the most important thing about a message only window is that it’s invisible.

    The main use is if you need someone to send you messages, but you have no UI.

Comments are closed.

Skip to main content