.NET Framework Developer Center > .NET Development Forums > Windows Presentation Foundation (WPF) > WPF in "Kiosk" mode - how to get back application focus when it lost it?
Ask a questionAsk a question
 

AnswerWPF in "Kiosk" mode - how to get back application focus when it lost it?

  • Saturday, November 07, 2009 8:24 AMMatjaž Kofol Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Hello,

    We are using WPF aplication in "Kiosk" mode like this:
    Window w = new Window();
    w.WindowStyle = WindowStyle.None;
    w.WindowState = WindowState.Maximized;
    w.TopMost = true;
    w.ResizeMode=NoResize;
    w.Show();
    
    Sometimes taskbar shows (look here for this invalid state ), i.e. when system yellow info balloon shows near the clock - no network connection, system info...etc., and our application lost focus.
    How we can get our application focus when it lost it automatically but not to compromise keyboard focus?

    we have a barcode scanner so when it scans our application textbox should show the scanned value no matter if application is in focus or not.

    Regards
    Matjaž

Answers

  • Monday, November 09, 2009 10:43 AMMatjaž Kofol Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    w.Focus() didn't helped us but this helped (using Interop):

    //Interop.cs
    using System.Runtime.InteropServices;
    using System.Windows.Interop;
    
    public class Interop
    {
            [DllImport("user32.dll")]
            public static extern bool SetForegroundWindow(IntPtr hWnd);
    
            [DllImport("user32.dll")]
            public static extern IntPtr GetForegroundWindow();
    
            public static IntPtr GetWindowHandle(Window window)
            {
                return new WindowInteropHelper(window).Handle;
            }
    }
    
    //Somewhere in main window
    IntPtr window = Interop.GetWindowHandle(this);
    IntPtr focused = Interop.GetForegroundWindow();
    if (window != focused)
    {
        Interop.SetForegroundWindow(window);
    }
    


    So it is solved.

    Regards
    Matjaž

All Replies

  • Saturday, November 07, 2009 8:40 AMWilliam Han - MSFT Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I believe you can call UIElement.Focus() (on any event handler) to set focus on an UIElement (including Window, in your case, perhaps w.Focus()) and it makes the window actived.

    William
  • Monday, November 09, 2009 10:43 AMMatjaž Kofol Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    w.Focus() didn't helped us but this helped (using Interop):

    //Interop.cs
    using System.Runtime.InteropServices;
    using System.Windows.Interop;
    
    public class Interop
    {
            [DllImport("user32.dll")]
            public static extern bool SetForegroundWindow(IntPtr hWnd);
    
            [DllImport("user32.dll")]
            public static extern IntPtr GetForegroundWindow();
    
            public static IntPtr GetWindowHandle(Window window)
            {
                return new WindowInteropHelper(window).Handle;
            }
    }
    
    //Somewhere in main window
    IntPtr window = Interop.GetWindowHandle(this);
    IntPtr focused = Interop.GetForegroundWindow();
    if (window != focused)
    {
        Interop.SetForegroundWindow(window);
    }
    


    So it is solved.

    Regards
    Matjaž