Click here to Skip to main content
15,881,248 members
Articles / General Programming / Tools

Utility to make important windows remain always on top

Rate me:
Please Sign up or sign in to vote.
4.96/5 (8 votes)
15 Aug 2014CPOL1 min read 32.7K   9   7
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? Here's an app for that.

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: 

Image 1

You can use this AlwaysOnTop utility to make any window remain always on top of other windows. 

Image 2

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:

C#
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:

C#
[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:

C#
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) // Normal
                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!

Image 3

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect BT, UK (ex British Telecom)
United Kingdom United Kingdom

Comments and Discussions

 
Generalextra benefit for me Pin
Brian_Lowe10-Dec-14 2:00
professionalBrian_Lowe10-Dec-14 2:00 
QuestionMight be the first step towards a solution to another problem I'm having Pin
dandy7217-Aug-14 3:36
dandy7217-Aug-14 3:36 
QuestionShould you also specify SWP_NOACTIVATE Pin
Philippe Mori16-Aug-14 2:59
Philippe Mori16-Aug-14 2:59 
GeneralMy vote of 5 Pin
deshjibon15-Jul-14 9:33
deshjibon15-Jul-14 9:33 
SuggestionCheck the image.. Pin
DaveAuld8-Jul-14 5:38
professionalDaveAuld8-Jul-14 5:38 
GeneralRe: Check the image.. Pin
Omar Al Zabir8-Jul-14 5:53
Omar Al Zabir8-Jul-14 5:53 
SuggestionThis is more a Tip than an article...... Pin
DaveAuld8-Jul-14 5:13
professionalDaveAuld8-Jul-14 5:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.