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

    Question

  • 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

     

    Tuesday, March 29, 2011 8:14 AM

Answers

  • 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.

    Thursday, March 31, 2011 7:00 AM
  • 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 6:30 AM

All replies

  • 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.

    Thursday, March 31, 2011 7:00 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

    Monday, April 18, 2011 7:14 AM
  • 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 6:30 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

    Tuesday, April 19, 2011 8:29 AM
  • Hi Bob,

    Wanted to say thanks, this helped me out tremendously!

    Best,
    Dale

    Wednesday, November 16, 2011 12:06 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 11:09 AM
  • 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
    Monday, January 30, 2012 12:26 PM
  • Hello Bob_Bao! You're the man! Just what i needed, almost...

    Can you tell me if this is working on IE 10? I'm getting an error i tried with the code for de IE9 that was no good.

    The error:

    I'm using IE10 on Win7x64

    Thanks!


    -- Jorge_M_P

    Monday, August 05, 2013 10:34 AM
  • I tested my code in Win 7 x64 and IE 10, it work. Do not forget to download my sample and update the code in GetDirectUIHWND method to below:

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

    before testing with IE9 and 10.


    Bob Bao

    Do you still use the same Windows 8 LockScreen always? Download Chameleon Win8 App quickly, that changes your LockScreen constantly.
    你是否还在看着一成不变的Windows 8锁屏而烦恼,赶紧下载这个 百变锁屏 应用,让你的锁屏不断地变化起来。

    Tuesday, August 06, 2013 2:55 AM
  • Hello bob, sorry about my stubbornness but i did that and replaced the code, it did not worked...

    i'll try to find another way...


    -- Jorge_M_P

    Tuesday, August 06, 2013 9:04 AM