文档库 最新最全的文档下载
当前位置:文档库 › WebBrowser控件使用技巧

WebBrowser控件使用技巧

WebBrowser控件使用技巧

首先分享一个WebBrowser的扩展类(此类所需的dll将在文章末尾提供下载),大家最好都使用这个类来替代.net框架中的WebBrowser类,它提供了两个扩展功能:

1.屏蔽错误脚本提示。修正了WebBrowser控件本身屏蔽错误不全的问题,由启明提出,原文:https://www.wendangku.net/doc/15945584.html,/hobe/archive/2007/01/14/619906.html

2.扩展NewWindow事件。修正了WebBrowser控件本身的NewWindow事件不提供新窗口Url的问题,通过新增的BeforeNewWindow事件予以支持,由佳文转载并整理,原文:https://www.wendangku.net/doc/15945584.html,/yjwgood/archive/2009/02/09/1386789.html

整合后的代码如下:

public class ExWebBrowser : System.Windows.Forms.WebBrowser
{
private SHDocVw.IWebBrowser2 Iwb2;
protected override void AttachInterfaces(object nativeActiveXObject)
{
Iwb2 = (SHDocVw.IWebBrowser2)nativeActiveXObject;
Iwb2.Silent = true;
base.AttachInterfaces(nativeActiveXObject);
}
protected override void DetachInterfaces()
{
Iwb2 = null;
base.DetachInterfaces();
}
System.Windows.Forms.AxHost.ConnectionPointCookie cookie;
WebBrowserExtendedEvents events;
//This method will be called to give you a chance to create your own event sink
protected override void CreateSink()
{
//MAKE SURE TO CALL THE BASE or the normal events won't fire
base.CreateSink();
events = new WebBrowserExtendedEvents(this);
cookie = new System.Windows.Forms.AxHost.ConnectionPointCookie(this.ActiveXInstance, events, typeof(DWebBrowserEvents2));
}
protected override void DetachSink()
{
if (null != cookie)
{
cookie.Disconnect();
cookie = null;
}
base.DetachSink();
}
//This new event will fire when the page is navigating
public event EventHandler BeforeNavigate;
///


/// 可用于替代原来的NewWindow事件,新增了事件的Url参数支持。
///

[CategoryAttribute("操作"), DescriptionAttribute("经过扩展的NewWindow事件,使用继承后的WebBrowserExtendedNavigatingEventArgs类型参数实现Url参数支持")]
public event EventHandler BeforeNewWindow;
protected void OnBeforeNewWindow(string url, out bool cancel)
{
EventHandler h = BeforeNewWindow;
WebBrowserExtendedNavigatingEventArgs args = new WebBrowserExtendedNavigatingEventArgs(url, null);

if (null != h)
{
h(this, args);
}
cancel = args.Cancel;
}
protected void OnBeforeNavigate(string url, string frame, out bool cancel)
{
EventHandler h = BeforeNavigate;
WebBrowserExtendedNavigatingEventArgs args = new WebBrowserExtendedNavigatingEventArgs(url, frame);
if (null != h)
{
h(this, args);
}
//Pass the cancellation chosen back out to the events
cancel = args.Cancel;
}
//This class will capture events from the WebBrowser
class WebBrowserExtendedEvents : System.Runtime.InteropServices.StandardOleMarshalObject, DWebBrowserEvents2
{
ExWebBrowser _Browser;
public WebBrowserExtendedEvents(ExWebBrowser browser) { _Browser = browser; }
//Implement whichever events you wish
public void BeforeNavigate2(object pDisp, ref object URL, ref object flags, ref object targetFrameName, ref object postData, ref object headers, ref bool cancel)
{
_Browser.OnBeforeNavigate((string)URL, (string)targetFrameName, out cancel);
}
public void NewWindow3(object pDisp, ref bool cancel, ref object flags, ref object URLContext, ref object URL)
{
_Browser.OnBeforeNewWindow((string)URL, out cancel);
}
}
[https://www.wendangku.net/doc/15945584.html,Import(), System.Runtime.InteropServices.Guid("34A715A0-6587-11D0-924A-0020AFC7AC4D"),
System.Runtime.InteropServices.InterfaceTypeAttribute(https://www.wendangku.net/doc/15945584.html,InterfaceType.InterfaceIsIDispatch),
System.Runtime.InteropServices.TypeLibType(System.Runtime.InteropServices.TypeLibTypeFlags.FHidden)]
public interface DWebBrowserEvents2
{
[System.Runtime.InteropServices.DispId(250)]
void BeforeNavigate2(
[System.Runtime.InteropServices.In,
System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.IDispatch)] object pDisp,
[System.Runtime.InteropServices.In] ref object URL,
[System.Runtime.InteropServices.In] ref object flags,
[System.Runtime.InteropServices.In] ref object targetFrameName, [System.Runtime.InteropServices.In] ref object postData,
[System.Runtime.InteropServices.In] ref object headers,

[System.Runtime.InteropServices.In,
System.Runtime.InteropServices.Out] ref bool cancel);
[System.Runtime.InteropServices.DispId(273)]
void NewWindow3(
[System.Runtime.InteropServices.In,
System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.IDispatch)] object pDisp,
[System.Runtime.InteropServices.In, System.Runtime.InteropServices.Out] ref bool cancel,
[System.Runtime.InteropServices.In] ref object flags,
[System.Runtime.InteropServices.In] ref object URLContext,
[System.Runtime.InteropServices.In] ref object URL);
}
}
public class WebBrowserExtendedNavigatingEventArgs : CancelEventArgs
{
private string _Url;
public string Url
{
get { return _Url; }
}
private string _Frame;
public string Frame
{
get { return _Frame; }
}
public WebBrowserExtendedNavigatingEventArgs(string url, string frame)
: base()
{
_Url = url;
_Frame = frame;
}
}




技巧1:在当前窗口内打开目标为新窗口的超链接

通过上述的扩展类支持得以实现,增加BeforeNewWindow事件的处理函数以进行处理:

void WebBrowser1_BeforeNewWindow(object sender, EventArgs e)
{
WebBrowserExtendedNavigatingEventArgs eventArgs = e as WebBrowserExtendedNavigatingEventArgs;
if (eventArgs.Url.ToLower() != "about:blank")
webBrowser1.Navigate(eventArgs.Url);
eventArgs.Cancel = true;
}

这种方法的弊病在于可能会错误地转向到网站的弹窗广告,为了规避此问题,可以强制取消一切弹出窗口,采取另一种方法实现当前窗口内打开新窗口超链接,增加DocumentCompleted事件的处理函数以进行处理:

void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (webBrowser1.ReadyState > WebBrowserReadyState.Interactive)
{
foreach (HtmlElement f in webBrowser1.Document.Links)
{
var s = f.GetAttribute("target");
if (s != null && s.ToLower() == "_blank") f.SetAttribute("target", "_self");
}
}
}

此方法将遍历所有元素,修改其目标为当前窗口,但是此方法又会引发新的问题,即如果页面中某些元素长时间都未加载完成时,此事件将迟迟不会被引发,也就是说用户必须要等到页面完完全

全加载完毕之后才可能在当前窗口内打开新窗口超链接。



根据一些人的经验,DocumentCompleted事件会在每次加载网页的过程中触发两次,第一次触发时WebBrowser控件的ReadyState属性应为Interactive,第二次则为Complete,根据注释来看,Interactive应该是代表页面加载初步完成,已具有基本交互能力的状态,这时应当是理想的编辑状态,但我尝试将代码中的if (webBrowser1.ReadyState > WebBrowserReadyState.Interactive)修改为if (webBrowser1.ReadyState >= WebBrowserReadyState.Interactive),并没有什么明显效果,页面上的超链接还是要等待全部加载之后才会被修改。

为此我还尝试过在Navigated事件中进行处理,也不起作用。希望高人能对此给出完美的解决方案。

技巧2:获取状态栏信息

增加StatusTextChanged事件处理函数进行处理:

void webBrowser1_StatusTextChanged(object sender, EventArgs e)
{
label1.Text = webBrowser1.StatusText;
}

技巧3:页面转向后改变地址栏地址

在Navigated事件处理函数中改变地址栏地址是最恰当的:

private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
textBox1.Text = webBrowser1.Url.ToString();
}

技巧4:正确设置单选框的方法

建议使用执行单击事件的方式来设置单选框,而不是修改属性:

webBrowser1.Document.GetElementById("RBT_A").InvokeMember("click");

技巧5:正确设置联动型下拉列表的方法

比较常见的联动型多级下拉列表就是省/市县选择了,这种情况下直接设置选择项的属性不会触发联动,需要在最后执行触发事件函数才能正常工作:


foreach (HtmlElement f in s.GetElementsByTagName("option"))
{
if (f.InnerText == "北京")
{
f.SetAttribute("selected", "selected");
}
else
{
f.SetAttribute("selected", "");
}
}
s.RaiseEvent("onchange");

此方法来源于:https://www.wendangku.net/doc/15945584.html,/u/20070309/11/aef46651-a15a-4777-b832-e71b09a7b9e0.html

技巧6:使用延迟等待服务器交互

有时会遇到联动型下拉列表需要同服务器交互的情况,如果只在一个函数里连续进行设置,往往会失败,因为代码执行速度很快,这期间页面还没有从服务器得到并装载数据。

这时候应当通过使用Timer等方法设置延迟间隔,再进行更改,需注意的是,不应当使用Sleep方法停止当前线程的执行以求达到延迟目的,因为WebBrowser控件也处于当前线程内,Sleep会同时暂停WebBrowser控件的运作。

还有一点需要注意,就是如果程序内用到多个Timer的话,有可能引发不可预料的错乱

尚未解决的问题

 

 有一个问题一直困扰我,始终也没找到相关的资料:

我现在可以通过WebBrowser实现对各种Html元素的操控,唯独无法控制Html的上传控件,即:



应当是出于安全原因,JS代码无法访问和设置此控件所选择的文件路径,这是符合情理的,但是WebBrowser中也没能找到相关的支持,这样就无法实现自动上传等功能,希望有高手能指出解决办法。


还有一个似乎是无解的问题,就是读取和操作页面内的框架页或内嵌页的问题,很多人发出疑问,但始终没找到解决方法,此方面最典型的应用就是自动点嵌入式广告功能了,而现在不但无法点击,甚至都无法获取框架页的代码等信息。

文章来源:https://www.wendangku.net/doc/15945584.html,/


相关文档
相关文档 最新文档