How do I create a topmost window that is never covered by other topmost windows?

We already know that you can't create a window that is always on top, even in the presence of other windows marked always-on-top. An application of the What if two programs did this? rule demonstrates that it's not possible, because whatever trick you use to be on-top-of-always-on-top, another program can use the same trick, and now you have two on-top-of-always-on-top windows, and what happens?

A customer who failed to understand this principle asked for a way to establish their window as "super-awesome topmost". They even discovered the answer to the "and what happens?" rhetorical question posed above.

We have overridden the OnLostFocus and OnPaint methods to re-assert the TopLevel and TopMost window properties, as well as calling BringToFront and Activate. The result is that our application and other applications end up fighting back and forth because both applications are applying similar logic. We tried installing a global hook and swallowing paint and focus events for all applications aside from our own (thereby preventing the other applications from having the opportunity to take TopMost ahead of us), but we found that this causes the other applications to crash. We're thinking of setting a timer and re-asserting TopMost when the timer fires. Is there a better way?

This is like saying, "Sometimes I'm in a hurry, and I want to make sure I am the next person to get served at the deli counter. To do this, I find whoever has the lowest number, knock them unconscious, and steal their ticket. But sometimes somebody else comes in who's also in a hurry. That person knocks me unconscious and steals my ticket. My plan is to set my watch alarm to wake me up periodically, and each time it wakes me up, I find the person with the lowest number, knock them unconscious, and steal their ticket. Is there a better way?"

The better way is to stop knocking each other unconscious and stealing each other's tickets.

The customer (via the customer liaison) provided context for their question.

This is not a general-purpose application. This application will be run on dedicated machines which operate giant monitors in retail stores. There are already other applications running on the computer which rotate through advertisements and other display information.

The customer is writing another application which will also run on the machine. Most of the time, the application does nothing, but every so often, their application needs to come to the front and display its message, regardless of whatever the other applications are displaying. (For example, there might be a limited-time in-store promotion that needs to appear on top of the regular advertisements.)

Unfortunately, all of these different programs were written by different vendors, and there is no coordination among them for who gets control of the screen. We were hoping that there was some way we could mark our window as "super topmost" so that when it came into conflict with another application running on the machine, it would win and the other application would lose.

I'm thinking of recommending that the vendors all come up with some way of coordinating access to the screen so they can negotiate among themselves and not get into focus fights. (Easier said than done, since all the different applications running on the machine come from different vendors...)

Since there is no coordination among the various applications, you're basically stuck playing a game of walls and ladders, hoping that your ladder is taller than everybody else's wall. The customer has pretty much found the tallest ladder which the window manager provides. There is no "super topmost" flag.

Sure, you can try moving to another level of the system, like say creating a custom desktop, but all that does is give you a taller ladder. And then one of the other applications is going to say, "I need to display a store-wide page (manager to the deli please, manager to the deli), overriding all other messages, even if it's a limited-time in-store promotion." And they'll try something nastier, like enumerating all the windows in the system and calling ShowWindow(SW_HIDE).

And then another application will say, "I need to display an important store-wide security announcement (Will the owner of a white Honda Civic, license plate 037-MSN, please return to your vehicle), overriding all other messages, even if it's a store-wide page." And it'll try something nastier, like setting their program as the screen saver, disabling the mouse and keyboard devices, and then invoking the screen saver on the secure desktop.

And then another application will say, "I need to display a critical store-wide announcement (Fire in the automotive department. Everybody evacuate the building immediately), overriding all other messages, even if it's an important store-wide security announcement." And it'll try something nastier, like enumerating all the processes on the system, attaching to each one with debug privilege, and suspending all the threads.

Stop the madness. The only sane way out is to have the programs coöperate to determine who is in control of the screen at any particular time.

In response to my hypothetical game of walls and ladders, one of my colleagues wrote, "Note to self: Do not get into a walls-and-ladders contest with Raymond."

Comments (74)
  1. Sunil Joshi says:

    Are you sure it's wise to leave such tempting suggestions in this post? Some developer of user hostile programs might stumble across it and start implementing them…

    I certainly do not look forward to the day programs try to install drivers to patch the window manager to finally achieve victory.

  2. Jake says:

    You forgot overwriting the MBR to point at your micro kernel and forcing a reboot!

  3. SimonRev says:

    And none of you will defeat my hypervisor that will disable Windows entirely at my command :)

  4. laonianren says:

    For the really, really, really important messages they should have another computer controlling a KVM so that it can seize control of the screen's input.

    P.S. What's Walls and Ladders?

  5. Ivo says:

    I just had an idea. It may be crazy enough to work. Looks like it is not your generic "I want to be always on top of all other windows". It is a more specific scenario where application A wants to always be on top of a specific application B. Why not make window A owned by window B? Doing that across processes is asking for trouble, but it's possible (AFAIK).

  6. Brian G. says:

    Yesterday I mentioned that I read this blog though much of the material in the technical articles is above my head.

    It is for gems like this that I read each and every day.  Thank you, Raymond.  This one's a beaut.  (I just about did a coffee-spew at the customers knocking one another unconscious….and it only got better from there.)

  7. Jake says:

    @Ivo: I'm guessing that a window can't be owned by another application.

  8. Florian W. says:

    What about a Graphic-Device-Driver?

  9. Christopher Allen-Poole says:

    You never cease to entertain

  10. David Walker says:

    Raymond is the tallest, most super-awesome topmost blogger.  Don't try to be taller than he is.  He will win.  And that's A Good Thing.  

  11. Ben says:

    Wow, really interesting to have an example of a legitimate reason for wanting to do this. I always assumed that these examples were cr*pware and adware vendors! But this actually sounds like a reasonable request (albeit one which cannot be fulfilled for the reasons you gave).

  12. Wizou says:

    Why not create an electronic device with 2 VGA inputs and 1 VGA output, that would switch from one to the other using a command received by COM1 or LPT ;)

  13. Adam Rosenfield says:

    Just run each application that wants to be super-awesome-topmost in a VM, and have your VMM decide who wins.  Then, if another application comes along that wants to be super-duper-awesome-topmost, you start up another VM with that and hide or suspend the other VMs.  It's like giving each customer its own private deli where it doesn't have to fight to be first in line, but you decide which deli actually is visible to the real world.

  14. Joe Dietz says:

    I'm going to own you all, I would just write a SMM bios patch that does a screen overlay (this is how battery functions etc. used to be implemented on laptops before ACPI came along for instance).  SMM will even take precedence over your hyper-visor.  Or perhaps I'll just code up something to run on the AMT processor you've been ignoring on your shiny new V-Pro branded machine to just take direct control over the video memory.

  15. Danny Moules says:

    I always find it funny (no… wait… REALLY ANNOYING) when two DRM apps try to prevent each other doing things that they need to do in order to prevent each other doing things that they need to do to prevent each other doing things that they need to do in order to prevent each other…

    See also: Desktop security software ('Please uninstall any other desktop security applications before you install this desktop security application')

  16. Jesse says:

    By far one of my favorite posts. The "Deli" analogy was amazing.

  17. Mike Marcelais says:

    The link on the license plate comment is broken.  (403: Permission denied.)  Maybe too many people are trying to click on it all at once.

    On topic, it's sad how often "I want to do A but B won't let me — how do I kill B?" questions come up (when the right answer is "How do I fix A or cooperate with B?").

  18. Tanveer Badar says:

    I really like the attach and suspend tip. /me goes to write the next awesome adware.

  19. Eddie says:

    Funny, I was thinking of a similar post for my new blog, The Older Newer Thing.

  20. Boris says:

    The right way to handle a limited-time in-store promotion is to paste a poster on top of the computer monitor.  That gives you super-awesome topmost that trumps all ways Raymond is thinking of.

  21. Mike Dimmick says:

    Perhaps use Windows' little-known multiple desktops support to switch desktops. Then the other programs can't pop up on top of yours (and nor can Explorer notifications!) because the console is showing a completely separate environment.

    Idea came from Mark Russinovich's post about using Desktops.exe to launch Process Explorer somewhere that malware can't find it:…/3392087.aspx

    The trick is limited to how much Desktop Heap you have, but if you really need a completely isolated kiosk it might be worth a try. Of course you really should be using an Embedded version of Windows, maybe Windows Embedded Point-of-Service/POSReady.

  22. Brian says:

    Boris: That looks unprofessional.  Instead, let's do the equivalent.  Have a separate computer for each vendor's application and physically swap which computer is connected to the monitor.

  23. Jason says:

    More posts like this.  They make me laugh.

  24. Henke37 says:

    I am not complaining, but this is the third story about the guy who wanted to be the topmost window. I think this entry consumed the remaining jokes. But as I said, not complaining because the last jokes where good ones.

    [It's cute that you think only one guy has requested this. -Raymond]
  25. dave says:

    Does switching into kernel mode and grabbing the scheduler lock help solve the problem?  ;-)

  26. Nik says:

    My application controls a robotic arm holding a sharpie pen that writes on the screen.  It overrides everything: hacked video drivers, switched monitor inputs, labels pasted on the screen.  My application needs to be infinity-topmost because it wants to remind you that there are unused icons on your desktop.

  27. Jake says:

    My application controls a robotic arm that sticks a label on your eyeballs. It overrides everything: hacked video drivers, switched monitor inputs, labels pasted on the screen, robotic arms holding a sharpie pen. My application needs to be double-infinity-topmost because it wants to offer you the chance to change your screen resolution.

  28. Some Guy says:

    Reminds me of fliers on a telephone pole.

  29. Gabe says:

    Jake: Now you've started an arms (get it? "arm"s?) race to see who can create a more powerful robotic arm to hold off the other robotic arms!

  30. Brian G. says:

    My applicatoin controls a robotic arm that peforms optic surgery severing the connection between the optic nerve and your eyeball replacing it wiht a generated bio-electric signal to be interpreted by the brain.  It overrides everything: hacked vidoe drivers, switched monitor inputs, labels pasted on the screen, robotic arms holding a sharpie pen or sticking labels on your eyeballs.  My application needs to be extra-double-infinity-topmost because it wants to offer you the chance to be welcomed to your new operating system and be shown a tour.

    Sadly, since the technology is in its infancy, it'll only be shown in basic 640×480 @ 8-bit color with no hardware acceleration.

  31. David Brooks says:

    Nice to see "coöperate". The diaeresis is still alive, but barely. Raymond, are you a New Yorker reader?

    [Nope. I just learned to spell a long time ago. That's why I spell Hallowe'en with an apostrophe, and then people take the time to "correct" me. -Raymond]
  32. Ken Hagan says:

    Mike Marcelais:

    "On topic, it's sad how often "I want to do A but B won't let me — how do I kill B?" questions come up (when the right answer is "How do I fix A or cooperate with B?")."

    Is that particularly on-topic? I've just been reading the news from North Africa. I don't think the mindset is peculiar to programmers. :(

  33. Joshua Ganes says:

    My application controls a robotic arm that simply kills the user and scavenges the body for cash, credit cards, or anything of value. It overrides everything: hacked video drivers; switched monitor inputs; labels pasted on the screen; robotic arms holding a sharpie pen or sticking labels on the screen or performing optic surgery. My application needs to be ultimate-extra-double-infinity-topmost because it provides the optimal opportunity to get money from the user.

  34. Fred Fnord says:

    We tried … swallowing paint …


  35. Joshua says:

    Ever got HDC to the desktop window and scribbled it?

    Last I checked it still draws on top of everything, but everything gets drawn on top of it as soon as it thinks it needs to be redrawn.

  36. JB says:

    No one is going to defeat my evil plan of printing out my message and then going to each screen and pulling the power and taping my message onto the screen.

  37. Peter says:

    "It's cute that you think only one guy has requested this. -Raymond"

    Hey Raymond, if so many people request this, then why not do something about it?  Maybe Windows could include some sort of arbiter service that would allow foreground hogging applications to share screen time.

    [You're just building a taller wall. "How do I take foreground when the foreground arbiter service denies me permission?" Windows already has a foreground arbiter and these people are unhappy with its policy, so they're looking for taller ladders. -Raymond]
  38. Gabe says:

    I think Peter has hit on something. Let the user assign the Z-order for super-duper-ultra-topmost windows. That way the apps won't have to fight over it and users can get what they want.

  39. Clipboarder Gadget says:

    Well, I think there are legitimate reasons to have super topmost windows. The thumbnails of the win7 taskbar for example are always visible, even if there is a foreground topmost window…

    There seems to be an undocumented function to do this.

  40. What about using a more flexible tool. says:

    Under X11, you'd have options for this, even if you couldn't find a window manager that would do what you want:

    * Write a window manager that had the behavior you want.

    * Run the less important display under XNest.

    I suppose in Windows you could use a VM. :)

  41. Crescens2k says:


    Except that won't make these super duper program writers happy since that won't guarantee that their window will get on top of all the others.

    @Clipboard Gadget

    This is a very different scenario since it comes at the request of the user. Also, if people found the undocumented way to do this, what does that mean, everyone will be using the same method to get their window on top of all the others and the exact same thing will happen again. But undocumented functions have the added advantage of not being guaranteed to be stable.

  42. dave says:

    How about a Win32 call that grants you 10mS of topmost-window time for every $10 it charges to your credit card, which must be supplied as an input argument?

  43. Cheong says:

    Actually I have an idea on how to solve the problem.

    Can there be a feature for DWM that sets a threshold, if a program request being topmost window a number of times within a period, it'll completely ignore the request or send it to another "virtual desktop" so it'll be contented to be to topmost window forever there?

  44. Jacolyte says:

    I could easily solve this problem on Linux, since you might as well pipe everything to /dev/null.

  45. Crescens2k says:


    Actually there is a better way to solve this problem without even touching Windows. This is an obvious design problem, a mass of applications written with no co-ordination or any respect for the possibilities of other windows needing to get on top. What this really needs is a re-architect to deal with this kind of window properly. Regardless of what is done to Windows itself there will always be someone who isn't happy with that and will find a way to do it which suits them, and at the same time not care about the end user. This particular case may be a "valid" reason for doing this, but how many other applications would use this for putting themselves on top because they are the most awesome super duper application available, and how many users would want this.


    Windows has it better you know, it is just NUL on Windows. So we don't have to redirect to /dev/null, but rather just NUL.

  46. Cheong says:

    @Crescens2k: I'm sure that while they MAY be "valid" reason to keep themselves topmost, it's almost ALWAYS wrong to repeatly bash for the topmost state (therefore I suggest setting a theshold).

    Just send them to virtual desktop (or I think even just tell them they're topmost already will do… but they eventally will figure out the workaround like firing up another program to detect, so just send them to other desktop. And I admit that I was tempted to use the word "chrooted" or "caged" but that would likely be overkill.) so that the application's window would be the only window there and must be the topmost one, use the way Win2008 used to deal with interactive service that announce some poorly behaved program is asking for your attention and judge yourself if you really want to see them, and let the user decide.

  47. JustSomeGuy says:

    "How do I create a topmost window that is never covered by other topmost windows?" – What happens when an irresistible force meets an immovable object? Absolutely nothing, since it cannot happen.

    The existence of one precludes the other. If you have an irresistible force, it means nothing can resist it, hence there's no such thing as an immovable object. Likewise, an immovable object means their is no force that it cannot resist.

    You can never have a topmost window that's never covered by other topmost windows since the former means there are no other topmost ones.

    Having awed you guys with my philosophical skills :-), I rather like the idea someone came up with, of the machines running in VMs and the host deciding which VM is maximised at any given point. If life gives you lemons, kick it in the testicles and take its oranges :-)

  48. DodgyBob says:

    Hey, Crescens2k, what happens if you try to create a nul.txt file? :-)

  49. Crescens2k says:


    This suggestion will be a lot of work for nothing, because of one very important fact. In Windows Vista and newer the window manager only allows windows to bring themselves to the front if they are from the active process. If the window is marked as topmost then it will stay on top but loses its focus, but if two windows are marked as topmost and one gets brought ontop of another, the one which goes behind can only get a chance to get its topmost back if it belongs to the same process as the newly selected window. If not, then the function will fail and at most will cause the task bar icon to flash. So a window will only get a chance to get on top if it is allowed by the current process (requires calling a function) or if the user allows (it includes running a new application). So what you suggested is pointless in current Windows versions because the given situation just can't happen.


    Notepad failes with the error Incorrect function. You see, these files and all variations (so NUL.*) would be reserved and they would all point at NUL. This is a filesystem driver guarantee for backwards compatability.

  50. coöperate

    I <3 archaic spelling

    Looking forward to posts w/ ca'n't and connexion

  51. Miral says:


    Nice in theory, but there still exist applications and windows that relentlessly steal focus for no adequately explored reason, even though they're not owned by the active application.  Including apps from MS and other big vendors.  (I'm not going to name specific apps, that's not the point.)


    Both "can't" and "ca'n't" are valid, technically, since it just depends on which letters you're leaving out.  Although the pronunciation is usually closer to the second one.

  52. Lawrence says:

    LOVE this post. Awesome story. Let's hope it teaches someone!

  53. Anton says:

    Out of joke: the real solution is simple. Integrate other applications into yours. If they resist then hack and patch them. There is no game of ladders, whatever you do is final.

  54. Cheong says:

    @Crescens2k: I'm using Win7 Pro with 2 display. When I make VMWare player full screen, it'll occuply the whole 2nd screen. If I have any application previously running on that screen before, they won't come up even if I clicked the application on the taskbar.

    But if I maximized that application before VMWare play runs, even if VMWare player starts in full screen, clicking on the taskbar can still successfully maximize/minimize the first application window.

    So the war will continue… not on topmost window this time, but on topmost maximized/full screen window…

  55. Gabe says:

    Harumph! Everybody knows the correct spelling is "co-operate".

  56. Crescens2k says:


    Strangely enough, on Win7 Ultimate, doing the exact same thing with VMWare workstation has no issues bringing the window behind VMWare to the front.

    But no, I had a long post typed out but I really can't be bothered. Really, whats with people wanting an extremely complex solution to a simple problem.

  57. Medinoc says:

    Gah! The interface swallowed my post again. Why does one need to validate twice when posting? Is that linked to that CAPTCHA issue that was somehow corrected?

  58. TK says:

    How about a Virtual machine?

    Run the applications in separate VMs and write a program to co-ordinate…. although the expression 'sledge hammer to crack a walnut' springs to mind!

  59. Crescens2k says:


    I'm sure it is the site's session timing out because you took a long time to type the post. It does it to me at times, especially with posts I take a long time to type out.

  60. kinokijuf says:

    „coöperate”? Bad bad bad. The ö looks almost like ó, which is pronounced like „oo” in polish. You make me pronounce „cooperate” badly.

  61. mdw says:

    The obvious answer is to allow programs to specify just how much they have their metaphorical hearts set on having the most-tippy-toppest window.  A little thought reveals that having a DWORD or so encode the desire level is no good because is has an obvious maximum value, which everyone will immediately choose.

    So we need to be able to represent a value from some (notionally) unbounded set.  Such a thing will obviously require an unbounded amount of storage to represent.  Once we notice that the main effective limit on the `superness' that one want to specify is the availability of storage, we realise that we don't actually need the value for anything especially interesting: the program which is willing to spend more memory should win.  It stands to reason that superness memory should actually be committed (and therefore unavailable for other purposes).

    So, the interface for making a really-on-the-top-yes-I mean-it window should accept a count of memory pages which the caller is willing to sacrifice in order to prevent other windows from having even more topmostness.

    (Obviously, users will complain about memory usage of programs which have very super windows.)

    Plan B: Microsoft could sell window super-topness certificates.  The cert needs to identify the window in question (umm, class name, creating module name, maybe a pattern for the window's title — this is actually the hard bit) and the amount of superness purchased.  The pricing structure probably ought to be exponential.  It would be extra shiny to donate the proceeds from the sale of these certificates to a charity dedicated to caring for those traumatized by awful user interfaces.

  62. Paul says:

    You apply Polish pronunciation rules to English, you will get errors.

    Raymond's post was compiled for English – if you try to use a Polish parser you are using unsupported features and no liability can be taken by Raymond for complications arising from this situation.

  63. Gabe says:

    mdw: I don't want my programs to bid for topness — I want to specify it myself!

  64. Anonymous says:

    The ö in coöperate follows the convention in some languages where two dots over a vowel says "don't make me into a diphthong" or "don't alter the pronunciation of the previous letter".  Think "naïve", where aï is pronounced as two separate vowels and not blended together.

  65. Alex Grigoriev says:

    Mike D:

    Multiple desktops is the cleanest way. If you have an "arbiter" app that creates a dedicated desktop and then calls CreateProcess from the thread that has that desktop as current, the new process will run there. Then have a DLL loaded to them, that'll hook to windows and communicate with the "arbiter" application.

    Also, this will avoid interference from Windows balloons and other undesired stuff.

  66. WendyHome says:

    Awesome to see my treasured number-plate making a walk-on appearance in a punch-up over line and window positions in a Deli. Life's so exciting!  The Wendyhome felt the concern of over 1000 silent visitors. We're ok :-)

  67. Adam V says:


    mdw: I don't want my programs to bid for topness — I want to specify it myself!

    If a user can do it, a program can pretend to be a user and do it for them. Have you never seen old install programs which loaded unsigned drivers by clicking through all the "PLEASE UNDERSTAND WHAT YOU'RE DOING" popups so fast the user doesn't see them? I think Raymond has referenced the phenomenon before. That's pretty much the ultimate in user-unfriendly to me.

  68. Ben Voigt [Visual C++ MVP] says:

    I don't see a problem here.  I have a way to satisfy multiple applications requesting that their window not ever be covered by any other topmost window, simultaneously, and this method scales fairly to over 1 million simultaneous applications.  I freely offer my solution to the Microsoft Windows development team on the condition that MY applications receive 10 times their fair share of screen pixels as compared to other topmost applications.

  69. M Hotchin says:

    I think "don't make me into a diphthong" should be the new slogan for, um, *something*.

  70. jader3rd says:

    I think the source of the desire to accomplish this is that the implementers of the system don't want to be embarrassed by having Windows OS notifications pop up on the screen. We’ve all seen funny photos of some public digital display with a very out of place pop up, requesting a response from a user. I don’t know for sure, but I suspect these result from somebody using XP (and previously Win98) as the OS for a very non client centric application. Instead of choosing a different OS that’s configured to realize that no user is looking at this display and has its alerting mechanism wired up to an admin console, people are trying to dedicate a full blown Windows OS to just display messages, because that’s the OS they’re familiar with.

    So to all of those people saying that the initial request is legitimate, I say that what they’re trying to accomplish is legitimate, but their solution is not. The reason is because something will go wrong with the system, and since all notifications are covered up, the sad soul who has to figure out what’s wrong is going to spend most of their time trying to figure out a way to see a different window.

    To those people saying that the best thing to do is to coordinate all of the windows, I agree, if the applications were the only windows that could possibly pop up, but they’re not. The best thing to do would be to use an OS that’s not designed under the assumption that someone is constantly staring at a monitor with a keyboard attached to it.

  71. Joe says:

    We have a similar issue with our Windows Embedded based device. Only one of our applications is allowed to be absolute topmost. Our solution to the multiple vendor issue isn't algorithmic, but contractual. To whit: third parties must follow certain guidelines and if they don't, we don't certify their application to run on our device.

  72. Mike says:

    Sadly, I've dealt with the exact scenario in a call center app recently.  The vendor's solution was novel at least…injecting their code into whatever happened to own the foreground.  

    I'd really love to see Microsoft handle this though – force everyone to have some manners.  In my ideal world, this would be permissions based, establish a z order – standard priv. apps can run in say the lowest 100, then 100+ reserved for uac auth'd apps.  It could even be granular enough to completely accommodate the prioritized model that Raymond suggests as a customer need – ie: when we authorize an app to become 'topmost' we can set it's highest 'z' either directly, or via a registry setting.

    In effect Microsoft keeps creating the above situation because they don't give developers a better way.

  73. Anon says:


    I am so happy you aren't in the virus writing business – your thinking is too nefarious!

  74. Dan says:

    @vio "Why not make window A owned by window B? Doing that across processes is asking for trouble, but it's possible" Yes, it is possible. I once needed to embed a Java Swing window inside a Qt window. Now, they were both owned by the same process (Java was run in an embedded JVM), but while I was testing, I was using arbitrary windows (notepad mostly) and it worked just fine. (There were some oddities, such as if I embedded notepads text area, instead of the parent window, and then close it, notepad is left open but without a text area!)

    @Jake, not true. You can reparent any window (any HWND, so a window or any widget) to any other window. I would avoid it if possible though.

Comments are closed.