Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading a WPF Window without showing it

Tags:

window

wpf

hwnd

I create a global hot key to show a window by PInvoking RegisterHotKey(). But to do this I need that window's HWND, which doesn't exist until the window is loaded, that means shown for the first time. But I don't want to show the window before I can set the hot key. Is there a way to create a HWND for that window that is invisible to the user?

like image 553
svick Avatar asked Sep 09 '09 11:09

svick


People also ask

How do I hide a window in WPF?

An alternative to H.B.'s method is just to set the Visibility to hidden and set ShowInTaskbar to false. This still creates the window and lets it do its thing.

How do I display a WPF window?

When a Window is created at run-time using the Window object, it is not visible by default. To make it visible, we can use Show or ShowDialog method. Show method of Window class is responsible for displaying a window.

How do I open a WPF window in full screen?

Currently, the only way I know how to fullscreen my wpf application is: WindowStyle=None and WindowState=Maximized (and Topmost=True, though this is just needed to make sure it's above everything else).

How do I know if a WPF window is open?

In WPF there is a collection of the open Windows in the Application class, you could make a helper method to check if the window is open. Here is an example that will check if any Window of a certain Type or if a Window with a certain name is open, or both. Show activity on this post. Show activity on this post.


2 Answers

If you are targeting .NET 4.0 you can make use of the new EnsureHandle method available on the WindowInteropHelper:

public void InitHwnd() {     var helper = new WindowInteropHelper(this);     helper.EnsureHandle(); } 

(thanks to Thomas Levesque for pointing this out.)

If you are targeting an older version of the .NET Framework, the easiest way is to show the window to get to the HWND while setting a few properties to make sure that the window is invisible and doesn't steal focus:

var window = new Window() //make sure the window is invisible {     Width = 0,     Height = 0,     WindowStyle = WindowStyle.None,     ShowInTaskbar = false,     ShowActivated = false }; window.Show(); 

Once you want to show the actual window you can then set the Content, the size and change the style back to a normal window.

like image 191
Patrick Klug Avatar answered Sep 17 '22 20:09

Patrick Klug


You can also change the window into a so called message-only window. As this window type does not support graphical elements it will never be shown. Basically it comes down to calling:

    SetParent(hwnd, (IntPtr)HWND_MESSAGE); 

Either create a dedicated message window which will always be hidden, or use the real GUI window and change it back to a normal window when you want to display it. See the code below for a more complete example.

    [DllImport("user32.dll")]     static extern IntPtr SetParent(IntPtr hwnd, IntPtr hwndNewParent);      private const int HWND_MESSAGE = -3;      private IntPtr hwnd;     private IntPtr oldParent;      protected override void OnSourceInitialized(EventArgs e)     {         base.OnSourceInitialized(e);         HwndSource hwndSource = PresentationSource.FromVisual(this) as HwndSource;          if (hwndSource != null)         {             hwnd = hwndSource.Handle;             oldParent = SetParent(hwnd, (IntPtr)HWND_MESSAGE);             Visibility = Visibility.Hidden;         }     }      private void OpenWindowMenuItem_Click(object sender, RoutedEventArgs e)     {         SetParent(hwnd, oldParent);         Show();         Activate();     } 

For me the solution of setting the width, height to zero and style to none didn't work out, as it still showed a tiny window, with an annoying shadow of what seems to be the border around a 0x0 window (tested on Windows 7). Therefore I'm providing this alternative option.

like image 27
DJP Avatar answered Sep 19 '22 20:09

DJP