Recently, I am working on a Winform program. As a person who writes Web programs most of the time, I will definitely think of using Web programs to do this, therefore, the WebBrowser control in Winform is involved at this time. Therefore, this Blog is mainly written around WebBrowser, right! OK! If you don't talk much about it, go to the point! Since the whole program is mainly on display, there will not be much Interface Optimization on the Winform, because the program is processed in full screen mode, so the original WinForm interface is invisible! At this time, the program will be closed for a while. Without the Winform interface, how can we close the program? The only way is to close it through the Web program, however, Web programs and Winform are completely independent. How can they be connected! I think there are two ideas: 1. Determine through the URL address. We define an address connection to close the program and close the program when accessing this address. 2: close the program through the elements in the Document. 3: Can I use js in the Web program to call the Winform method to close the program, as we all know, Webbrowser can call JavaScript of Web programs! So follow these two ideas to start implementation and give a Demo.) create a program named WinWebBrowserDemo and drag a WebBrowser control to MainForm (Change Form1 to MainForm ), the interface is as follows: 650) this. width = 650; "border =" 0 "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1T94CO6-0.jpg "/> There is no blank space. This is the case. But how can we determine whether a connection closes the connection of the program? In this case, an event of the WebBrowser control will be used, we switch the program to the code page! This event is the Navigating event! This event can capture the address information of the page being accessed, and here we can control whether this address can be accessed correctly in the past, we may be based on the obtained address, change the address to another address. You can also cancel this access event and stop the access! Next let's write this event. For example, we define exist.htm to close the connection of the program to facilitate the compilation of the current Code! Code 650) this. width = 650; "align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T9463P0-1.gif "/> Void webBrowser1_Navigating (object sender, WebBrowserNavigatingEventArgs e)
650) this. width = 650; "align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T9463P0-1.gif "/> {
650) this. width = 650; "align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T9463P0-1.gif "/> // throw new NotImplementedException ();
650) this. width = 650; "align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T9463P0-1.gif "/> // check whether this address contains the address of the closed program we defined
650) this. width = 650; "align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T9463P0-1.gif "/> if (e. Url. AbsoluteUri. ToLower (). Contains (" exist.htm "))
650) this. width = 650; "align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T9463P0-1.gif "/> {
650) this. width = 650; "align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T9463P0-1.gif "/> // If the access is canceled and the form or program is closed
650) this. width = 650; "align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T9463P0-1.gif "/> e. Cancel = true;
650) this. width = 650; "align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T9463P0-1.gif "/> this. Close ();
650) this. width = 650; "align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T9463P0-1.gif "/> // close the program
650) this. width = 650; "align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T9463P0-1.gif "/> // Application. Exit ();
650) this. width = 650; "align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T9463P0-1.gif "/>}
650) this. width = 650; "align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T9463P0-1.gif "/>} OK! In this way, as long as we access such a page, the program will be closed! Of course, sometimes we want to add some Js event events to the element tags on the page, so we don't need to use the original events, how can we add events to this element tag at this time? Here, we should note that these js events are custom events on the page, or js events! Or call some methods in Webbrowser! At this time, you need to access the document of the entire page to find out one or some of the elements! The DocumentCompleted event of Webbrowser is used here. This event can obtain all the information on the accessed page. For example, if we want to find a button with the ID number btnHello in the page, the Code is as follows: 650) this. width = 650; "align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T9463P0-1.gif "/> void webbrowserappsdocumentcompleted (object sender, WebBrowserDocumentCompletedEventArgs e)
650) this. width = 650; "align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T9463P0-1.gif "/> {
650) this. width = 650; "align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T9463P0-1.gif "/> // throw new NotImplementedException ();
650) this. width = 650; "align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T9463P0-1.gif "/> HtmlDocument CurentDocument = webBrowser1.Document;
650) this. width = 650; "align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T9463P0-1.gif "/> HtmlElement btnHello = CurentDocument. GetElementById (" btnHello ");
650) this. width = 650; "align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T9463P0-1.gif "/> btnHello. Click + = new HtmlElementEventHandler (btnHello_Click );
650) this. width = 650; "align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T9463P0-1.gif "/>}
650) this. width = 650; "align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T9463P0-1.gif "/>
650) this. width = 650; "align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T9463P0-1.gif "/> void btnHello_Click (object sender, HtmlElementEventArgs e)
650) this. width = 650; "align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T9463P0-1.gif "/> {
650) this. width = 650; "align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T9463P0-1.gif "/> // throw new NotImplementedException ();
650) this. width = 650; "align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T9463P0-1.gif "/> HtmlElement btnHello = sender as HtmlElement;
650) this. width = 650; "align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T9463P0-1.gif "/> // this is the Winfrom method called
650) this. width = 650; "align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T9463P0-1.gif "/> MessageBox. show (btnHello. getAttribute ("value"), "call C # method ");
650) this. width = 650; "align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T9463P0-1.gif "/> // call the js method here, the two are not the same Oh note
650) this. width = 650; "align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T9463P0-1.gif "/> webBrowser1.Document. invokeScript ("alert", new object [] {btnHello. getAttribute ("value") + "Call js method "});
650) this. width = 650; "align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T9463P0-1.gif "/>} Here we can also define a button called btnClose to close the program! No code is written here. The method is the same as above! The following is the last one. I personally think it is also the most flexible method, that is, to write a C # class by myself and provide the front-end JavaScript code to control the program at the front-end, let's start by adding a new class named ServerJsToClient! It should be noted that, here we need to add a COM Accessability modifier to this class as shown in the following 650) this. width = 650; "align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T9463P0-1.gif "/> [System. runtime. interopServices. comVisibleAttribute (true)]
650) this. width = 650; "align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T9463P0-1.gif "/> public class ServerJsToClient so that we can write some methods in this class for front-end js Call Oh, as follows, write the DialogShow method 650) this. width = 650; "align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T9463P0-1.gif "/>/// <summary>
650) this. width = 650; "align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T9463P0-1.gif "/> // call the WebBrowser method class on the HTML page of the Client
650) this. width = 650; "align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T9463P0-1.gif "/>/// </summary>
650) this. width = 650; "align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T9463P0-1.gif "/> [System. Runtime. InteropServices. ComVisibleAttribute (true)]
650) this. width = 650; "align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T9463P0-1.gif "/> public class ServerJsToClient
650) this. width = 650; "align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T9463P0-1.gif "/> {
650) this. width = 650; "align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T9463P0-1.gif "/>/// <summary>
650) this. width = 650; "align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T9463P0-1.gif "/>/// display information
650) this. width = 650; "align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T9463P0-1.gif "/>/// </summary>
650) this. width = 650; "align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T9463P0-1.gif "/>/// <param name =" Caption "> display information title </param>
650) this. width = 650; "align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T9463P0-1.gif "// <param name =" Message "> displayed information </param>
650) this. width = 650; "align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T9463P0-1.gif "/> public void DialogShow (string Caption, string Message)
650) this. width = 650; "align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T9463P0-1.gif "/> {
650) this. width = 650; "align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T9463P0-1.gif "/> MessageBox. Show (Message, Caption, MessageBoxButtons. OK, MessageBoxIcon. Information );
650) this. width = 650; "align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T9463P0-1.gif "/>}
650) this. width = 650; "align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T9463P0-1.gif "/>} class is written, but how to call it, return to MainForm code inside come, add a 650 sentence to MainForm_Load) this. width = 650; "align =" top "src =" http://www.bkjia.com/uploads/allimg/131228/1T9463P0-1.gif "/> webBrowser1.ObjectForScripting = new ServerJsToClient (); then you can call the method of this class in js! Start writing js to call the methods in this class. Let's add a js file External. js, here we specifically write a class to call the methods of the class in Webbrowser, as follows var WEBBROWSER = function (){
/// <Summary>
/// Call the WebBrowser method encapsulation class
/// </Summary>
This. Show = function (caption, msg ){
/// <Summary>
/// Display the message
/// </Summary>
/// <Param name = "caption"> title </param>
/// <Param name = "msg"> message </param>
Window. external. DialogShow (caption, msg );
}
} Here we use window. external, which is a js calling extension js method. In this way, we can call the Webbrowser method as follows: <! Doctype html public "-// W3C // dtd html 4.0 Transitional // EN">
<Html>
<Head>
<Title> </title>
<Script type = "text/javascript" src = "External. js"> </script>
<Script type = "text/javascript">
Var myBrowser = new WEBBROWSER ();
Function Show (){
MyBrowser. Show ("warning", "I want to warn you! ");
}
</Script>
</Head>
<Body>
<A onclick = "Show ();" href = "javascript:;"> warning </a>
<Br/>
<A href = "exist.htm"> close the program </a>
<Br/>
<Input type = "button" id = "btnHello" value = "Click me and then! Webbrowser calls the js method. "/>
</Body>
</Html> OK! If you want to close the program, you can close it by yourself! The program is in the attachment! Thank you. Here is just a summary of a problem I encountered in the program. What are the shortcomings? Thank you!
This article is from the "Laputaliya" blog, please be sure to keep this source http://laputaliya.blog.51cto.com/751941/627200