如何使用华廷与WebBrowser控件?如何使用、控件、WebBrowser

2023-09-02 21:53:20 作者:哥的生活叫逍遥°

有没有什么办法将允许我使用华廷的功能从我的网​​页浏览器的控制?只是我想用华廷与我的网页浏览器控制,我不希望我的应用程序打开一个新的窗口,我需要在我的Web浏览器控件。

Is there any way which would allow me to use watin's functionalities from my web browser control? simply i want to use watin with my web browser control i don't want my application to open a new window ,i need it in my web browser control.

推荐答案

如果您使用的是.NET WebBrowser控件,你可以使用下面的$ C $创建华廷的对象C:

If you are using .Net WebBrowser Control you can create WatiN's IE object by using following code:

var ie = new IE(webBrowser.ActiveXInstance);

但是,如果你这样做,在你的的Form_Load ActiveXInstance 将是无效的。如果你这样做,比如里面的某种 button_Click 的应用程序将挂起您使用后如。 ie.GoTo 。您需要启动新的线程和运行在那里。例如:

But if you do that inside your Form_Load ActiveXInstance will be null. And if you do that for example inside some kind of button_Click the application will hang after you use eg. ie.GoTo. You need to start new thread and operate there. For example:

private void Form1_Load(object sender, EventArgs e)
{
    var t = new Thread(() =>
    {
        Settings.AutoStartDialogWatcher = false;
        var ie = new IE(webBrowser1.ActiveXInstance);
        ie.GoTo("http://www.google.com");
    });
    t.SetApartmentState(ApartmentState.STA);
    t.Start();
}

您需要禁用对话框观察者的自动启动,因为你不能使用内置的华廷对话框守望者。但随着黑客,你可以创建自己的基于原始 DialogWatcher 类的一点点。处理弹出窗口和创建新的Web浏览器控件也是可能的。

You need to disable auto start of dialog watcher, because you cannot use built in WatiN dialog watcher. But with a little bit of hacking you can create your own based on original DialogWatcher class. Handling popups and creating new web browser controls are also possible.