How to Check If Window Is "Always on Top"

How to check if window is Always on top?

I think you can do this:

DWORD dwExStyle = ::GetWindowLong(m_hWnd, GWL_EXSTYLE);

if ((dwExStyle & WS_EX_TOPMOST) != 0)
{
// do stuff
}

Here's the MSDN link - http://msdn.microsoft.com/en-us/library/ms633584(VS.85).aspx

And here's the MSDN link to the extended styles - http://msdn.microsoft.com/en-us/library/ff700543(v=VS.85).aspx - topmost is currently listed as "TBD" :)

How to check if my current window is the top most visible window

Call GetWindow passing your topmost window's handle and the GW_HWNDFIRST flag. The window returned will be the topmost window that is highest in the Z-order. You can then use the GW_HWNDNEXT flag to walk through the topmost windows in order of decreasing Z-order until you find yours. If any of the windows overlap your window, then your window is underneath.

How to detect if an application window is set to 'always on top' / 'top most' (that is not part of my application)?

You'll need to P/Invoke GetWindowLongPtr() to get the extended style of the window (GWL_EXSTYLE = -20) and check if the WS_EX_TOPMOST style is turned on (0x08). Visit pinvoke.net for the declarations.

C# / .NET: Get all windows with a stay on top flag

You could use GetWindowLong.

Assuming you have the window handle, you could do the following :

public static bool IsWindowTopMost(IntPtr Handle)
{
return (GetWindowLong(Handle, GWL_EXSTYLE) & WS_EX_TOPMOST) != 0;
}

Keep browser window always on top for a valid use case

I don't think what you're asking for exists, no.

The closest that I can think of is the fullscreen API, but that won't work for your use case — it sounds like you want other windows to be visible, just not on top.

I think you'll have to use user training to get the best results you can, telling them to be sure that their face is visible and not behind some other window.

Enumerate all windows which are always on top

Here's a working example which finds all processes which have a topmost window. Be careful though: Windows Explorer always has a topmost window and you probably don't want to kill that process.

class Program
{
const int WS_EX_TOPMOST = 0x00000008;
const int WS_VISIBLE = 0x10000000;
const int GWL_STYLE = -16;
const int GWL_EXSTYLE = -20;

static void Main(string[] args)
{
var topmostWindowHandles = new ArrayList();
EnumWindows(EnumWindowsCallback, topmostWindowHandles);

var processesToKill = new HashSet<uint>();
foreach (IntPtr hWnd in topmostWindowHandles)
{
uint processId = 0;
GetWindowThreadProcessId(hWnd, out processId);
processesToKill.Add(processId);
}

foreach (uint pid in processesToKill)
{
Process proc = Process.GetProcessById((int)pid);
Console.WriteLine("Killing " + proc.ProcessName);
// kill process, except explorer.exe
}
}

static bool EnumWindowsCallback(IntPtr hWnd, ArrayList lParam)
{
int exStyle = GetWindowLong(hWnd, GWL_EXSTYLE);
int style = GetWindowLong(hWnd, GWL_STYLE);
if ((exStyle & WS_EX_TOPMOST) == WS_EX_TOPMOST
&& (style & WS_VISIBLE) == WS_VISIBLE)
{
lParam.Add(hWnd);
}
return true;
}

public delegate bool EnumWindowsProc(IntPtr hwnd, ArrayList lParam);

[DllImport("user32.dll")]
[return:MarshalAs(UnmanagedType.Bool)]
static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, ArrayList lParam);

[DllImport("user32.dll")]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);

[DllImport("user32.dll")]
static extern int GetWindowLong(IntPtr hWnd, int nIndex);
}

Qt - Is there a way to check if the window is marked always-on-top (linux)?

Using xlib, the needed window state hint can be checked calling the XGetWindowProperty function.

Check requisites first, e.g. sudo apt-get install libx11-dev.

In the pro file, link xlib and require the x11extras qt module.

QT += x11extras
LIBS += -lX11

This is a working example, a function that returns true if the passed-in widget pointer points to a always-on-top window:

#include <X11/Xlib.h>
#include <QtX11Extras/QX11Info>

bool isAlwaysOnTop(QWidget * widget)
{
Atom atr;
int afr;
unsigned long items;
unsigned long bytes;
unsigned char *data;

Display * display = QX11Info::display();
Atom property = XInternAtom(display, "_NET_WM_STATE", False);
if(XGetWindowProperty(display, widget->winId(), property, 0L, 1L, False, 4, &atr, &afr, &items, &bytes, &data) == 0)
{
Atom abv = XInternAtom(display, "_NET_WM_STATE_ABOVE", False);
Atom res = reinterpret_cast<Atom *>(data)[0];
return (res==abv);
}
return false;
}

It can be used from inside the widget closeEvent:

void Form::closeEvent(QCloseEvent *)
{
qDebug() << isAlwaysOnTop(this);
}

How fix window on top

You can make your window top-most by setting the WS_EX_TOPMOST extended style when you create the window, or afterwards by calling SetWindowPos with HWND_TOPMOST as the second parameter.

Note however there's no way to make your window stay on top of other top-most windows (i.e. there's no "on top of absolutely everything" flag).



Related Topics



Leave a reply



Submit