none
Is there a way to find that application is activated or active in WPF ? WM_ACTIVATEAPP

    Question

  • Hello every one

    Is there a way to find that application is  activate or activated in WPF ?
    we use
    public static Window ActiveWindow
            {
                get
                {
                    if (Application.Current != null)
                        foreach (Window window in Application.Current.Windows)
                            if (window.IsActive)
                                return window;
                    return null;
                }

            }

    if(ActiveWindow !=null)
         MessageBox.Show("Application is active");
    -------------------------------
    but in some situation it does not work true
    ----
     after that we use  :

     void ComponentDispatcher_ThreadPreprocessMessage(ref MSG msg, ref bool handled)
            {
                if (msg.message == 28)//WM_ACTIVATEAP
                    MessageBox.Show("I activated or Deactivated");
            }
    but up code does not execute , may this message"WM_ACTIVATEAP" can not be filtered!!
    ------------------------------
    but for each window if we register WndProc it works good :
     internal static void AddHook(System.Windows.Window window)
            {
                WindowInteropHelper windowInteropHelper = new WindowInteropHelper(window);
                //remark: for CodeGenerator windowInteropHelper.Handle is 0
                if (windowInteropHelper.Handle != (IntPtr)0)
                {
                    HwndSource source = HwndSource.FromHwnd(windowInteropHelper.Handle);
                    source.AddHook(new HwndSourceHook(WindowsInterop.WndProc));

                }
            }
        internal static IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
            {
                // Handle whatever Win32 message it is we feel like handling
                if (msg == 28 )
                    if(wParam.ToInt32() != 0)
                    MessageBox.Show("I Activated");
                    else
                    MessageBox.Show("I DeActivated");
                return IntPtr.Zero;
            }
    but according to this solution we have to register this message for each window!!
    -----------------------------
    Is there a way that we can trap "WM_ACTIVATEAP" for whole aplication?
    why ComponentDispatcher.ThreadPreprocessMessage does not work good?
    Is anothor way to filter message queue in WPF?

    ---------------
    And final question how about xbap project, can we filter message in IE is there any way?

    thanks you for attention.

    Wednesday, July 1, 2009 11:18 AM

Answers

  • Hello again

    we try this solution , may it works for window app and xbap app:
    (we test it a little and it works)

            public bool AppIsActive
            {
                get
                {
                    if (ActiveWindow == null)
                        return false;
                    IntPtr osActiveWindowHandler = WindowsInterop.GetForegroundWindow();
                    if (ActiveWindow.GetWindowHandle() == osActiveWindowHandler)
                        return true;
                    else if (WindowsInterop.IsWindowVisible(ActiveWindow.GetWindowHandle()) &&
                        WindowsInterop.IsChild(osActiveWindowHandler, ActiveWindow.GetWindowHandle()))
                        return true;
                    return false;
                }
            }
    
         public static Window ActiveWindow
            {
                get
                {
                    if (Application.Current != null)
                        foreach (Window window in Application.Current.Windows)
                            if (window.IsActive)
                                return window;
                    return null;
                }
    
            }
    //--------------------------
     public static class WindowExtension
        {
           private static readonly DependencyProperty WindowHandleProperty = DependencyProperty.Register(
              "WindowHandle", typeof(IntPtr), typeof(Window), new PropertyMetadata(IntPtr.Zero));
    
           public static IntPtr GetWindowHandle(this Window window)
            {
                if (((IntPtr)window.GetValue(WindowHandleProperty)) == IntPtr.Zero)
                    window.SetValue(WindowHandleProperty,
                        (new WindowInteropHelper(window)).Handle);
                if (((IntPtr)window.GetValue(WindowHandleProperty)) == IntPtr.Zero)
                    throw new Exception("Can not find window handle");
               return  (IntPtr)window.GetValue(WindowHandleProperty);
            }
        }
    thanks again
    Sunday, July 5, 2009 1:34 PM

All replies

  • Have you tried adding handlers for the Application.Activated and Application.Deactivated events?

    (note these won't work for XBAP apps)


    Mark


    Mark Salsbery Microsoft MVP - Visual C++
    Wednesday, July 1, 2009 8:44 PM
  • Thank you Mark for your reply
    we haven't try that
    it works good for Windows WPF and you said this does not work for xbap(we test).

    our application work in full trust mode xbap and same version in Windows WPF mode, according this we need a same solution in xbap.

    Can we use hook for filter windows message and trap WM_ACTIVATEAP?
    Our may use windows interop and find top most windows and   test that windows is for our application?
    Thursday, July 2, 2009 6:29 AM
  • Thank you Mark for your reply
    we haven't try that
    it works good for Windows WPF and you said this does not work for xbap(we test).

    our application work in full trust mode xbap and same version in Windows WPF mode, according this we need a same solution in xbap.

    Can we use hook for filter windows message and trap WM_ACTIVATEAP?
    Our may use windows interop and find top most windows and   test that windows is for our application?

    I have zero experience with XBAPs, but I suspect the activation messages go to the browser window(s).

    Maybe someone who knows will jump in and help :)


    Mark

    Mark Salsbery Microsoft MVP - Visual C++
    Thursday, July 2, 2009 4:52 PM
  • Hello again

    we try this solution , may it works for window app and xbap app:
    (we test it a little and it works)

            public bool AppIsActive
            {
                get
                {
                    if (ActiveWindow == null)
                        return false;
                    IntPtr osActiveWindowHandler = WindowsInterop.GetForegroundWindow();
                    if (ActiveWindow.GetWindowHandle() == osActiveWindowHandler)
                        return true;
                    else if (WindowsInterop.IsWindowVisible(ActiveWindow.GetWindowHandle()) &&
                        WindowsInterop.IsChild(osActiveWindowHandler, ActiveWindow.GetWindowHandle()))
                        return true;
                    return false;
                }
            }
    
         public static Window ActiveWindow
            {
                get
                {
                    if (Application.Current != null)
                        foreach (Window window in Application.Current.Windows)
                            if (window.IsActive)
                                return window;
                    return null;
                }
    
            }
    //--------------------------
     public static class WindowExtension
        {
           private static readonly DependencyProperty WindowHandleProperty = DependencyProperty.Register(
              "WindowHandle", typeof(IntPtr), typeof(Window), new PropertyMetadata(IntPtr.Zero));
    
           public static IntPtr GetWindowHandle(this Window window)
            {
                if (((IntPtr)window.GetValue(WindowHandleProperty)) == IntPtr.Zero)
                    window.SetValue(WindowHandleProperty,
                        (new WindowInteropHelper(window)).Handle);
                if (((IntPtr)window.GetValue(WindowHandleProperty)) == IntPtr.Zero)
                    throw new Exception("Can not find window handle");
               return  (IntPtr)window.GetValue(WindowHandleProperty);
            }
        }
    thanks again
    Sunday, July 5, 2009 1:34 PM