Thursday, October 18, 2007


How to display a top level window in .NET without making it active

For the poGmail program I needed a way to display a semi transparent alert window in the corner of the screen that was on top of all the other windows without giving it focus. .NET supports transparent windows but the forms are always brought to the front and given the input focus when you call .Show(). This is not nice when you are working, as whatever you write goes to the alert window instead of the current window.

The class below uses interop to modify the form so it does not get activated when you show it. You can call it from the constructor of the form like this: 


using System;

using System.Windows.Forms;

using System.Runtime.InteropServices;


namespace NoFocusWindow


    class WindowUtils



        extern public static bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags);


        public const int HWND_TOPMOST = -1; // 0xffff

        public const int SWP_NOSIZE = 1; // 0x0001

        public const int SWP_NOMOVE = 2; // 0x0002        

        public const int SWP_NOACTIVATE = 16; // 0x0010

        public const int SWP_SHOWWINDOW = 64; // 0x0040        


        public static void SetWindowTopMostWithoutFocus(IntPtr handle)



                 (IntPtr)HWND_TOPMOST, 0, 0, 0, 0,




        public static void SetWindowTopMostWithoutFocus(Control ctrl)







Based on this class

No comments:

Post a Comment