How to Make a Window Always Stay on Top in .Net

How to make a window always stay on top in .Net?

Form.TopMost will work unless the other program is creating topmost windows.

There is no way to create a window that is not covered by new topmost windows of another process. Raymond Chen explained why.

How to make form always on top in Application

You are looking for an owned window. It is always on top of the owner and it gets minimized along with the owner. Good examples of owned windows are the various helper windows inside Visual Studio. You can undock them but they'll always stay on top of the VS main window.

You create an owned window by displaying it with the Show(owner) overload. Or by explicitly assigning its Owner property.

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;
}

How do I include Always on Top functionality into my C# application

if its winform application Form.TopMost will work

Make window always stay on top of ANOTHER window that already stays on top?

Thanks to SLaks's answer and some of the comments on it, I was able to figure out how set a child-parent relationship between my forms. I couldn't use Form.Show(owner), because the form that I wanted to stay in front was shown before the other form. I used Reflector to examine the code behind Form.Show(owner) and discovered that behind the scenes, it all resolves down to SetWindowLong in the Windows API.

LONG SetWindowLong(      
HWND hWnd,
int nIndex,
LONG dwNewLong
);

Form.Show(owner) calls SetWindowLong with an nIndex of -8. The MSDN online documentation won't tell you about it, but according to Winuser.h one of the constants available for nIndex is GWL_HWNDPARENT, which has a value of -8. Once I connected these dots, the problem was quite easy to solve.

This following is how to set the window's parent, even if it is already shown:

using System.Runtime.InteropServices;

[DllImport("user32.dll")]
public static extern int SetWindowLong(HandleRef hWnd, int nIndex, HandleRef dwNewLong);

public static void SetOwner(IWin32Window child, IWin32Window owner)
{
NativeMethods.SetWindowLong(
new HandleRef(child, child.Handle),
-8, // GWL_HWNDPARENT
new HandleRef(owner, owner.Handle));
}

How to make Windows Always on top application

For making it generic to any process, you have to overload two methods of the User32.dll which is a part of Win32 API.

Just use the code given below and specify your process name without its extension, say for vlc - specify

processName = "vlc"; and NOT LIKE "vlc.exe"

using System.Runtime.InteropServices;
using System.Diagnostics;

public class ProcessManager
{
[DllImport("user32.dll")]
private static extern bool ShowWindow(IntPtr hWnd, uint windowStyle);

[DllImport("user32.dll")]
private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint uFlags);

ProcessManager()
{
string processName = "vlc"; /* Your process name here */
SearchProcessAndModifyState(processName);
}

void SearchProcessAndModifyState(string targetProcessName)
{
Process specifiedProcess = null;
Process[] processes = Process.GetProcesses();
for (int i = 0; i < processes.Length; i++)
{
Process process = processes[i];
if (process.ProcessName == targetProcessName)
{
specifiedProcess = process;
break;
}
}
if (specifiedProcess != null)
{
ProcessManager.ShowWindow(specifiedProcess.MainWindowHandle, 1u);
ProcessManager.SetWindowPos(specifiedProcess.MainWindowHandle, new IntPtr(-1), 0, 0, 0, 0, 3u);
}
}
}

Visual Basic .Net, keep windows on top of other on-top windows

Apparently the solution was pretty simple.

I was afraid of side-effects, but the solution I applied works perfectly.

I added a timer, enabled it, and every tick it simply does:

me.Topmost = true

If I now click the taskbar, the program is obviously pushed to the back, but every 100ms when the timer ticks, the program is automatically put back on top without the app actually receiving focus.



Related Topics



Leave a reply



Submit