Always on top window

Un petit bout d’interop pour créer des TopMost window. Les flags permettent d’éviter a la fenêtre de se resizer et de bouger après l’appel de la fonction. (Sinon, avec les arguments {x,y,cx,cy} à 0, la fenêtre fera 0*0 à la coordonnée 0:0).
Ensuite le placement de la fenêtre se fait avec le IntPtr HWND_x.

        private static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
        private static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2);
        private const UInt32 SWP_NOSIZE = 0x0001;
        private const UInt32 SWP_NOMOVE = 0x0002;
        private const UInt32 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 void SetOnTop()
        {
            SetWindowPos(this.Handle, HWND_TOPMOST, 0, 0, 0, 0, FLAGS);
        }

        public void UnSetOnTop()
        {
            SetWindowPos(this.Handle, HWND_NOTOPMOST, 0, 0, 0, 0, FLAGS);
        }

Ref. : msdn

Published by Emmanuel Istace

Musician, Software developer and guitar instructor. https://emmanuelistace.be/

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: