none
How Can I do to disactive when wpf window start? RRS feed

  • Question

  •  

    hello,all

    Now I  create a wpf application ,and I want to the mainwindow do not active when It start,Just in c++ use createwindowEx with WS_EX_NOACTIVE style.How can I do?

    Monday, September 24, 2007 9:28 AM

Answers

  • Hello, unfortunately, there’s a known issue which preventing you from showing a Window without activating it by setting WS_EX_NOACTIVE in WPF. Fortunately, our developers Steve and Andre have found a workaround.

     

    Basically, you’ll need to set a CBT(computer-based training) hook by calling the Win32 SetWindowsHookEx function. This hook procedure will happen before activating, creating, destroying, etc… Thus you can use it to prevent the window from activating by set the return value to 1 (similar to RoutedEventArgs.Handled property). Try something like this:

     

    First the native functions:

            [DllImport("user32.dll")]

            static extern IntPtr SetWindowsHookEx(int code, HookProc func, IntPtr hInstance, int threadID);

            [DllImport("user32.dll")]

            static extern bool UnhookWindowsHookEx(IntPtr hhk);

            [DllImport("user32.dll")]

            static extern int CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);

     

    We’ll need a delegate for the callback function:

            private delegate int HookProc(int code, IntPtr wParam, IntPtr lParam);

            private HookProc cbtCallbackDelegate;

            private IntPtr hook;

     

    Now in the Window’s constructor (or any place before the window is shown), let’s setup the hook:

            public Window1()

            {

                InitializeComponent();

                this.cbtCallbackDelegate = new HookProc(CbtCallbackFunction);

                hook = SetWindowsHookEx(5 /* wh_cbt */, this.cbtCallbackDelegate, IntPtr.Zero, AppDomain.GetCurrentThreadId());

            }

     

    Then in the callback function, we’ll check the code. If it is HCBT_ACTIVATE, we can unhook the hook, since we only want to prevent the first activation. For other codes, just let them pass in the normal way by calling CallNextHookEx function:

            private int CbtCallbackFunction(int code, IntPtr wParam, IntPtr lParam)

            {

                switch (code)

                {

                    case 5: /* HCBT_ACTIVATE */

                        UnhookWindowsHookEx(hook);

                        return 1; /* prevent windows from handling activate */

                }

                //return the value returned by CallNextHookEx

                return CallNextHookEx(IntPtr.Zero, code, wParam, lParam);

            }

     

     

     

    Wednesday, September 26, 2007 7:10 AM

All replies

  • Hi,

    Just start the application without arguments:

    class Program
        {
            static void Main(string[] args)
            {
                System.Windows.Application app = new System.Windows.Application();
                app.Run();
            }
        }

    Best regards,

    Benny
    Monday, September 24, 2007 1:53 PM
  • Have you tried to listen to Window.SourceInitialized and set window style bit (WS_EX_NOACTIVE) using SetWindowLong? SourceInitialized is fired after the hwnd is created but before it is first shown.

    Tuesday, September 25, 2007 11:54 PM
  • Hello, unfortunately, there’s a known issue which preventing you from showing a Window without activating it by setting WS_EX_NOACTIVE in WPF. Fortunately, our developers Steve and Andre have found a workaround.

     

    Basically, you’ll need to set a CBT(computer-based training) hook by calling the Win32 SetWindowsHookEx function. This hook procedure will happen before activating, creating, destroying, etc… Thus you can use it to prevent the window from activating by set the return value to 1 (similar to RoutedEventArgs.Handled property). Try something like this:

     

    First the native functions:

            [DllImport("user32.dll")]

            static extern IntPtr SetWindowsHookEx(int code, HookProc func, IntPtr hInstance, int threadID);

            [DllImport("user32.dll")]

            static extern bool UnhookWindowsHookEx(IntPtr hhk);

            [DllImport("user32.dll")]

            static extern int CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);

     

    We’ll need a delegate for the callback function:

            private delegate int HookProc(int code, IntPtr wParam, IntPtr lParam);

            private HookProc cbtCallbackDelegate;

            private IntPtr hook;

     

    Now in the Window’s constructor (or any place before the window is shown), let’s setup the hook:

            public Window1()

            {

                InitializeComponent();

                this.cbtCallbackDelegate = new HookProc(CbtCallbackFunction);

                hook = SetWindowsHookEx(5 /* wh_cbt */, this.cbtCallbackDelegate, IntPtr.Zero, AppDomain.GetCurrentThreadId());

            }

     

    Then in the callback function, we’ll check the code. If it is HCBT_ACTIVATE, we can unhook the hook, since we only want to prevent the first activation. For other codes, just let them pass in the normal way by calling CallNextHookEx function:

            private int CbtCallbackFunction(int code, IntPtr wParam, IntPtr lParam)

            {

                switch (code)

                {

                    case 5: /* HCBT_ACTIVATE */

                        UnhookWindowsHookEx(hook);

                        return 1; /* prevent windows from handling activate */

                }

                //return the value returned by CallNextHookEx

                return CallNextHookEx(IntPtr.Zero, code, wParam, lParam);

            }

     

     

     

    Wednesday, September 26, 2007 7:10 AM
  • its very useful. thanks Yi-Lun Luo. I have slight different need.

    I need to bring my WPF window on some specific occasions during lifetime of application. So I am using 

    MainView.Activate(); 

    So, can I directly call this callback function from my view-model whenever I Activate my window ?

    Dheeraj

    Tuesday, January 8, 2013 12:08 PM