The dangers of messing with activation when handling a WM_ACTIVATE message

Date:August 9, 2005 / year-entry #219
Tags:code
Orig Link:https://blogs.msdn.microsoft.com/oldnewthing/20050809-13/?p=34653
Comments:    5
Summary:This is basically the same thing as The dangers of playing focus games when handling a WM_KILLFOCUS message, just with activation in place of focus. One developer discovered the hard way that if you mess with activation inside your WM_ACTIVATE handler, things get weird. The author noted that if he posted a message and did...

This is basically the same thing as The dangers of playing focus games when handling a WM_KILLFOCUS message, just with activation in place of focus. One developer discovered the hard way that if you mess with activation inside your WM_ACTIVATE handler, things get weird. The author noted that if he posted a message and did the work from the posted message, then everything came out okay.

A follow-up to the original message noted that passing the SWP_NOACTIVATE flag to the SetWindowPos function solves the problem. Do you understand why?


Comments (5)
  1. Chump says:

    Hmmm let me see….

    When the messagebox receives WM_ACTIVATE it sends a WM_ACTIVATE message back to it’s parent (setting wParam==WA_INACTIVE).

    The parent therefore re-enters the WM_ACTIVATE handler, and…’oh look I’ve been sent WA_INACTIVE, I’d better reposition the messagebox’….and on and on.

    Is this correct?

  2. David says:

    The poster notes that he doesn’t experience the problem in Windows ME. Do you have any idea why this is the case?

    I always imagined that things like the order of messages sent etc would be the same on 9x and NT-based versions of Windows – that the Win32 layer would expose the same behaviour, in other words, regardless of what was underneath. But if this problem doesn’t occur on ME it implies something’s quite different about what happens under the hood.

    It’s not important, but I’m curious. Any insights?

  3. Norman Diamond says:

    Various bugs in controls have had different behaviours in different versions of Windows 9x, 2000, etc., AND have had different behaviours depending on which version of Internet Explorer is installed. So it doesn’t surprise me a bit that the Win32 layers don’t expose the same behaviour in this kind of situation either.

    I often have to put code in some kinds of handlers in order to check if an unwanted event is occuring due to some other known action, and do nothing and exit immediately if so. The same handler might or might not be reentered, but either way it has to be programmed to avoid interfering with the intended actions.

  4. Tom West says:

    This is apropros to my current programming assignment. I’m trying to make a window modal to several other (but not all) application windows. I have window A that is always supposed to be above window B (although there may be other windows above both of them). if the user clicks on B to activate it, I want A to come to the foreground with B right behind it.

    My understanding is that when the user clicks on B, I will receive a WM_SETWINDOWPOSCHANGING message, where I could change the WINDOWPOS structure to place B behind A and have it not activate.

    My question is could I also call SetForegroundWindow on A while handling B’s WM_SETWINDOWPOSCHANGING message, or will it, as I suspect, occasionally blow up? Is there a message I could post to A to make it activate later?

  5. Nekto2 says:

    Thanks for the article. I have some strange crashes on WinCE after messing with Activate and focus messages.

    Still I can’t see why my background windows (all of them are modal dialog 8-) could just activate/deactivate even if I will not show them. I think this article will help me find out why it is.

    But this lead me to the questions:

    1. Why there is no integrated messages debugger? The one which will show both current messages in progress (and handler are reentered) and message queue of unprocessed messages and history with timeline of messages (with starting and ending processing time and overlaps if there are any).

    2. Why there is no list of "this action sould be done in that handler" for common actions. Example is – "when should I update data in controls of a dialog, which is not recreated every time it is used?".

    –answer?–

    MSDN: SWP_NOACTIVATE Does not activate the window. If this flag is not set, the window is activated and moved to the top of either the topmost or the nontopmost group, depending on the setting of the pWndInsertAfter parameter.

    So there is reenterance.

    But the questions is: If I set SWP_HIDEWINDOW flag without SWP_NOACTIVATE will that set window to Active before it hides it? :)

Comments are closed.


*DISCLAIMER: I DO NOT OWN THIS CONTENT. If you are the owner and would like it removed, please contact me. The content herein is an archived reproduction of entries from Raymond Chen's "Old New Thing" Blog (most recent link is here). It may have slight formatting modifications for consistency and to improve readability.

WHY DID I DUPLICATE THIS CONTENT HERE? Let me first say this site has never had anything to sell and has never shown ads of any kind. I have nothing monetarily to gain by duplicating content here. Because I had made my own local copy of this content throughout the years, for ease of using tools like grep, I decided to put it online after I discovered some of the original content previously and publicly available, had disappeared approximately early to mid 2019. At the same time, I present the content in an easily accessible theme-agnostic way.

The information provided by Raymond's blog is, for all practical purposes, more authoritative on Windows Development than Microsoft's own MSDN documentation and should be considered supplemental reading to that documentation. The wealth of missing details provided by this blog that Microsoft could not or did not document about Windows over the years is vital enough, many would agree an online "backup" of these details is a necessary endeavor. Specifics include:

<-- Back to Old New Thing Archive Index