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

No.of Views2193
Bookmarked0 times
Downloads 
Votes0
By  Prabu   On  27 Jul 2010 09:07:43
Tag : WPF , Miscellaneous
In Windows based WPF application, there is an option to close the whole application using (Application.Current.ShutDown) statement
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

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.

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);

 That''s it. just copy and use in your project.

 
Sign Up to vote for this article
 
About Author
 
Prabu
Occupation-Software Engineer
Company-
Member Type-Fresh
Location-India
Joined date-23 Jun 2010
Home Page-
Blog Page-http://prabu-guru.blogspot.com/
 
 
Other popularSectionarticles
Comments
By:Christoph IngenhaagDate Of Posted:8/30/2011 4:15:29 AM
The same in VB.NET
Private Declare Function GetAncestor Lib "user32" Alias "GetAncestor" (ByVal hwnd As IntPtr, ByVal flags As Int32) As IntPtr Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As IntPtr, ByVal msg As Int32, ByVal wparam As IntPtr, ByVal lparam As IntPtr) As Boolean Private Sub CloseWindow() Dim wih As New WindowInteropHelper(Application.Current.MainWindow) wih.EnsureHandle() SendMessage(GetAncestor(wih.Handle, 2), &H10, IntPtr.Zero, IntPtr.Zero) End Sub
By:JgarretsDate Of Posted:1/11/2011 10:58:34 AM
You da Man!
Works great. Just what I needed!
By:Miral ShahDate Of Posted:12/17/2010 1:50:13 AM
It works as Charm
Above piece of code works as charm...... Thanks for the same..... Highly oblige Miral Shah
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top