WPF Tips and Tricks: Window.Show() Without Activating The Window

August 22 2007

So you want to spawn a new WPF window but you don't want it to be activated?  This is doable in Win32, but with WPF, it presents some problems ... until now.  This issue recently came up and some folks (read: not me) on the WPF team discovered a a pretty clever workaround.  Basically, the workaround is to use an obscure windows hooks that exists for Computer Based Training (CBT), which allows or a training program to choose to ignore or allow messages to get through to the window.

So, the solution is to set the hook before calling the Show() method on the window. The call back receives the HCBT_ACTIVATE notification, returns 1 to ignore/prevent the operation, then unhooks the hook. The window is shown, but does not take focus. There is no flicker or visible degradation.

If you'd like to see this working in action, check out the sample application that I've deployed via ClickOnce.  You'll notice that the main window will spawn three windows yet you will continue to be able to type in the textbox of the main window. I've posted the source code and here's the crux of the code if you are interested:

 

using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;


namespace ShowWindowWithoutActivating
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>

    public partial class Window1 : System.Windows.Window
    {
        delegate int HookProc(int code, IntPtr wParam, IntPtr lParam);
        private HookProc myCallbackDelegate;
        IntPtr hook;

        DispatcherTimer dt = new DispatcherTimer();

        int counter = 0;

        public Window1()
        {
            InitializeComponent();
            textBox1.Focus();
            dt.Interval = new System.TimeSpan(0, 0, 3);
            dt.Tick += new EventHandler(TimerTick);
            dt.Start();

            // initialize our delegate
            this.myCallbackDelegate = new HookProc(this.MyCallbackFunction);

        }
        public void TimerTick(object sender, EventArgs e)
        {
            if (counter == 2) dt.Stop();
            counter++;

            Window w = new Window();
            w.Title = "TestWindow";
            w.Width = 400;
            w.Height = 300;
            w.Top = 400;
            w.Left = 400;

            // setup a cbt hook
            hook = SetWindowsHookEx(5 /* wh_cbt */, this.myCallbackDelegate, IntPtr.Zero, AppDomain.GetCurrentThreadId());
            w.Show();
        }

        private int MyCallbackFunction(int code, IntPtr wParam, IntPtr lParam)
        {
            switch (code)
            {
                case 5: /* HCBT_ACTVIATE */
                    UnhookWindowsHookEx(hook);
                    return 1; /* prevent windows from handling activate */
            }
            //return the value returned by CallNextHookEx
            return CallNextHookEx(IntPtr.Zero, code, wParam, lParam);
        }

        [DllImport("user32.dll")]
        static extern bool UnhookWindowsHookEx(IntPtr hhk);

        [DllImport("user32.dll")]
        static extern IntPtr SetWindowsHookEx(int code, HookProc func, IntPtr hInstance, int threadID);

        [DllImport("user32.dll")]
        static extern int CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);

    }
}

Comments (14) -

11/18/2007 10:56:00 AM #

Karsten,

I'd love to hear more about Flotsam for our email newsletter InteractiveTV Today [itvt] and for our show, The TV of Tomorrow Show  March 11-12, 2008 in San Francisco.  

Thanks very much,

Tracy
          

Tracy Swedlow

2/15/2011 4:47:35 AM #

Joey

Why not simply set ShowActivated=false in the Window object? That does exactly the same job! Wink

Joey

2/15/2011 4:47:57 AM #

Joey

Why not simply set ShowActivated=false in the Window object? That does exactly the same job! Wink

Joey

2/15/2011 4:48:19 AM #

Joey

Why not simply set ShowActivated=false in the Window object? That does exactly the same job! Wink

Joey

2/15/2011 4:48:27 AM #

Joey

Why not simply set ShowActivated=false in the Window object? That does exactly the same job! Wink

Joey

4/11/2011 5:49:25 PM #

Cheap Max Shoes

You may not be ready to control the instances, but you are able to compose your response. It is possible to turn your ache into profanity -or into poetry. The selection is up to you. You may well not have selected your challenging time, but you'll be able to choose how you'll react to it.

Cheap Max Shoes

4/21/2011 5:59:00 PM #

GHD Straightener Outlet

You will discover hundreds of millions exactly who imagine this Messiah possesses are available. In the event he / she performed, subsequently it truly is sad to say the way it is of which his or her brave giving up in addition to demise have gotten not any consequence at all within the incredibly dilemma his or her forthcoming might have been required to target, intended for heritage proves, further than dilemma, that any of us Christians are as risky, singly in addition to en masse, seeing that non-Christians.

GHD Straightener Outlet

9/22/2011 10:35:17 PM #

iphone 4 case


just wanted to add a comment here to mention thanks for you very nice ideas. Blogs are troublesome to run and time consuming thus I appreciate when I see well written material. Your time isn’t going to waste with your posts. Thanks so much and stick with it No doubt you will definitely reach your goals! have a great day!

iphone 4 case

9/27/2011 12:18:49 AM #

iphone 4 case

If you find the one that you need, you can get it there, you can also tell your friend.That is very kind of you to write this share for us, thanks a lot.

iphone 4 case

3/5/2012 2:06:01 AM #

a

a.com a.com a.com

a

3/5/2012 2:06:17 AM #

a

a.com a.com a.com

a

3/5/2012 2:06:41 AM #

a

a.com a.com a.com

a

5/16/2012 10:23:46 PM #

casino free games

Nice weblog! Keep it " up " these intresting content, Let me bide time until the another one particular.

casino free games

5/16/2012 10:24:04 PM #

casino free games

Nice weblog! Keep it " up " these intresting content, Let me bide time until the another one particular.

casino free games

Add comment

Enter your name, handle, alias, or email.

We'll incarnate your avatar from the services below.



biuquote
Loading