<html><body><input type="button" id="btnClose" value="关闭" /></body></html>
HtmlDocument htmlDoc = webBrowser.Document;HtmlElement btnElement = htmlDoc.All["btnClose"];if (btnElement != null){btnElement.click += new HtmlElementEventHandler(HtmlBtnClose_Click);}
btnElement.AttachEventHandler("onclick", new EventHandler(HtmlBtnClose_Click));//这一句等价于上面的btnElement.click += new HtmlElementEventHandler(HtmlBtnClose_Click);
formElement.AttachEventHandler("onsubmit", new EventHandler(HtmlForm_Submit));
HtmlElement btnSubmit = webBrowser.Document.All["submitbutton"];HtmlElement tbUserid = webBrowser.Document.All["username"];HtmlElement tbPasswd = webBrowser.Document.All["password"];if (tbUserid == null || tbPasswd == null || btnSubmit == null)return;tbUserid.SetAttribute("value", "smalldust");tbPasswd.SetAttribute("value", "12345678");btnSubmit.InvokeMember("click");
HtmlElement formLogin = webBrowser.Document.Forms["loginForm"];//……formLogin.InvokeMember("submit");
public partial class SearchDemo : Form{// 建立一个查找用的TextRange(IHTMLTxtRange接口)private IHTMLTxtRange searchRange = null;public SearchDemo(){InitializeComponent();}private void btnSearch_Click(object sender, EventArgs e){// Document的DomDocument属性,就是该对象内部的COM对象。IHTMLDocument2 document = (IHTMLDocument2) webBrowser.Document.DomDocument;string keyword = txtKeyword.Text.Trim();if (keyword == "")return;// IE的查找逻辑就是,如果有选区,就从当前选区开头+1字符处开始查找;没有的话就从页面最初开始查找。// 这个逻辑其实是有点不大恰当的,我们这里不用管,和IE一致即可。if (document.selection.type.ToLower() != "none"){searchRange = (IHTMLTxtRange) document.selection.createRange();searchRange.collapse(true);searchRange.moveStart("character", 1);}else{IHTMLBodyElement body = (IHTMLBodyElement) document.body;searchRange = (IHTMLTxtRange) body.createTextRange();}// 如果找到了,就选取(高亮显示)该关键字;否则弹出消息。if (searchRange.findText(keyword, 1, 0)){searchRange.select();}else{MessageBox.Show("已搜索到文档结尾。");}}}
public partial class HilightDemo : Form{// 定义高亮显示效果的标签。string tagBefore = "<span style=‘background-color:yellow;color:black‘>";string tagAfter = "</span>";// ……private void btnHilight_Click(object sender, EventArgs e){HtmlDocument htmlDoc = webBrowser.Document;string keyword = txtKeyword.Text.Trim();if (keyword == "")return;object oTextRange = htmlDoc.Body.InvokeMember("createTextRange");mshtml.IHTMLTxtRange txtrange = oTextRange as mshtml.IHTMLTxtRange;while (txtrange.findText(keyword, 1, 4)){try{txtrange.pasteHTML(tagBefore + keyword + tagAfter);}catch { }txtrange.collapse(false);}}}
public partial class HilightDemo : Form{//……private void btnHilight_Click(object sender, EventArgs e){HTMLDocument document = (HTMLDocument) webBrowser.Document.DomDocument;IHTMLDOMNode bodyNode = (IHTMLDOMNode) webBrowser.Document.Body.DomElement;string keyword = txtKeyword.Text.Trim();if (keyword == "")return;HilightText(document, bodyNode, keyword);}private void HilightText(HTMLDocument document, IHTMLDOMNode node, string keyword){// nodeType = 3:text节点if (node.nodeType == 3){string nodeText = node.nodeValue.ToString();// 如果找到了关键字if (nodeText.Contains(keyword)){IHTMLDOMNode parentNode = node.parentNode;// 将关键字作为分隔符,将文本分离,并逐个添加到原text节点的父节点string[] result = nodeText.Split(new string[] { keyword }, StringSplitOptions.None);for (int i = 0; i < result.Length - 1; i++){if (result[i] != ""){IHTMLDOMNode txtNode = document.createTextNode(result[i]);parentNode.insertBefore(txtNode, node);}IHTMLDOMNode orgNode = document.createTextNode(keyword);IHTMLDOMNode hilightedNode = (IHTMLDOMNode) document.createElement("SPAN");IHTMLStyle style = ((IHTMLElement) hilightedNode).style;style.color = "black";style.backgroundColor = "yellow";hilightedNode.appendChild(orgNode);parentNode.insertBefore(hilightedNode, node);}if (result[result.Length - 1] != ""){IHTMLDOMNode postNode = document.createTextNode(result[result.Length - 1]);parentNode.insertBefore(postNode, node);}parentNode.removeChild(node);} // End of nodeText.Contains(keyword)}else{// 如果不是text节点,则递归搜索其子节点IHTMLDOMChildrenCollection childNodes = node.childNodes as IHTMLDOMChildrenCollection;foreach (IHTMLDOMNode n in childNodes){HilightText(document, n, keyword);}}}}
<b>Hel</b>lo World!
function DoAdd(a, b) {return a + b;}
object oSum = webBrowser.Document.InvokeScript("DoAdd", new object[] { 1, 2 });int sum = Convert.ToInt32(oSum);
string scriptline01 = @"function ShowPageInfo() {";string scriptline02 = @" var numLinks = document.links.length; ";string scriptline03 = @" var numForms = document.forms.length; ";string scriptline04 = @" var numImages = document.images.length; ";string scriptline05 = @" var numScripts = document.scripts.length; ";string scriptline06 = @" alert(‘网页的统计结果:\r\n链接数:‘ + numLinks + ";string scriptline07 = @" ‘\r\n表单数:‘ + numForms + ";string scriptline08 = @" ‘\r\n图像数:‘ + numImages + ";string scriptline09 = @" ‘\r\n脚本数:‘ + numScripts);}";string scriptline10 = @"ShowPageInfo();";string strScript = scriptline01 + scriptline02 + scriptline03 + scriptline04 + scriptline05 +scriptline06 + scriptline07 + scriptline08 + scriptline09 + scriptline10;IHTMLWindow2 win = (IHTMLWindow2)webBrowser.Document.Window.DomWindow;win.execScript(strScript, "Javascript");
联系客服