ChromiumWebBrowser.docx
《ChromiumWebBrowser.docx》由会员分享,可在线阅读,更多相关《ChromiumWebBrowser.docx(17页珍藏版)》请在冰豆网上搜索。
ChromiumWebBrowser
基于Chromium构建ChromeWebBrowserfor.net控件
我分为了两个项目,一是libfuncs,为了使DLL名称一致,我重命名了CefSharp项目,它负责提供操作浏览器的方法和触发事件;二是cwber,它是自定义的WinForm控件,用于在Form上的布局,必须引用libfuncs.dll。
源码地址:
cwber比较简单,以下是它的代码:
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Drawing;
usingSystem.Data;
usingSystem.Text;
usingSystem.Windows.Forms;
usinglibfuncs;
namespacecwber
{
publicpartialclassChromeWebBrowser:
UserControl
{
privateChromeAppchrome=null;
ToolTipmyToolTip=newToolTip();
publicChromeWebBrowser()
{
InitializeComponent();
ChromeWebBrowser.CheckForIllegalCrossThreadCalls=false;
}
privatevoidChromeWebBrowser_Load(objectsender,EventArgse)
{
if(chrome==null)
{
chrome=newChromeApp();
}
Start();
}
#region外部调用方法
libfuncs.DocumentCompletedEventelementEvent1;
libfuncs.FrameLoadStartEventelementEvent2;
libfuncs.FrameLoadEndEventelementEvent3;
libfuncs.FileDownloadingEventfileDownloading;
libfuncs.FileDownloadCompletedEventfileDownloaded;
libfuncs.ComponentInitializedcomponentInitialized;
libfuncs.ToolTipEventListenertoolTipEvent;
publicvoidStart()
{
if(chrome!
=null)
{
chrome.Dock=DockStyle.Fill;
if(componentInitialized==null)
{
componentInitialized=newlibfuncs.ComponentInitialized(componentInitializedEvent);
chrome.RegisterEvent(componentInitialized);
}
Controls.Add(chrome);
if(elementEvent1==null)
{
elementEvent1=newlibfuncs.DocumentCompletedEvent(documentComplete);
chrome.RegisterEvent(elementEvent1);
}
if(elementEvent2==null)
{
elementEvent2=newlibfuncs.FrameLoadStartEvent(frameStartStart);
chrome.RegisterEvent(elementEvent2);
}
if(elementEvent3==null)
{
elementEvent3=newlibfuncs.FrameLoadEndEvent(frameLoadEnd);
chrome.RegisterEvent(elementEvent3);
}
if(fileDownloading==null)
{
fileDownloading=newlibfuncs.FileDownloadingEvent(this.downloading);
chrome.RegisterEvent(fileDownloading);
}
if(fileDownloaded==null)
{
fileDownloaded=newlibfuncs.FileDownloadCompletedEvent(this.downloaded);
chrome.RegisterEvent(fileDownloaded);
}
if(toolTipEvent==null)
{
toolTipEvent=newlibfuncs.ToolTipEventListener(this.ShowToolTipText);
chrome.RegisterEvent(toolTipEvent);
}
chrome.Visible=true;
chrome.BringToFront();
}
}
/*
*描述:
释放浏览器
*/
publicvoidFree()
{
elementEvent1=null;
elementEvent2=null;
elementEvent3=null;
fileDownloading=null;
fileDownloaded=null;
componentInitialized=null;
toolTipEvent=null;
chrome.Dispose();
chrome=null;
}
/*
*参数:
Url打开网页地址
*描述:
打开网址。
*/
publicvoidOpenUrl(stringUrl)
{
if(chrome!
=null)
chrome.Load(Url);
}
/*
*参数:
id网页中的控件元素ID
*描述:
根据元素ID获取元素的值,适用于Input,A标签元素
*/
publicstringGetElementValueById(stringid)
{
returnchrome==null?
"":
chrome.GetElementValueById(id);
}
/*
*参数:
id网页中的控件元素ID,value元素新值
*描述:
为页面中元素赋予新值。
*/
publicvoidSetElementValueById(stringid,stringvalue)
{
if(chrome!
=null)
{
chrome.SetElementValueById(id,value);
}
}
publicdelegatevoidTCallBackElementEventListener();
privateListelementEventList=newList();
/*
*描述:
附加元素的侦听事件。
当该元素触发附加事件时,则执行TCallBackElementEventListener委托方法
*/
publicvoidAppendElementEventListener(stringid,stringeventName,TCallBackElementEventListenercallFunc)
{
libfuncs.ElementEventListenerelementEvent=newlibfuncs.ElementEventListener(callFunc);
elementEventList.Add(elementEvent);
chrome.AddElementEventListener(id,eventName,elementEvent);
}
/*
*描述:
向页面中注入并执行脚本。
*/
publicvoidExecuteScript(stringscript)
{
if(chrome!
=null)
chrome.ExecuteScript(script);
}
publicobjectEvaluateScript(stringscript)
{
if(chrome!
=null)
returnchrome.EvaluateScript(script);
else
returnnull;
}
/*
*描述:
计算文件单位。
用于文件下载。
*/
privatestringCompareFileSize(Int64size)
{
//计算K,M单位
stringstrTotalSize=string.Empty;
if(size<1024)
{
strTotalSize=size.ToString()+"B";
}
elseif(size>=1024&&size<1024*1024)
{
strTotalSize=(size/1024).ToString()+"KB";
}
else
{
strTotalSize=(size/1024/1024).ToString()+"MB";
}
returnstrTotalSize;
}
#endregion
#region属性
publicstringUrl
{
get
{
returnchrome==null?
"":
chrome.Core.Address;
}
}
#endregion
#region事件
/*控件初始化事件*/
publiceventEventHandlerComponentInitializedEventHandler;
privatevoidcomponentInitializedEvent()
{
EventArgse=newEventArgs();
if(ComponentInitializedEventHandler!
=null)
ComponentInitializedEventHandler(this,e);
}
/*页面加载完成事件*/
publiceventEventHandlerDocumentCompletedEventHandler;
privatevoiddocumentComplete()
{
EventArgse=newEventArgs();
if(DocumentCompletedEventHandler!
=null)
DocumentCompletedEventHandler(this,e);
}
/*Frame加载完成事件,这里的Frame可以是页面本身,也是iFrame元素*/
publiceventEventHandlerPageLoadFinishEventHandler;
privatevoidframeLoadEnd()
{
EventArgse=newEventArgs();
if(PageLoadFinishEventHandler!
=null)
PageLoadFinishEventHandler(this,e);
}
/*Frame加载开始事件,这里的Frame可以是页面本身,也是iFrame元素*/
publiceventEventHandlerPageLoadStartEventHandler;
privatevoidframeStartStart()
{
EventArgse=newEventArgs();
if(PageLoadStartEventHandler!
=null)
PageLoadStartEventHandler(this,e);
}
/*下载中事件,不开放该事件*/
FormdownloadForm=null;
privatevoiddownloading(Int64totalSize,Int64loadedSize)
{
stringstrTotalSize=CompareFileSize(totalSize);
stringstrLoadedSize=CompareFileSize(loadedSize);
if(downloadForm==null)
{
downloadForm=newForm();
downloadForm.Text="下载中";
downloadForm.Width=280;
downloadForm.Height=150;
downloadForm.MaximizeBox=false;
downloadForm.MinimizeBox=false;
downloadForm.ControlBox=false;
downloadForm.StartPosition=FormStartPosition.CenterScreen;
Labellabel=newLabel();
label.Left=20;
label.Top=50;
label.Width=250;
label.Text="已下载:
"+strLoadedSize+"/"+strTotalSize;
downloadForm.Controls.Add(label);
}
downloadForm.Show();
downloadForm.BringToFront();
foreach(ControlcindownloadForm.Controls)
{
if(cisLabel)
{
Labellabel=(Label)c;
label.Text="已下载:
"+strLoadedSize+"/"+strTotalSize;
label.Update();
}
}
downloadForm.Update();
}
/*下载完成事件,不开放该事件*/
privatevoiddownloaded()
{
if(downloadForm!
=null)
downloadForm.Close();
downloadForm=null;
}
/*消息提示事件,不开放*/
privatevoidShowToolTipText(stringtext)
{
if(chrome==null)return;
if(string.IsNullOrEmpty(text))
{
myToolTip.RemoveAll();
return;
}
//保证每行40个字
intlen=text.Length;
intoffset=40;
intcount=len/offset;
for(inti=1;i<=count;i++)
{
text=text.Insert(offset*i,"\n");
}
myToolTip.ShowAlways=false;
myToolTip.UseAnimation=true;
myToolTip.UseFading=true;
//t.SetToolTip(button1,text);
Pointp=Control.MousePosition;
Pointp1=this.PointToClient(p);
myToolTip.Show(text,chrome,p1.X+20,p1.Y+10);
}
#endregion
}
}
重点是libfuncs中的libfuncs.h、ChromeApp.h、ChromeApp.cpp、ClientAdapter.h、ClientAdapter.cpp五个文件,实现方法都在里面,其他的文件基本都是接口文件。
我自己只根据项目实际需求来做的功能,接口没有全部实现。
一、ChromeWebBrowser.Net下载与使用
ChromeWebBrowser.Net项目包含三个包,分别是:
浏览器核心:
libfuncs,主要处理浏览器布局、解释、事件、命令等,源代码使用vs2008编译;
dotNet桌面控件:
cwber,是浏览器在界面载体控件,面向开发者,提供与浏览器交互的各种函数和事件,源代码使用vs2008编译;
例子:
example,描述了如果使用cwber控件,源代码使用vs2010编译,也可以自己重做例子,我只是为了验证在vs2010下好不好用;
例子中包含了cwber.dll和libfuncs.dll。
下载地址:
最新版本:
cwber2013.08.08:
包含了cwber和example;
cwber2013.08.08.2:
包含libfuncs;
二、ChromeWebBReleasev1.01
源码地址:
新发的版本中包含了以下函数:
1.设置语言
SetLanguage
2.为浏览器设置cookie保存路径
SetCookiePath
3.全选
SelectAll
4.复制
Copy
5.粘贴
Paste
6.重载
Reload
7.打印
Print
8.查看源码
ViewSource
9.加载Html文本
LoadHtml
10.注入JS脚本
ExecuteScript
11.向前
GoForward
12.回退
GoBack
13.是否能前进
canGoForward
14.是否能后退
canGoBack
15.打开URL地址
OpenUrl
16.获取元素的值
GetElementValueById
17.元素赋予新值
SetElementValueById
18.附加元素的侦听事件。
当该元素触发附加事件时,则执行TCallBackElementEventListener委托方法
AppendElementEventListener
包含的事件:
1.控件初始化事件
ComponentInitializedEventHandler
2.页面加载完成事件
DocumentCompletedEventHandler
3.加载开始事件
PageLoadStartEventHandler
4.加载完成事件
PageLoadFinishEventHandler
三、ChromeWebBv1.02增加缓存数据库支持
源码地址:
在1.02版本增加缓存数据库的支持,缓存数据库是使用SQLite。
cwber:
增加CacheDB类操作SQLite.
libfunc:
增加JsExtendHandler对JS方法的响应。
下面建立html文件来测试,主要内容:
functionmyclick()
{
alert(cachedb.Connect("testdb"));
};
functionmyexecute()
{
cachedb.Execute("createtablemytable(column1int,column2int)");
cachedb.Execute("insertintomytablevalues(1,2)");
cachedb.Execute("insertintomytablevalues(2,3)");
cachedb.Execute("insertintomytablevalues(3,4)");
};
functionmyquery()
{
varres=cachedb.Query("select*frommytable");
alert(res);
varitem=JSON.parse(res);
alert(item.rows[0].column1);
}
functionmyclose()
{
cachedb.Close();
}
四、ChromeWebB1.0.3发布
源码地址:
感谢众多小伙伴们的关注和支持,我现在建立一个讨论QQ群:
343517844,希望大家多提意见。
在1.0.3版本中针对网友提出的问题进行修改,主要修改内容:
1、增加了JavaScript方法:
CallCSharpMethod在JS中调C#方法。
原形定义:
stringCallCSharpMethod(s