`
mqttb32q
  • 浏览: 14069 次
社区版块
存档分类
最新评论

使用Webbrowser的一点心得体会

 
阅读更多

使用Webbrowser的一点心得体会
2009年06月29日
         自从用上VS2008后,发现WebBrowser控件实在强大不少(.net 2003中不带),方便、实际、强大,有好多小工具就用这个写的,慢慢也有点体会了,总结一下,与网友们共享吧。
  1、如何获得“打开网页出错”信息
  
        在documentCompleted事件中,判断document.Url.AbsoluteUri中的"res://":标志即可(以前总用e.Url,怪不得总截取不到)
  if (webBrowser1.document.Url.AbsoluteUri.IndexOf("res://") > -1)      //出错处理
  {
  webBrowser1.Navigate(e.Url);
  return;
  }
  2、如何使用IHTMLdocument2等MSHTML功能
  
        VS2005中没有完全封装MSHTML中的功能,留了个Domdocument接口。直接引用Microsoft HTML Object Library类库后,就可以操作IHTMLdocument2等复杂的功能了。                如:IHTMLdocument2 doc2 = (IHTMLdocument2)webBrowser1.document.Domdocument;
  3、如何提取网页中的图片,尤其是验证码图等以流方式返回的图片
  很多网站一些图片是动态生成了,是从服务器计算4个代码,然后相对应一组序号,再以流方式发送到本地,然后GDI+绘画出来。不管是以什么方式,到了客户端,都是完整的。用WebBrowser的好处就在这里,只要管住最终结果就OK了。以下是得到网页上验证码的代码:
  ///
  /// 返回指定WebBrowser中图片中的图内容
  ///
  /// WebBrowser控件
  /// IMG元素
  /// IMG对象
  代码:
  private Image GetWebImage(WebBrowser WebCtl, HtmlElement ImgeTag)
  {
  HTMLdocument doc = (HTMLdocument)WebCtl.document.Domdocument;
  HTMLBody body = (HTMLBody)doc.body;
  IHTMLControlRange rang = (IHTMLControlRange)body.createControlRange();
  IHTMLControlElement Img = (IHTMLControlElement)ImgeTag.DomElement; //图片地址
  Image oldImage = Clipboard.GetImage();
  rang.add(Img);
  rang.execCommand("Copy", false, null);  //拷贝到内存
  Image numImage = Clipboard.GetImage();  //从 Clipboard中取图
  Clipboard.SetImage(oldImage);     //还原
  return numImage;
  }
  VB.net2008代码为:
  补充一下:
  1、引用Microsoft HTML Object Library类库
  2、添加一个WebBrowser控件、一个PictureBox控件
  开始代码:
  Imports mshtml
  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  Me.WebBrowser1.Navigate("http://ui.ptlogin2.qq.com/cgi-bin/login?link_target=blank&target=self&appid=8000108&hide_title_bar=1&s_url=http%3A%2F%2Fimgcache.qq.com%2Fqzone%2Fv5%2Floginsucc.html%3fpara%3dizone&f_url=loginerroralert")
  '呵呵,QQ空间的登录页面就是这么地长
  End Sub
  Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
  ’WebBrowser1加载完后触发事件
  PictureBox1.Image = GetWebImage(WebBrowser1)
  End Sub
  Private Function GetWebImage(ByVal WebCtl As WebBrowser) As Image
  Dim doc As HTMLDocument = DirectCast(WebCtl.Document.DomDocument, HTMLDocument)
  Dim body As HTMLBody = DirectCast(doc.body, HTMLBody)
  Dim rang As IHTMLControlRange = DirectCast(body.createControlRange, IHTMLControlRange)
  Dim ImgeTag As HtmlElement
  For Each ImgeTag In WebBrowser1.Document.All
  If ImgeTag.Id = "imgVerify" Then
  Exit For
  End If
  Next
  Dim Img As IHTMLControlElement = DirectCast(ImgeTag.DomElement, IHTMLControlElement)
  Dim old As Object = Clipboard.GetDataObject
  rang.add(Img)
  rang.execCommand("Copy", False, Nothing)
  Dim numImage As Image = Clipboard.GetImage
  If (old Is Nothing) = False Then
  Clipboard.SetDataObject(old)
  End If
  Return numImage
  End Function
  4、如何屏蔽掉Alert()类型的弹出窗口
  
        首先申明这不是技术,只是一种处理的技巧。网上查了很多资料,对于网页中弹出Alert()窗口不好屏蔽(尤其是Writer出来的)。我的方法是做两个EXE,一个为主程序.exe,一个Run.exe。WebBrowser控件放在RUN.exe中,在主程序中通过Process调用RUN.exe,而用在执行完任务后,将RUN.exe杀掉(Kill),这时Alert窗口会自动关闭。我用这种技巧做了好了个投票机,可以安静的运行,还能回避Session处理等问题,也不会在桌面上留下一堆窗口。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics