Click here to Skip to main content
12,693,244 members (26,640 online)
Click here to Skip to main content
Add your own
alternative version

Stats

16.7K views
10 bookmarked
Posted

Utility to make important windows remain always on top

, 15 Aug 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
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: 

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) // 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!

License

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

Share

About the Author


You may also be interested in...

Pro
Pro

Comments and Discussions

 
Generalextra benefit for me Pin
Brian_Lowe10-Dec-14 3:00
memberBrian_Lowe10-Dec-14 3:00 
QuestionMight be the first step towards a solution to another problem I'm having Pin
dandy7217-Aug-14 4:36
memberdandy7217-Aug-14 4:36 
QuestionShould you also specify SWP_NOACTIVATE Pin
Philippe Mori16-Aug-14 3:59
memberPhilippe Mori16-Aug-14 3:59 
GeneralMy vote of 5 Pin
deshjibon15-Jul-14 10:33
memberdeshjibon15-Jul-14 10:33 
SuggestionCheck the image.. Pin
DaveAuld8-Jul-14 6:38
protectorDaveAuld8-Jul-14 6:38 
GeneralRe: Check the image.. Pin
Omar Al Zabir8-Jul-14 6:53
memberOmar Al Zabir8-Jul-14 6:53 
SuggestionThis is more a Tip than an article...... Pin
DaveAuld8-Jul-14 6:13
protectorDaveAuld8-Jul-14 6: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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 15 Aug 2014
Article Copyright 2014 by Omar Al Zabir
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid