How to close Tab of IE running XBAP Application, not the whole browser?

Answered How to close Tab of IE running XBAP Application, not the whole browser?

  • Tuesday, March 29, 2011 8:14 AM
     
      Has Code

    Hi All,

    Using the following code, the entire IE browser gets closed and if any other tabs are open, it asks for confirmation.

     

        private void button1_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);
    

    Is there any way to close only the Tab which is running XBAP Application in IE? Any help will be greatly appreciated.

     

    Regards,

    Binaya

     

All Replies

  • Thursday, March 31, 2011 7:00 AM
    Moderator
     
     Answered

    Yes, use the above solution only can close the IE browser. Please refer to this thread: http://social.msdn.microsoft.com/Forums/en-US/ieextensiondevelopment/thread/03a8c835-e9e4-405b-8345-6c3d36bc8941 you could get the Tab framefrom IE by Accessibility class. And please download my sample from here:
    http://cid-51b2fdd068799d15.office.live.com/self.aspx/.Public/Samples%5E_2011/20110331%5E_XBAPCloseCurrentIETab.zip

     

    Sincerely,


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

  • Monday, April 18, 2011 7:14 AM
     
     

    Thanks Bob for the helpful link.

    I tried the Accessibility class. But, it's not working for IE9. Can you help me how to deal the problem for IE9? While closing the application, accessible value is always null and throws the exception.

    Thank you for the help.

     

    Regards,

    Binaya

  • Tuesday, April 19, 2011 6:30 AM
    Moderator
     
     Answered Has Code

    Hi Binaya,

    Since IE changes the UI, and the "TabBandClass" is not under the "CommandBarClass", for IE 9, it is under the "NavigationBar - WorkerW". Please view the diagram from Spy++:

    So for IE 9, you just should change the code:

      private IntPtr GetDirectUIHWND(IntPtr ieFrame)
      {
       // For IE 9:
       var directUI = FindWindowEx(ieFrame, IntPtr.Zero, "WorkerW", null);
    
       // For IE 8:
       //var directUI = FindWindowEx(ieFrame, IntPtr.Zero, "CommandBarClass", "Navigation Bar");
       directUI = FindWindowEx(directUI, IntPtr.Zero, "ReBarWindow32", null);
       directUI = FindWindowEx(directUI, IntPtr.Zero, "TabBandClass", null);
       directUI = FindWindowEx(directUI, IntPtr.Zero, "DirectUIHWND", null);
       return directUI;
    
      }
    

     

    or below can work on both:

      private IntPtr GetDirectUIHWND(IntPtr ieFrame)
      {
       // For IE 8:
       var directUI = FindWindowEx(ieFrame, IntPtr.Zero, "CommandBarClass", null);
       directUI = FindWindowEx(directUI, IntPtr.Zero, "ReBarWindow32", null);
       directUI = FindWindowEx(directUI, IntPtr.Zero, "TabBandClass", null);
       directUI = FindWindowEx(directUI, IntPtr.Zero, "DirectUIHWND", null);
    
       if (directUI == IntPtr.Zero)
       {
        // For IE 9:
        directUI = FindWindowEx(ieFrame, IntPtr.Zero, "WorkerW", "Navigation Bar");
        directUI = FindWindowEx(directUI, IntPtr.Zero, "ReBarWindow32", null);
        directUI = FindWindowEx(directUI, IntPtr.Zero, "TabBandClass", null);
        directUI = FindWindowEx(directUI, IntPtr.Zero, "DirectUIHWND", null);
       }
    
       return directUI;
      }
    

     

    Sincerely,


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

  • Tuesday, April 19, 2011 8:29 AM
     
     

    Hi Bob,

    Thank you for your support. I got the clear view from the diagram too. And it really works now. Thanks.

     

    Regards,

    Binaya

  • Wednesday, November 16, 2011 12:06 AM
     
     

    Hi Bob,

    Wanted to say thanks, this helped me out tremendously!

    Best,
    Dale

  • Monday, January 30, 2012 11:09 AM
     
     

    Hi Bob,

    I have downloaded and tested your sample, it closes the two IE tabs at the same time.

    While I am running this sample application I have five IE tabs opened. When I click the Close button, it closes the XBAP page hosted tab and 3rd tab from the IE. Please let me know the solution for this issue.

    Thanks,

     

     


    Prabu (http://prabu-guru.blogspot.com/)
  • Monday, January 30, 2012 12:26 PM
    Moderator
     
     
    What is your OS and IE version? Could you please tell me what is the content of the 3rd tab? Or just an empty page? And could you please try to use SPY++ to check the WIN32 Handles of your IE? Thanks.

     


    Bob Bao [MSFT]
    MSDN Community Support | Feedback to us