Introduction
Do you sometimes fail to notice Outlook reminder window? Do you wish a chat window would remain always on top of other windows so that you never miss a message? Do you want to have a notepad always on top so that you can take quick notes anytime, while working on other apps?
We have an app for that.
It runs quietly on system tray and monitors open windows. Whenever it finds a window that you want to make always on top, it does that:
You can use this AlwaysOnTop utility to make any window remain always on top of other windows.
Using the code
You can just download the binaries, extract and run.
https://github.com/oazabir/AlwaysOnTop/blob/master/Binary/AlwaysOnTop.zip?raw=true
The source code is here:
https://github.com/oazabir/AlwaysOnTop
Points of Interest
The app periodically scans the open windows, and checks the title against a set of user defined matches. If the window title contains the user defined words, then it calls SetWindowPos in user32.dll to make the window appear always on top.
Here's how it does it:
public static void AlwaysOnTop(string titlePart)
User32.EnumDelegate filter = delegate(IntPtr hWnd, int lParam)
StringBuilder strbTitle = new StringBuilder(255);
int nLength = User32.GetWindowText(hWnd, strbTitle, strbTitle.Capacity + 1);
string strTitle = strbTitle.ToString();
if (User32.IsWindowVisible(hWnd) && string.IsNullOrEmpty(strTitle) == false)
if (strTitle.Contains(titlePart))
SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
return true;
User32.EnumDesktopWindows(IntPtr.Zero, filter, IntPtr.Zero);
The EnumDesktopWindows is defined as:
[DllImport("user32.dll", EntryPoint = "EnumDesktopWindows",
ExactSpelling = false, CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool EnumDesktopWindows(IntPtr hDesktop, EnumDelegate lpEnumCallbackFunction, IntPtr lParam);
Setting window always on top is easy. But removing always on top, is difficult. If you just loop through the windows and call SetWindowPos to remove the always on top flag, it hangs. You have to specifically check if the window is a normal window. For that, some additional code is required:
public static void RemoveAlwaysOnTop()
User32.EnumDelegate filter = delegate(IntPtr hWnd, int lParam)
StringBuilder strbTitle = new StringBuilder(255);
int nLength = User32.GetWindowText(hWnd, strbTitle, strbTitle.Capacity + 1);
string strTitle = strbTitle.ToString();
if (!string.IsNullOrEmpty(strTitle))
WINDOWPLACEMENT placement = GetPlacement(hWnd);
if (placement.showCmd == SW_SHOWNORMAL)
SetWindowPos(hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
return true;
User32.EnumDesktopWindows(IntPtr.Zero, filter, IntPtr.Zero);
That's all the fun there is!
I am: Head of Engineering Consumer CRM & Billing, BT (ex British Telecom).
I got: 11 Microsoft MVP Awards, 4 CodeProject MVP awards.
I like: Performance and Scalability Challenges.
My Book: Building a Web 2.0 portal using ASP.NET 3.5. Also on Amazon
My Blog: http://omaralzabir.com
My Specialization: Web 2.0, SaaS, Performance and Scalability of Web Apps.
My Email: OmarALZabir at gmail dot com
Follow Me: twitter.com/omaralzabir
My Projects:
Droptiles: Open Source Web 2.0 AJAX Portal
SQL Server real-time monitor
Oracle real-time monitor
Codeuml - UML diagrams as fast as you can type
Fastest Streaming AJAX Proxy
CPU Alerter - Save power, work faster
My Articles:
Top 10 caching mistakes
99.99% Available Production Architecture
10 ASP.NET Performance and Scalability Secrets
UFrame: UpdatePanel and IFRAME combined
Fast ASP.NET web page loading
Fast Streaming AJAX Proxy
Using COM safely inside "using" block without requiring interop assembly
Implementing Word Like Automation Model
Distributed Command Pattern
I've been troubled with windows randomly switching to "always on top" - most commonly Chrome but also Visual Studio and sometimes windows' own file explorer. When it happens I get no clue, until I do something which should open a dialog or new window but nothing appears, or I drag another window and have it disappear behind a rogue application. This is mostly just annoying but also hides notifications from HipChat and others.
Your code might point me in the direction of a new utility to periodically check for unwanted "always on top" windows and reset them to normal.
Thanks!
Sign In·View Thread
Might be the first step towards a solution to another problem I'm having 
dandy7217-Aug-14 3:36
dandy7217-Aug-14 3:36
Nice utility. This got me thinking...
I practically live in full-screen RDP windows. I also need to run Skype in the background. That is, I run it on my local machine, as running Skype remotely (within those RDP sessions) results in rather poor performance. If it were practical at all, which isn't the case...
The problem I'm having is that because full-screen RDP window sessions hide popups from the local machine, I miss all visual notifications Skype is trying to send me, and have to rely solely on its audio cues. The problem with that is that I tend to run my system with the volume set very low (assuming my speakers are turned on to begin with), so I easily miss those as well, or if a notification happens when I'm away, then I won't know about it until I happen to minimize the RDP window (which can be hours later).
It wouldn't be terribly efficient to have this utility constantly pool for all open windows, then make the Skype notification window appear on top when it does show up...however, if the app can be notified when a new window is brought up, that might work out better.
So now the question I need to answer is, how do I get the app notified when new windows are created? My first thought is WMI events. Anybody got a better alternative before I start down that path?
[Edit]
I didn't realize there's already a timer that periodically pools for the opened windows. Upon reading the code in more detail, I suppose it's necessary to cover most scenarios. So, it seems to do everything I want out of the box...neat.
Worth a five.
modified 17-Aug-14 10:24am.
Sign In·View Thread
I would thing that the utility should not activate other Windows when changing their position...
Philippe Mori
Sign In·View Thread
but your work email address is visible in the picture!
You might want to replace the picture with a fuzzed one. 
Sign In·View Thread
Web02
2.8:2023-08-14:1