WebBrowser 控件可以让你装载Windows Form 应用程序中的 Web 网页和其它采用浏览器的文件。可以使用webbrowser 控件将现有的web框架控制项加入至 Windows Form 客户端应用程序。
还是直接看代码吧。
上面的代码是将 我们园子的主页载入到WebBrowser控件中。如果我们想要在应用程式中产生自己的网页内容,可以设定DocumentText属性。也可以通过Document属性来处理目前的网页内容。如下代码是使用 DocumentText 属性,显示网页内容。并用Document属性来处理所显示的网页。
 private void btnDocumentText_Click(object sender, EventArgs e)
private void btnDocumentText_Click(object sender, EventArgs e) {
        { string szHtml = @"
            string szHtml = @" <HTML>
<HTML> <HEAD>
<HEAD> <TITLE> DocumentText </TITLE>
<TITLE> DocumentText </TITLE> </HEAD>
</HEAD>
 <BODY>
<BODY> Please enter your name:<br/>
     Please enter your name:<br/> <input type=‘text‘ name=‘Name‘/><br/>
     <input type=‘text‘ name=‘Name‘/><br/> <a href=‘http://www.microsoft.com‘ >Send input to method of Form class</a>
    <a href=‘http://www.microsoft.com‘ >Send input to method of Form class</a> 
      </BODY>
</BODY> </HTML>";
</HTML>";
 webBrowser1.DocumentText = szHtml;
            webBrowser1.DocumentText = szHtml; 
           }
        }
 private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
        private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e) {
        { System.Windows.Forms.HtmlDocument document =  this.webBrowser1.Document;
            System.Windows.Forms.HtmlDocument document =  this.webBrowser1.Document;
 if (document != null && document.All["Name"] != null && String.IsNullOrEmpty(document.All["Name"].GetAttribute("value")))
            if (document != null && document.All["Name"] != null && String.IsNullOrEmpty(document.All["Name"].GetAttribute("value"))) {
            { e.Cancel = true;
                e.Cancel = true; System.Windows.Forms.MessageBox.Show("You must enter your name before you can navigate to " +  e.Url.ToString());
                System.Windows.Forms.MessageBox.Show("You must enter your name before you can navigate to " +  e.Url.ToString()); }
            }
 }
        }
既然我们可以通过DocumentText生成自己的网页,那么能不能象使用IE那样操作这个网页呢?,答案是肯定的,完全可以像操作Web程序那样操作WebBrowser 控制项。比如我们可以加入脚本,CSS。当然,如果你熟悉 HTML 物件对象模型 (DOM),也可以透过 Document 属性来处理目前的Web网页内容。下面的例子加入了JavaScript脚本来控制网页。如果要在Winfrom程序中写大量的Javascriot代码,而且这些代码最终要转换成String型载入到Webbrowser 那将是很痛苦的事情,不过没有关系,我们可以创建一个js文件,放入资源中,用的时候只需从资源中载入即可。这里我创建一个名为 ClientScript.js 的文件。
 <script language = "javascript">
<script language = "javascript"> function ClickEvent(name)
function ClickEvent(name) {
{ alert("Hello: " +name);
    alert("Hello: " +name); }
}
 function KeyDown()
function KeyDown() {
{  if (event.keyCode==116)
    if (event.keyCode==116) {
    { event.keyCode=0;
         event.keyCode=0; event.returnValue=false;
         event.returnValue=false; }
    } 
   return false;
      return false; }
}
 string szClientScript = ManagedWebBrowser.Properties.Resources.ResourceManager.GetString("ClientScript");
string szClientScript = ManagedWebBrowser.Properties.Resources.ResourceManager.GetString("ClientScript");
 string szWebBrowserText = "<html>" +
            string szWebBrowserText = "<html>" + "<head>" +
                "<head>" + "<title></title>"+
                "<title></title>"+                 szClientScript +
                    szClientScript + "</head>" +
                 "</head>" + "<body onkeydown=\"KeyDown()\" oncontextmenu=\"event.returnValue=false\">"+
               "<body onkeydown=\"KeyDown()\" oncontextmenu=\"event.returnValue=false\">"+ 
                "Please enter your name:<br/>"+
               "Please enter your name:<br/>"+ "<input type=‘text‘ name=‘Name‘/><br/>"+
                 "<input type=‘text‘ name=‘Name‘/><br/>"+ "<font onclick = ‘ClickEvent(Name.value)‘>Click Here</font>"+
                 "<font onclick = ‘ClickEvent(Name.value)‘>Click Here</font>"+ "</body></html>";
                "</body></html>";

 webBrowser1.DocumentText = szWebBrowserText;
            webBrowser1.DocumentText = szWebBrowserText;WebBrowser 是 System.Windows.Forms 下的控制项,也就是意味着它是用在WimForm程序下,那么WebWrower所载入的Web页面如何实现在WinForm程序下处理呢。例如上例中的 "<font onclick = ‘ClickEvent(Name.value)‘>Click Here</font>" 。这里的Click事件是通过脚本处理的,如何让这个Click事件在Winform中处理呢?这里要做一些修改。若要从指令码存取用户端应用程式,需要设定ObjectForScripting 属性。指令码可以将您指定的物件当做window.external 物件来存取。
使用ObjectForScripting属性,可启用 WebBrowser 控制项所装载之 Web 网页与包含 WebBrowser 控制项之应用程式间的通讯。
这个属性可让您整合动态超文字标记语言 (DHTML) 程式码与用户端应用程式程式码。
指定给这个属性的物件可让 Web 网页指令码做为 window.external 物件,这个物件是为了存取主应用程式而提供的内建 DOM 物件。
 private void btnScriptEvent_Click(object sender, EventArgs e)
 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
            // 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
            // 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
            // 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
            // This will allow us to interact with methods in this form class via the window.external property  webBrowser1.ObjectForScripting = this;
            webBrowser1.ObjectForScripting = this;
 string szWebBrowserText = "<html>" +
            string szWebBrowserText = "<html>" + "<head>" +
                "<head>" + "<title></title>"+
                "<title></title>"+                     "</head>" +
                 "</head>" + "<body onkeydown=\"KeyDown()\" oncontextmenu=\"event.returnValue=false\">"+
               "<body onkeydown=\"KeyDown()\" oncontextmenu=\"event.returnValue=false\">"+ 
                "Please enter your name:<br/>"+
               "Please enter your name:<br/>"+ "<input type=‘text‘ name=‘Name‘/><br/>"+
                 "<input type=‘text‘ name=‘Name‘/><br/>"+ "<font onClick=‘window.external.ClickEvent(Name.value)‘>Click Here</font>"+
                 "<font onClick=‘window.external.ClickEvent(Name.value)‘>Click Here</font>"+ "</body></html>";
                "</body></html>";

 webBrowser1.DocumentText = szWebBrowserText;
            webBrowser1.DocumentText = szWebBrowserText; }
        } public void ClickEvent(string userName)
        public void ClickEvent(string userName) {
        { // Simply echo out the name that the user typed in the input box of the HTML page
            // 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)
            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);
                MessageBox.Show("Hello " + userName, "Managed Web Browser Sample", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1, MessageBoxOptions.RightAlign | MessageBoxOptions.RtlReading); else
            else MessageBox.Show("Hello " + userName, "Managed Web Browser Sample", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
                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与WebForm交互
原文:https://www.cnblogs.com/asdyzh/p/9826601.html