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.gif UCOMIStream stream  =   null ;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
img_a6339ee3e57d1d52bc7d02b338e15a60.gif CreateStreamOnHGlobal(Marshal.StringToHGlobalUni(value), 
true out  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.gif UCOMIStream stream  =   null ;
img_a6339ee3e57d1d52bc7d02b338e15a60.gif  
img_a6339ee3e57d1d52bc7d02b338e15a60.gif CreateStreamOnHGlobal(Marshal.AllocHGlobal(
4096 ),  true out  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
目录
相关文章
|
8月前
|
C#
C#读取html文件
C#读取html文件
137 3
|
3月前
|
C#
C#实现的html内容截取.
C#实现的html内容截取.
31 0
|
5月前
|
编译器 C# Windows
C#基础:手动编译一个.cs源代码文件并生成.exe可执行文件
通过上述步骤,应该能够高效准确地编译C#源代码并生成相应的可执行文件。此外,这一过程强调了对命令行编译器的理解,这在调试和自动化编译流程中是非常重要的。
434 2
|
7月前
|
C#
【C#】C#读写Excel文件
【C#】C#读写Excel文件
150 1
|
7月前
|
人工智能 缓存 Java
技术经验解读:【转】详细解析用C#写的小游戏《彩色连珠》(附源代码)
技术经验解读:【转】详细解析用C#写的小游戏《彩色连珠》(附源代码)
32 0
|
8月前
|
JavaScript 前端开发 C#
C# webbrowser控件设置代理IP访问网站
C# webbrowser控件设置代理IP访问网站
425 5
|
8月前
|
数据安全/隐私保护
HTML表单(Form)常用控件
HTML表单(Form)常用控件。
82 1
|
8月前
|
JavaScript C#
C#winForm程序与html JS交互调用
C#winForm程序与html JS交互调用
|
8月前
|
XML C# 数据格式
C#生成sitemap.html文件
C#生成sitemap.html文件
36 3
|
8月前
|
Web App开发 前端开发 JavaScript
新的 HTML 控件登陆 Safari
# html # 网络开发者