Tuesday, May 4, 2010

How to close the whole application in WPF Browser based application (XBAP application)


In Windows based WPF application, there is an option to close the whole application using (Application.Current.ShutDown) statement. But the same statement is not working in Web based WPF application (perhaps it will close the application. But not close the IE browser – It showing with the blank inactive tab).  We have an option to close the application along with the IE window by using the following code,

    private void buttonClose_Click(object sender, RoutedEventArgs e)
    {
        WindowInteropHelper wih = new WindowInteropHelper(Application.Current.MainWindow);
        IntPtr ieHwnd = GetAncestor(wih.Handle, 2 /*GA_ROOT*/);
        PostMessage(ieHwnd, 0x10/*WM_CLOSE*/, IntPtr.Zero, IntPtr.Zero);
    }

    [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);
Post a Comment