日期:2014-05-18  浏览次数:20988 次

多选项卡浏览器,切换选项卡时,如何自动激活当前页面的滚动栏
最近帮客户 做了一个多选项卡的迷你浏览器。

但是发现一个问题,就是当你任意切换选项卡,当前载入的网站页面, 你滚动鼠标的滚轮,右边的滚动条是不会自动激活,没反应的。

这时候,你得点鼠标击页面的空白处 一下,然后 滚轮才能滚动 页面的滚动条。

这个用户体验是非常差的。 我们现在主流的浏览器,都是你切换任意 选项卡,载入的页面都是 立即支持 鼠标滚轮 控制 网页滚动条的。

这个要如何实现呢?

有些人说,你自己写一个 方法,切换页面后, 模拟鼠标 点击一下滚动条,就当是自动帮用户激活 滚轮功能,我认为这是不科学的。

看看大家有什么好主意?

------解决方案--------------------
C# code
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;
                }
            }
        }
    }
}