Using webbrowser to encapsulate web pages

Source: Internet
Author: User

Recently, the broken network in the school is getting worse and worse. After changing the win7 machine, dr. com has been dropping. I have to switch to different versions, but I have to switch to the webpage login mode. However, I still need to open a web page to access the Internet, which virtually increases my negative comments on the cleanliness of my computer. Therefore, I decided to encapsulate this ugly interface:

Use the webbrowser control to load the page and set visibility = "hidden" so that it is not displayed.

 
Wb_client.source =NewUri ("Http ://"+"192.168.20.1");

The above is the method for loading the page. wb_client is the control name and 192.168.20.1 is the IP address of the school server.

 

Next, extract the elements on the page: The User Name box, password box, confirmation button, and logout button. Microsoft is required. mshtml. DLL in c: \ Program Files \ Microsoft. net \ primary InterOP assemblies. After adding a reference:

 
_ Htmldoc = wb_client.documentAsMshtml. ihtmldocument2;

You can get the document of the page, and then use chrome or Firefox to get the identity of these controls on the browser, such as ID and name, And get these controls through DOM in the background:

 
Mshtml. ihtmlelement element = _ htmldoc. All. Item ("Ddddd");

Ddddd is the control's ID or name attribute value. The obtained element has three required methods: getattribute, setattribute, and click. Needless to say, you can use the first two methods to give the value in the page to the textbox corresponding to your WPF background, or upload the value from the foreground to the page. Click to trigger the logon button on the page. When you click the "fake" button in the foreground, use this method in the background to simulate logon.

If this control does not have an ID or name, for example, the Logout button after I log on does not have a name, how can this problem be solved?

You can use the stupid method to traverse the text and then:

 1   Mshtml. ihtmlelement element;  2 Mshtml. ihtmlelementcollection elements = _ htmldoc. All. Tags ( "  Input "  );  3   Foreach (Mshtml. ihtmlelement elm In  Elements)  4   {  5       If (ELM. getattribute ( "  Value  " )! = "  Cancel  " )  6   {  7 Element = Elm;  8           Break  ;  9   }  10 }

 

Well, the foundation is ready. How can I achieve the login status and local data storage? Webbrowser has two important events that can help solve these problems: navigating and loadcompleted.

Navigating is triggered before the page starts to jump. In this case, I make a judgment. If the "login" button can be found on the page, the user will be logged on, save the obtained username and password to the XML file. Of course, the password is encrypted and decrypted upon retrieval.

Loadcompleted is triggered after page Jump, and the document of webbrowser has been updated. If it is navigated, I find that there are no HTML elements in the document. In this method, tasks that need to be completed include verifying the logon status of the web page and setting the display elements of the WPF interface.

Other functions: automatic logon, minimum interface, automatic password saving,ProgramPallets and icons are easy to implement. Let's take a look at the encapsulated items:

Although it is not very nice, but by using the tray icon class policyicon, I can already put this page separately, and will not mistakenly close the login session because of closing the browser, enough.

Reprinted please specify the original address:Http://www.cnblogs.com/lekko/archive/2012/08/31/2665653.html 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.