Windows 8: TopMost vs. TopMost

May 16, 2012

In Windows, a Window can be made topmost, by setting an extended style with the value WS_EX_TOPMOST (8), typically provided to CreateWindowEx. A topmost window is always on top of non-topmost windows. Among all topmost windows, the normal z-order rules apply. That is, until Windows 8.

A Metro application is built with a topmost window – that makes perfect sense. What about desktop apps? Here’s an empty desktop snapped with a metro app (the desktop itself can be seen as a kind of metro app):

image

Now let’s open Task Manager and make it topmost (Options / Always On Top). Slide it across the dividing snap bar and Task Manager is happy to be on top of the metro app:

image

Curiously, clicking on the metro app window does not make Task Manager any less topmost. Now let’s open Process Explorer and make it topmost as well (Options / Always On Top). Slide it over the metro app window, and this is the result:

image

It seems its topmost attribute is less powerful than Task Manager’s or the metro window! That’s certainly a surprise. It turns out Task Manager is “special” – I couldn’t find any other window that can stay on top of a metro app window – or on top of Task Manager for that matter.

Maybe Task Manager has a new “secret” style bit that makes it “super topmost”. Let’s open Spy++ and select Spy / Find Window from the menu:

image

Dragging the “Finder Tool” over Task Manager’s main window and clicking ok, shows this in the Styles tab:

image

The extended style is 0x108, which is WS_EX_TOPMOST | WS_EX_WINDOWEDGE – the latter being a minor appearance adjustment, irrelevant for our purposes. It looks like a regular WS_EX_TOPMOST. Let’s try the same with the Metro window.

Dragging the Finder Tool over the metro window “doesn’t stick” for some reason. Dragging Process Explorer’s equivalent tool manages to pinpoint the process to which this window belongs (WWAHost.exe, meaning that app was written in HTML/JS), but it doesn’t give out the window handle. So, how can we get the window handle of that metro app?

I decided to try calling the simple WindowFromPoint function, that I’m pretty sure Process Explorer uses as well (I was also sure Spy++ does that, though I can’t understand why it fails to see the window):

  1. POINT pt = { 40, 40 };
  2. HWND h = ::WindowFromPoint(pt);

That worked. I got a handle and typed it into Spy++ handle text box. It turns out this window is a child window, so I navigated to its parent (that window’s name was “Store”) and finally to its parent with is the top level window. These are its properties:

image

The only extended style is WS_EX_TOPMOST. How about Process Explorer’s window?

image

Pretty much the same. So they all have the same extended style bit (WS_EX_TOPMOST), but Task Manager and the Metro window behave as “super topmost” in relation to other topmost windows. Task Manager’s window seems to be the strongest topmost ever. So far, I don’t know how that is configured.

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*

11 comments

  1. BenMay 16, 2012 ב 21:56

    Great post! Have you considered asking, for example, Raymond Chen about this behavior?

    Reply
  2. Yoav HaimovichMay 20, 2012 ב 17:28

    The topmost window is also affected by the accessibility settings. If you want a window on top of Metro, you need it to declare accessibility. Here are the key points:

    1. The application must demand uiAccess (app.manifest)
    2. The application must assert “topmost” window positioning (either in Win32/SetWindowPos or WinForms/WPF’s “Topmost” property, programmatically or otherwise)
    3. Without making changes to the group policy setting, it must be installed to some trusted location [C:\Windows, C:\Program Files, C:\Program Files (x86)].
    a. Note: If you want to be able to run it out of an arbitrary location, you must disable the security setting: “User Account Control: Only elevate UIAccess applications that are installed in secure locations”.
    b. Note2: This is the same as setting HKLM\Software\Microsoft\Windows\CurrentVersion\Policies\System\ValidateAdminCodeSignatures to 0
    4. Said application cannot be ran in the debugger
    5. If it’s a .NET application
    a. The manifest must be embedded in a post-build step
    b. The application must have “delayed signing” (meaning it cannot be ran from the built-in debugger, although you can build and attach – this is what Microsoft does)
    6. The application must be signed with a trusted certificate.
    7. Said trusted certificate must be installed to the Trusted Root Certificate Authority (this is important! It must not just simply installed)

    For more info see:
    http://msdn.microsoft.com/en-us/library/ms726294

    Reply
    1. RainseyMarch 26, 2015 ב 09:42

      Dear, Yoav Haimovich

      Thank you for ur post about make app always on top,but i try to follow your guide and it still can’t bring my app to top to start screen. why ??

      Could you give the full source code how to bring app to top?

      Thank you in advance.

      Reply
      1. Pavel Yosifovich
        Pavel YosifovichMarch 26, 2015 ב 09:49

        Just set the TopMost property to true.

        Reply
        1. RainseyMarch 27, 2015 ב 11:06

          It not work when i set TopMost = true and then when i go to start screen or metro app my application form not on the top.

          Reply
          1. Pavel Yosifovich
            Pavel YosifovichMarch 27, 2015 ב 20:58

            There’s no way that I know of to do that.

  3. AlexJune 28, 2012 ב 23:46

    On Windows 8 desktop, my app is TopMost (as Inspect and TaskManager), but Inspect is always bellow TaskManager, and TaskManager is always bellow my app, even when I click on them, it is not clear why.

    Reply
  4. Chris TJuly 8, 2014 ב 15:48

    Does this behavior still exist under Win 8.1?

    Reply
    1. Pavel Yosifovich
      Pavel YosifovichJuly 8, 2014 ב 19:10

      It seems so.

      Reply
      1. RainseyMarch 26, 2015 ב 09:38

        I looking so hard how to make my simple window form application always on top to like Task manager (include start screen).

        Please help me ! thank you in advance.

        Reply
  5. levieMarch 14, 2016 ב 14:37

    Hi, thanks for sharing your insights. I find out that inspect.exe can also be topmost over the metro apps, and is at an even higher level than Task Manager. However, if I copy inspect.exe to the desktop folder, it becomes a normal topmost desktop app. Following this approach (http://stackoverflow.com/a/15025426), as was mentioned by Yoav Haimovich, I can get my app behave like inspect.exe. As to Task Manager, it is still a mystery. : )

    Reply