Live Chat Icon For mobile
Live Chat Icon

How can I show a form without making it active

Platform: WinForms| Category: Win32

Normally when you make a Form visible by setting the Visible property to true, it will show the form and set the focus too. In some cases however, you do not want it to take focus until the user clicks on it. To get this behavior, do the following utility code:

When you want to show a form without activating it:

	UtilFuncs.SetVisibleNoActivate(myForm, true); // true to show.

When you want to hide it:

	UtilFuncs.SetVisibleNoActivate(myForm, false); // false to hide.
public class UtilFuncs
{
	[DllImport('USER32.dll')]
	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_NOZORDER = 4; // 0x0004 
	public const int SWP_NOACTIVATE = 16; // 0x0010 
	public const int SWP_SHOWWINDOW = 64; // 0x0040 
	public const int SWP_HIDEWINDOW = 128; // 0x0080 
	public const int SWP_DRAWFRAME = 32; // 0x0020 

	public static void ShowWindowTopMost(IntPtr handle)  
	{
		SetWindowPos(handle, 
			(IntPtr)HWND_TOPMOST, 0, 0, 0, 0, 
			SWP_NOSIZE|SWP_NOMOVE|SWP_NOACTIVATE|SWP_SHOWWINDOW);
	}

	public static void SetVisibleNoActivate(Control control, bool visible)
	{
		if(visible)
		{
			ShowWindowTopMost(control.Handle);
			control.Visible = true;
		}
		else
			control.Visible = false;
	}
}

Share with

Share on twitter
Share on facebook
Share on linkedin

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.