开发者社区> 杰克.陈> 正文
阿里云
为了无法计算的价值
打开APP
阿里云APP内打开

C#中实现WebBrowser控件的HTML源代码读写

简介: 原文: C#中实现WebBrowser控件的HTML源代码读写 C#中实现WebBrowser控件的HTML源代码读写http://www.blogcn.com/user8/flier_lu/index.
+关注继续查看
原文: C#中实现WebBrowser控件的HTML源代码读写

C#中实现WebBrowser控件的HTML源代码读写

http://www.blogcn.com/user8/flier_lu/index.html?id=1125200&run=.0D9CAA6

趁周末想折腾一下嵌入ASP.NET的WinForm程序
 需要用到WebBrowser控件的HTML源码读写
 就把以前的一些代码片断移值到C#下
 顺便发个帖子备忘,呵呵
  
 思路其实很简单,直接通过document.documentElement.outerHTML
 或者使用IPersistStreamInit接口直接对流进行处理
 前者我就不废话了,后者实现方法如下
  
 首先是写入HTML到已初始化的WebBrowser控件
 初始化可以通过Navigate("about:blank")完成
 必须确保WebBrowser.Document != null
 否则应该推迟到DocumentComplete事件再读写
  
 

img_a6339ee3e57d1d52bc7d02b338e15a60.gifUCOMIStream stream = null;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
img_a6339ee3e57d1d52bc7d02b338e15a60.gif CreateStreamOnHGlobal(Marshal.StringToHGlobalUni(value), 
trueout stream);
img_a6339ee3e57d1d52bc7d02b338e15a60.gif   
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
img_a6339ee3e57d1d52bc7d02b338e15a60.gif 
if(stream != null)
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
img_405b18b4b6584ae338e0f6ecaf736533.gifimg_1c53668bcee393edac0d7b3b3daff1ae.gif 
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif   IPersistStreamInit persistentStreamInit 
=
img_33d02437d135341f0800e3d415312ae8.gif     (IPersistStreamInit)WebBrowser.Document;
img_33d02437d135341f0800e3d415312ae8.gif  
img_33d02437d135341f0800e3d415312ae8.gif   persistentStreamInit.InitNew();
img_33d02437d135341f0800e3d415312ae8.gif   persistentStreamInit.Load(stream);
img_33d02437d135341f0800e3d415312ae8.gif   persistentStreamInit 
= null;
img_05dd8d549cff04457a6366b0a7c9352a.gif }

img_a6339ee3e57d1d52bc7d02b338e15a60.gif


  
 UCOMIStream是COM中IStream的CLR版本
 CreateStreamOnHGlobal函数从一个字符串的地址
 创建一个IStream供使用
  

img_a6339ee3e57d1d52bc7d02b338e15a60.gif [DllImport("ole32.dll", PreserveSig=false)]
img_a6339ee3e57d1d52bc7d02b338e15a60.gif 
static extern void CreateStreamOnHGlobal(IntPtr hGlobal,
img_a6339ee3e57d1d52bc7d02b338e15a60.gif   Boolean fDeleteOnRelease, [Out] 
out UCOMIStream pStream);


  
 然后就是通过IPersistStreamInit接口初始化并载入HTML源码,
 IPersistStreamInit接口CLR缺省没有导入,定义如下
  

img_a6339ee3e57d1d52bc7d02b338e15a60.gif [ComVisible(true), ComImport(), Guid("7FD52380-4E07-101B-AE2D-08002B2EC713"),
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
img_a6339ee3e57d1d52bc7d02b338e15a60.gif 
public interface IPersistStreamInit
img_405b18b4b6584ae338e0f6ecaf736533.gifimg_1c53668bcee393edac0d7b3b3daff1ae.gif 
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif   
void GetClassID([In, Out] ref Guid pClassID);
img_33d02437d135341f0800e3d415312ae8.gif  
img_33d02437d135341f0800e3d415312ae8.gif   [
return: MarshalAs(UnmanagedType.I4)] [PreserveSig]
img_33d02437d135341f0800e3d415312ae8.gif   
int IsDirty();
img_33d02437d135341f0800e3d415312ae8.gif  
img_33d02437d135341f0800e3d415312ae8.gif   
void Load([In, MarshalAs(UnmanagedType.Interface)] UCOMIStream pstm);
img_33d02437d135341f0800e3d415312ae8.gif   
void Save([In, MarshalAs(UnmanagedType.Interface)] UCOMIStream pstm,
img_33d02437d135341f0800e3d415312ae8.gif             [In, MarshalAs(UnmanagedType.I4)] 
int fClearDirty);
img_33d02437d135341f0800e3d415312ae8.gif   
void GetSizeMax([Out, MarshalAs(UnmanagedType.LPArray)] long pcbSize);
img_33d02437d135341f0800e3d415312ae8.gif   
void InitNew();
img_05dd8d549cff04457a6366b0a7c9352a.gif }


  
 读取HTML也是类似思路,将HTML源码写到一个IStream中
 然后转换成字符串供C#代码使用,不过实现方式比较麻烦
  
 比较简单的方法还是使用ole32.dll提供的函数
 重建流,但这需要预先设定流的长度,如
  

img_a6339ee3e57d1d52bc7d02b338e15a60.gifUCOMIStream stream = null;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
img_a6339ee3e57d1d52bc7d02b338e15a60.gif CreateStreamOnHGlobal(Marshal.AllocHGlobal(
4096), trueout stream);
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
img_a6339ee3e57d1d52bc7d02b338e15a60.gif IPersistStreamInit persistentStreamInit 
=
img_a6339ee3e57d1d52bc7d02b338e15a60.gif   (IPersistStreamInit)WebBrowser.Document;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
img_a6339ee3e57d1d52bc7d02b338e15a60.gif persistentStreamInit.Save(stream, 
0);
img_a6339ee3e57d1d52bc7d02b338e15a60.gif persistentStreamInit 
= null;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
img_a6339ee3e57d1d52bc7d02b338e15a60.gif IntPtr pStr;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
img_a6339ee3e57d1d52bc7d02b338e15a60.gif GetHGlobalFromStream(stream, 
out pStr);
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
img_a6339ee3e57d1d52bc7d02b338e15a60.gif 
return Marshal.PtrToStringAnsi(pStr);


  
 然后使用GetHGlobalFromStream函数和
 Marshal.PtrToStringAnsi将流转换为字符串
 另外一种方法是自行实现一个支持IStream接口的类
 通过流的方式灵活完成读取操作,我比较喜欢这种 

img_a6339ee3e57d1d52bc7d02b338e15a60.gif using(MemoryStream stream = new MemoryStream())
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
img_405b18b4b6584ae338e0f6ecaf736533.gifimg_1c53668bcee393edac0d7b3b3daff1ae.gif 
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif   ComStreamAdapter adapter 
= new ComStreamAdapter(stream);
img_33d02437d135341f0800e3d415312ae8.gif  
img_33d02437d135341f0800e3d415312ae8.gif   IPersistStreamInit persistentStreamInit 
=
img_33d02437d135341f0800e3d415312ae8.gif     (IPersistStreamInit)WebBrowser.Document;
img_33d02437d135341f0800e3d415312ae8.gif  
img_33d02437d135341f0800e3d415312ae8.gif   persistentStreamInit.Save(adapter, 
0);
img_33d02437d135341f0800e3d415312ae8.gif  
img_33d02437d135341f0800e3d415312ae8.gif   stream.Seek(
0, SeekOrigin.Begin);
img_33d02437d135341f0800e3d415312ae8.gif  
img_33d02437d135341f0800e3d415312ae8.gif   
using(StreamReader reader = new StreamReader(stream))
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif   
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif     
return reader.ReadToEnd();
img_105a1e124122b2abcee4ea8e9f5108f3.gif   }

img_05dd8d549cff04457a6366b0a7c9352a.gif }

img_a6339ee3e57d1d52bc7d02b338e15a60.gif


  
 这里的ComStreamAdapter是一个使用了adapter模式的类
 将普通的System.IO.Stream转换为IStream支持的类
  
   

img_a6339ee3e57d1d52bc7d02b338e15a60.gif  public class ComStreamAdapter : UCOMIStream
img_405b18b4b6584ae338e0f6ecaf736533.gifimg_1c53668bcee393edac0d7b3b3daff1ae.gif     
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif       
private Stream _stream;
img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif  
img_33d02437d135341f0800e3d415312ae8.gif       
public ComStreamAdapter(Stream stream)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif       
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif         _stream 
= stream;
img_105a1e124122b2abcee4ea8e9f5108f3.gif       }

img_33d02437d135341f0800e3d415312ae8.gif  
img_7a2b9a960ee9a98bfd25d306d55009f8.gifimg_2887d91d0594ef8793c1db92b8a1d545.gif       
UCOMIStream Members#region UCOMIStream Members
img_33d02437d135341f0800e3d415312ae8.gif  
img_33d02437d135341f0800e3d415312ae8.gif       
public void Commit(int grfCommitFlags)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif       
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_105a1e124122b2abcee4ea8e9f5108f3.gif       }

img_33d02437d135341f0800e3d415312ae8.gif  
img_33d02437d135341f0800e3d415312ae8.gif       
public void Clone(out UCOMIStream ppstm)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif       
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif         ppstm 
= null;
img_105a1e124122b2abcee4ea8e9f5108f3.gif       }

img_33d02437d135341f0800e3d415312ae8.gif  
img_33d02437d135341f0800e3d415312ae8.gif       
public void CopyTo(UCOMIStream pstm, long cb, System.IntPtr pcbRead, Syste
img_33d02437d135341f0800e3d415312ae8.gif m.IntPtr pcbWritten)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif       
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_105a1e124122b2abcee4ea8e9f5108f3.gif       }

img_33d02437d135341f0800e3d415312ae8.gif  
img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif       
public void Revert()
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif       
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_105a1e124122b2abcee4ea8e9f5108f3.gif       }

img_33d02437d135341f0800e3d415312ae8.gif  
img_33d02437d135341f0800e3d415312ae8.gif       
public void LockRegion(long libOffset, long cb, int dwLockType)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif       
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_105a1e124122b2abcee4ea8e9f5108f3.gif       }

img_33d02437d135341f0800e3d415312ae8.gif  
img_33d02437d135341f0800e3d415312ae8.gif       
public void UnlockRegion(long libOffset, long cb, int dwLockType)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif       
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_105a1e124122b2abcee4ea8e9f5108f3.gif       }

img_33d02437d135341f0800e3d415312ae8.gif  
img_33d02437d135341f0800e3d415312ae8.gif       
public void Seek(long dlibMove, int dwOrigin, System.IntPtr plibNewPositio
img_33d02437d135341f0800e3d415312ae8.gif n)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif       
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif         _stream.Seek(dlibMove, (SeekOrigin)dwOrigin);
img_33d02437d135341f0800e3d415312ae8.gif  
img_33d02437d135341f0800e3d415312ae8.gif         
if(plibNewPosition != IntPtr.Zero)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif         
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif           Marshal.WriteInt32(plibNewPosition, (
int)_stream.Position);
img_105a1e124122b2abcee4ea8e9f5108f3.gif         }

img_105a1e124122b2abcee4ea8e9f5108f3.gif       }

img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif  
img_33d02437d135341f0800e3d415312ae8.gif       
public void Read(byte[] pv, int cb, System.IntPtr pcbRead)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif       
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif         
int size = _stream.Read(pv, (int)_stream.Position, cb);
img_33d02437d135341f0800e3d415312ae8.gif  
img_33d02437d135341f0800e3d415312ae8.gif         
if(pcbRead != IntPtr.Zero)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif         
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif           Marshal.WriteInt32(pcbRead, size);
img_105a1e124122b2abcee4ea8e9f5108f3.gif         }

img_105a1e124122b2abcee4ea8e9f5108f3.gif       }

img_33d02437d135341f0800e3d415312ae8.gif  
img_33d02437d135341f0800e3d415312ae8.gif       
public void Write(byte[] pv, int cb, System.IntPtr pcbWritten)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif       
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif         _stream.Write(pv, 
0, cb);
img_33d02437d135341f0800e3d415312ae8.gif  
img_33d02437d135341f0800e3d415312ae8.gif         
if(pcbWritten != IntPtr.Zero)
img_2887d91d0594ef8793c1db92b8a1d545.gifimg_7a2b9a960ee9a98bfd25d306d55009f8.gif         
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif           Marshal.WriteInt32(pcbWritten, cb);
img_105a1e124122b2abcee4ea8e9f5108f3.gif         }

img_105a1e124122b2abcee4ea8e9f5108f3.gif       }

img_33d02437d135341f0800e3d415312ae8.gif  
img_33d02437d135341f0800e3d415312ae8.gif       
public void SetSize(long libNewSize)
img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif
img_33d02437d135341f0800e3d415312ae8.gif         _stream.SetLength(libNewSize);
img_05dd8d549cff04457a6366b0a7c9352a.gif       }

img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
img_a6339ee3e57d1d52bc7d02b338e15a60.gif       
public void Stat(out STATSTG pstatstg, int grfStatFlag)
img_405b18b4b6584ae338e0f6ecaf736533.gifimg_1c53668bcee393edac0d7b3b3daff1ae.gif       
img_a76e9bb6ed00cf1c9c9f4ee2f04b558b.gif{
img_33d02437d135341f0800e3d415312ae8.gif         pstatstg 
= new STATSTG ();
img_05dd8d549cff04457a6366b0a7c9352a.gif       }

img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
img_a6339ee3e57d1d52bc7d02b338e15a60.gif       
#endregion
img_a6339ee3e57d1d52bc7d02b338e15a60.gif     }
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
img_a6339ee3e57d1d52bc7d02b338e15a60.gif
img_a6339ee3e57d1d52bc7d02b338e15a60.gif

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
+关注
杰克.陈
一个安静的程序猿~
文章
问答
文章排行榜
最热
最新
相关电子书
更多
《零基础HTML入门教程》
立即下载
利用 Poplayer 在手淘中实现稳定业务和临时业务分离
立即下载
低代码开发师(初级)实战教程
立即下载