Coder
Coder

Reputation: 79

How do I make my window on top always like firefox's picture-in-picture feature?

I mean this feature:

enter image description here

So you can open other stuff, folder, files, etc. and that window is on top always. I managed to archive pretty much that with:

SetWindowPos(Handle, HWND_TOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS);

but there are cases like pressing windows+D or open brave browser, where it's no longer on top. The first case is very important. How do I fix that? I thought making global hotkey to prevent window+D to work would be a hacky and not only that I need to make it so only in my application not make windows+D to stop overall. It just need to keep that window on top even so. Here's my current code so far:

using System.Runtime.InteropServices;

namespace fixedWindow
{
    public partial class Form1 : Form
    {
        private static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
        private const UInt32 SWP_NOSIZE = 0x0001;
        private const UInt32 SWP_NOMOVE = 0x0002;
        private const UInt32 TOPMOST_FLAGS = SWP_NOMOVE | SWP_NOSIZE;

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

        public Form1()
        {
            InitializeComponent();
 
        }

        private void Form1_Load(object sender, EventArgs e)
        {
           SetWindowPos(Handle, HWND_TOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS);
        }

    }
}

Upvotes: 0

Views: 680

Answers (0)

Related Questions