Objective
My humble Caishuxueqian, if wrong, also ask you to enlighten me
1. What is WebBrowser
Here are the existing wheels, I want to say they are professional
Http://baike.baidu.com/view/2981935.htm?fr=aladdin
Http://msdn.microsoft.com/zh-cn/library/system.windows.forms.webbrowser (v=vs.110). aspx
2. What to do with it
In addition to the access/operation of the Web page, or as a data acquisition scheme, I have limited knowledge, it is difficult to think what it can do, welcome advice
A lot of people must know that the collection should be HttpWebRequest, or something WebClient, that's a lot more efficient.
Indeed, HttpWebRequest is really much more efficient, because it asks for a web Url that gets all the HTML strings, doesn't load the stuff you basically don't use to collect data.
But if you want to do a simple Web URL client simulation, I think this still has his useful, why say so, because it will load JS Ah, then there is no then.
3. How to use
1. Drag/new
2. Binding Events
3. Handling actions in an event
For more information, please download/view the source code after this article
4. One point extension, what is closures, C # closures
1. What is closures
The word closure comes from JavaScript.
According to the name, it can be explained simply that the enclosing package is simply an anonymous function in which variables can be defined and externally inaccessible, which can be used to extend the action time of external variables.
2.c# Closure Package
/// <summary> ///bind WebBrowser Action/// </summary> /// <param name= "W" ></param> /// <param name= "pageobjects" ></param> Public Static voidXbinding ( ThisW W, List<pageobject> Pageobjects,ObjectProgress =NULL) {setprogress (Progress, P_max, Pageobjects.count); for(inti =0, j = Pageobjects.count; I < J; i++) { varCompletedhandler = (Newfunc<int, PageObject, webbrowserdocumentcompletedeventhandler> (INDEX,PO) = { return NewWebbrowserdocumentcompletedeventhandler (sender, e) = = { BOOLtemp = po. DoAction (Sender asW) && setprogress (progress,p_value,index+1); }); }) (I,pageobjects[i]); W.documentcompleted+=Completedhandler; } } Private Const stringP_max ="Maximum"; Private Const stringP_value ="Value"; Private Static BOOLSetprogress (ObjectPstringPropertyObjectvalue) { if(P! =NULL&& (P isToolstripprogressbar | | P isProgressBar)) {Type type=P.gettype (); Type. GetProperty (property). SetValue (p, value); return true; } return false; }
If the direct new Webbrowserdocumentcompletedeventhandler does not have to be closed, there will be WebBrowser each time the DocumentCompleted event is triggered,
If I were quoted in Webbrowserdocumentcompletedeventhandler, I would always be pageobjects.count-1.
5. A little thought, how to use
Actually, I'm a little guilty about this, because when I use it, I documentcompleted the contents of the Web page.
I don't know if there's a better way to do things after the page is loaded
And the thing that bothers me most is that the code looks really hard to flatter, and if my documentcompleted is going to have multiple pages, I'm going to have to try desperately if else
This is a thing that makes the code very unpleasant, the code is not happy, how can I make friends with it?
Then, then, you can look at the code
6. Conclusion
WebBrowser about the same stuff, that's all I know.
One more thing I forgot to say is that in WebBrowser you might want to call your own JavaScript function, or the function inside the page.
But webbrowser.document only a invokescript, this is not so flexible, probably because I'm not enough daoxing
Maybe you can play like this, too, in the code.
<summary> ////Execute script///In webbrowser.documentcompleted event to prevent the contents of the call not finished loading/// </summary> // <param name= "W" > WebBrowser object to execute script </param>/// <param name= "script" > Script </param > Public static void Xexecscript (this w w, string script, params object[] parameters) { W. Document.invokescript ("eval", new object[] {string. Format (script, Parameters)}); }
Do you think it's over, and wait, let me say something
The page may not be loaded after loading, perhaps you need to check the elements of the page regularly, or do a timeout-like function to perform certain actions
7. Download
Http://files.cnblogs.com/lxmyn/MSolution.Stu.Win.WebBrowser.rar
This is developed with VS2012, of course, I use pirated, I am deeply guilty of this,
If your vs version is lower then a little bit or higher, you might be able to open the project by modifying the. csproj file.
If you're too low, you might have to create a new project yourself, then get the code in, remove the redundant using, and replace the incompatible code with your own handwriting.
On the WebBrowser of "Winform control"