Search code examples
c#xbap

How can I close the browser from an XBAP?


I am using an XBAP application in full trust. I need to close the browser hosting the XBAP when I click a button. How can I achieve this? Application.Currenty.ShutDown() only closes the application, leaving the browser blank.


Solution

  • EDIT: My mistake, here is a thread with your problem - http://social.msdn.microsoft.com/forums/en-US/wpf/thread/21c88fed-c84c-47c1-9012-7c76972e8c1c

    and to be more specific (this code needs full trust security settings)

    using System.Windows.Interop;
    using System.Runtime.InteropServices;
    
    [DllImport("user32", ExactSpelling = true, CharSet = CharSet.Auto)]
    private static extern IntPtr GetAncestor(IntPtr hwnd, int flags);
    
    [DllImport("user32", CharSet = CharSet.Auto)]
    private static extern bool PostMessage(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam);
    
    private void button1_Click(object sender, RoutedEventArgs e)
    {
           WindowInteropHelper wih = new WindowInteropHelper(Application.Current.MainWindow);
           IntPtr ieHwnd = GetAncestor(wih.Handle, 2);
           PostMessage(ieHwnd, 0x10, IntPtr.Zero, IntPtr.Zero);      
    }