Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set the 'always on top' flag/setting on a window that is external to my application?

Tags:

c#

winapi

Is there a managed way to set the always on top flag/setting on a window that is external to my application or will I need to P/Invoke a native function?

And if P/Invoke is the only way what is the function call required and from which dll?

like image 970
InvertedAcceleration Avatar asked Feb 28 '23 16:02

InvertedAcceleration


1 Answers

Since asking the question I have been researching this and came across what looks like a good example of how to achieve this via p/invoking SetWindowPos in 'user32.dll'. I will come back and accept this answer if this works.

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

    static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);

    const UInt32 SWP_NOSIZE = 0x0001;
    const UInt32 SWP_NOMOVE = 0x0002;
    const UInt32 TOPMOST_FLAGS = SWP_NOMOVE | SWP_NOSIZE;

    public static void MakeTopMost (IntPtr hWnd)
    {
        SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS);
    }
like image 126
InvertedAcceleration Avatar answered Apr 28 '23 23:04

InvertedAcceleration