WebBrowser 控件可以讓你裝載Windows Form 應用程序中的 Web 網頁和其它採用瀏覽器的文件。可以使用webbrowser 控件將現有的web框架控制項加入至 Windows Form 客戶端應用程序。
WebBrowser 控制項 提供的屬性、方法和事件,可用來實現 Internet Explorer 的控制項
webBrowser1.Navigate("www.cnblogs.com"); //將指定位置處的文件載入至 WebBrowser
webBrowser1.GoBack();//上一頁
webBrowser1.GoForward();//下一頁
webBrowser1.Refresh();//刷新
webBrowser1.GoHome();//主頁
這裡提供了WebBrowser常用的方法,
上面的代碼是將 我們園子的主頁載入到WebBrowser控件中。如果我們想要在應用程式中產生自己的網頁內容,可以設定DocumentText屬性。也可以通過Document屬性來處理目前的網頁內容。如下代碼是使用DocumentText 屬性,顯示網頁內容。並用Document屬性來處理所顯示的網頁。
private void btnDocumentText_Click(object sender, EventArgs e)
{
string szHtml = @"
<HTML>
<HEAD>
<TITLE> DocumentText </TITLE>
</HEAD>
<BODY>
Please enter your name:<br/>
<input type='text' name='Name'/><br/>
<a href='http://www.microsoft.com' >Send input to method of Form class</a>
</BODY>
</HTML>";
webBrowser1.DocumentText = szHtml;
}
private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
System.Windows.Forms.HtmlDocument document = this.webBrowser1.Document;
if (document != null && document.All["Name"] != null && String.IsNullOrEmpty(document.All["Name"].GetAttribute("value")))
{
e.Cancel = true;
System.Windows.Forms.MessageBox.Show("You must enter your name before you can navigate to " + e.Url.ToString());
}
}
<script language = "javascript">
function ClickEvent(name)
{
alert("Hello: " +name);
}
function KeyDown()
{
if (event.keyCode==116)
{
event.keyCode=0;
event.returnValue=false;
}
return false;
}
string szClientScript = ManagedWebBrowser.Properties.Resources.ResourceManager.GetString("ClientScript");
string szWebBrowserText = "<html>" +
"<head>" +
"<title></title>" +
szClientScript +
"</head>" +
"<body onkeydown=\"KeyDown()\" oncontextmenu=\"event.returnValue=false\">" +
"Please enter your name:<br/>" +
"<input type='text' name='Name'/><br/>" +
"<font onclick = 'ClickEvent(Name.value)'>Click Here</font>" +
"</body></html>";
webBrowser1.DocumentText = szWebBrowserText;
WebBrowser 是 System.Windows.Forms 下的控制項,也就是意味著它是用在WimForm程序下,那麼WebWrower所載入的Web頁面如何實現在WinForm程序下處理呢。例如上例中的 "Click Here" 。這裡的Click事件是通過腳本處理的,如何讓這個Click事件在Winform中處理呢?這裡要做一些修改。若要從指令碼存取用戶端應用程式,需要設定ObjectForScripting屬性。指令碼可以將您指定的物件當做window.external 物件來存取。
使用ObjectForScripting屬性,可啟用 WebBrowser 控制項所裝載之 Web 網頁與包含 WebBrowser 控制項之應用程式間的通訊。
這個屬性可讓您整合動態超文字標記語言 (DHTML) 程式碼與用戶端應用程式程式碼。
指定給這個屬性的物件可讓 Web 網頁指令碼做為 window.external 物件,這個物件是為了存取主應用程式而提供的內建 DOM 物件。
private void btnScriptEvent_Click(object sender, EventArgs e)
{
// This is the handler for loading the script into the Web Browser control and allowing us to interact
// between the script in the Browser control and this form class
// Set the ObjectForScripting property of the Web Browser control to point to this form class
// This will allow us to interact with methods in this form class via the window.external property
webBrowser1.ObjectForScripting = this;
string szWebBrowserText = "<html>" +
"<head>" +
"<title></title>" +
"</head>" +
"<body onkeydown=\"KeyDown()\" oncontextmenu=\"event.returnValue=false\">" +
"Please enter your name:<br/>" +
"<input type='text' name='Name'/><br/>" +
"<font onClick='window.external.ClickEvent(Name.value)'>Click Here</font>" +
"</body></html>";
webBrowser1.DocumentText = szWebBrowserText;
}
public void ClickEvent(string userName)
{
// Simply echo out the name that the user typed in the input box of the HTML page
if (System.Threading.Thread.CurrentThread.CurrentUICulture.TextInfo.IsRightToLeft == true)
MessageBox.Show("Hello " + userName, "Managed Web Browser Sample", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign | MessageBoxOptions.RtlReading);
else
MessageBox.Show("Hello " + userName, "Managed Web Browser Sample", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
}
這裡的ObjectForScripting 屬性設置為 this。注意:在From1 類的開頭加入了這麼一句[ComVisible(true)], 它在System.Runtime.InteropServices下,預設值為 true,指出 Managed 型別對於 COM 為可見的。
[ComVisible(true)]
public partial class Form1 : System.Windows.Forms.Form
結束語:
本文簡單的介紹了 WebBrowser 的用法,實現了WinForm程序下與Web頁面的交互使用。本人在院子裡一直以來都是看客,這是第一次在園子裡留下技術足跡。有不妥之處,還望各位朋友指正。
感謝您能看到這裡。