日期:2014-05-18 浏览次数:21213 次
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            webBrowser1.Navigate("http://www.qq.com");
            webBrowser2.Navigate("http://www.sina.com.cn");
        }
        [DllImport("user32.dll", SetLastError = true)]
        static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
        [DllImport("user32.dll")]
        static extern IntPtr SetFocus(IntPtr hWnd);
        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {
            TabPage tabPage = tabControl1.TabPages[tabControl1.SelectedIndex];
            foreach(Control c in tabPage.Controls)
            {
                WebBrowser web = c as WebBrowser;
                if (web != null)
                {
                    IntPtr h = FindWindowEx(web.Handle, IntPtr.Zero, "Shell Embedding", "");
                    if (h != IntPtr.Zero)
                    {
                        h = FindWindowEx(h, IntPtr.Zero, "Shell DocObject View", "");
                        if (h != IntPtr.Zero)
                        {
                            h = FindWindowEx(h, IntPtr.Zero, "Internet Explorer_Server", "");
                            if (h != IntPtr.Zero)
                            {
                                SetFocus(h);
                            }
                        }
                    }
                    break;
                }
            }
        }
    }
}