Post: http://blog.csdn.net/jiangsheng/article/details/35567
Integrate browser controls in applications overview what is browser controls
Browser control is an ActiveX control that provides the vast majority of browser functions? Internet Explorer 4.0 (IE) or later. In fact, ie can be considered as a program that integrates browser controls.
Why browser controls?
How to provide users with rich content has always been the goal of programmers. Despite the wide variety of interface libraries that may dazzle you, these are also the nightmare of artists and programmers-it is not easy to customize the appearance of each element on the interface, and sometimes complicated technologies are required, such as the color of the scroll bar in a custom program.
The appearance of browser controls makes it easy. A simple application is to use browser controls to display rich content, just like Microsoft Outlook's Preview pane. You can let the artist make gorgeous effects for you, such as gradient effects, animated GIF or Flash animations on the interface, but at most you only need to write a small amount of HTML code; advanced applications include displaying the entire or part of the user interface using browser controls. For example, Norton Antivirus and Real Player both use HTML to display the interface and use Cascading Style Sheets to unify the interface style; you can use the DHTML Object Model to analyze a webpage without an interface, or write a Browser Helper Object (BHO) to customize browser behavior.
I must also discuss the possible problems with browser integration for users' consideration.
Browser controls are in-process components. That is to say, if the browser or related modules crash, the entire program will crash. Although this is unlikely to happen, there are too many components/plug-ins loaded by the browser, and nothing can be done without failure. This may also be the reason why browser controls do not load BHO by default.
The browser occupies a lot of resources. This includes CPU resources and memory. At the same time, webpages may need additional resources, such as client XML parsing (csdn ......), Dynamic images and ActiveX Controls (mostly Flash controls ). In fact, if you do not restrict the URLs you browse, the common advertisements on the website (usually dynamic images or flash controls) will greatly increase the resources occupied by the browser, especially the CPU resources.
The script loaded in the browser control may cause program instability. Microsoft recommends disabling scripts, but this greatly reduces the practicability of browser controls and makes some advanced custom features such as Dom extension meaningless.
Browsers are not suitable for automated browsing operations. Although Microsoft regards the browser as the core of the operating system, we still see that the memory usage of the browser controls keeps increasing as the browser controls continuously browse the Web page. Because of this phenomenon, browser controls are recommended for static or human control display.
Getting started
Before using the browser control, you need to create it. This can be done by inserting a micro-soft browser control in the dialog box editor or creating a control using the class ID clsid_webbrowser. If you only use the control for display, the first method is sufficient. If you need some advanced functions, such as custom features, you need to use code to control the control creation process. Some class libraries (such as MFC) have built-in support for browser controls (the chtmlxxxx and cdhtmldialog classes), but they provide convenient access to browser controls while at the same time, it also makes the process of custom browsers more complicated. In my opinion, they are best used as examples and custom basis for accessing browser controls.
Iwebbrowser2 is a main COM interface provided by browser controls. It provides a basic browser operation interface and some methods of this interface. For example, the commonly used method is navigate/navigate2, which enables the browser control to open a target you specify, for example, a folder, a webpage, or an activity document. You may need to effectively understand the features provided by the iwebbrowser2 interface to your application. First, you need to have a basic understanding of the object models of Internet Explorer and DHTML. Of course, a full explanation of these models may occupy a large amount of space, because each independent element type in the DHTML file (<B>, <HTML>, <body>, and so on) all have their own COM object classes or interfaces. The best way to get a thorough understanding of these object models is to read some books and get a tool. Inside
Dynamic HTML (Microsoft Press, 1997) is one of the best reference books for DHTML Object Models. For more technical information about the object model, you can study the DHTML header files of the Internet client SDK (especially mshtml. H, expdispid. H, mshtmhst. H, and mshtmdid. h ). These files specify interfaces supported by various types or objects. Unfortunately, the available Internet
The help documentation in the client SDK only provides a moderate level of assistance. They explain how to use the iwebbrowser2 interface, but do not provide much information about the relationship between objects and interfaces. Microsoft has few related documents, so it is interesting to explore whether DHTML objects support those interfaces. In the Microsoft Knowledge Base, you often see DHTML objects supporting standard iconnectionpointcontainer, iolecommandtarget, or iolecontainer interfaces.
How to use MFC to integrate browser controls
Mfc6 has built-in support for browser controls, but advanced interfaces such as idochostuihandler are only supported by version 7.0. Using MFC to write browser control applications is relatively simple. You may only need a small amount of code to create a program with most browser functions. The disadvantage of using MFC is that mfc6 has many bugs in these classes. to customize some features, you need to understand the control creation process of MFC. MFC has an mfcie example to demonstrate a simple browser program created using chtmlview. Paul dilascia's c ++ Q & A column in The msdn magazine demonstrates how to integrate chtmlview in the dialog box and disable the context menu of browser controls.
Customize browser behavior
If the browser control is integrated into the application, you can refer to the webbrowser customization article in msdn. An MFC-based example can be found in the http://www.beginthread.com/Article/Ehsan/Advanced%20CHtmlView% 20Hosting.
If you write BHO, you can use the msdn Article Browser Helper Objects: the browser The Way You Want It (Dino Esposito) to get started.
Reload the default control site in MFC
To implement some advanced browser host features, some interfaces must be implemented on the same object of the control site. For example, iserviceprovider used to query the service interfaces provided by the host (common services include ihtmledithost for the HTML editor, ihtmleditservices, and iinternethostsecuritymanager for Security Options of custom IE) and the idochostuihandler, idochostuihandler2, and idochostshowui interfaces used to customize browser behaviors and interfaces.
Although in mfc7, You can overload cwnd: createcontrolsite to overwrite the creation of the control site, it is much more complicated in mfc6. Microsoft Knowledge Base Article HOWTO: Disable the default pop-up menu for chtmlview describes one of the methods, but this method does not look so elegant, the default OLE container creator must be overwritten at startup, and other types of control sites cannot be created in the window. In fact, this is not necessary. Analyze the mfc6 code under idea
Void afx_cdecl afxenablecontrolcontainer (coccmanager * poccmanager)
{
If (poccmanager = NULL)
Afxoccmanager = _ afxoccmanager. getdata ();
Else
Afxoccmanager = poccmanager;
}
You can see that you only need to call afxenablecontrolcontainer before creation, and pass your own container Creator as the parameter, after the site is created, call null again to overwrite the default OLE container creator. This is called in the chtmlview: create that you overwrite. You must override this process, because chtmlview: Create will call afxenablecontrolcontainer (null ).
Bool chtmlview: Create (maid, maid,
DWORD dwstyle, const rect & rect, cwnd * pparentwnd,
Uint NID, ccreatecontext * pcontext)
{
......
// Assume that the control container is enabled
Afxenablecontrolcontainer ();
......
FAQ mfc6 chtmlview bug
When I wrote a second version of my web-based game plug-in, I tried to write my experience into an article (this article has been published in my column ). As my first non-tested browser (other browsers are basically too simple and have such inconvenience or defects that cannot be used conveniently and stably, the first problem I encountered was to fix the MFC bug (not to mention the IE bug ). Although I found and found not too many bugs, the amount of code used to solve these bugs is also considerable. Most of the bugs I have found so far have been solved in the sample code in this article, although the article has very little space on these issues.
Press Ctrl + N in the cdhtmldialog-derived dialog box to display IE
Cdhtmldialog captures the dwebbrowserevets event and forwards it to the virtual function without capturing dwebbrowserevents2. Therefore, when pressing CTRL + n to trigger the dwebbrowserevents2 event, execute the default operation -- open the new ie window. This may not be your expected behavior. The solution is to write an eventsink by yourself, and you do not have to forward it to the virtual function. See Microsoft Knowledge Base Article 181845
Howto: Create a sink interface in MFC-based COM Client
Access Document Object Model in ActiveX Control
Knowledge Base Article q172763 info: accessing the object model from within an ActiveX control describes the solution to this problem. You can also use ioleclientsite to interact with the IE control container. You can use ioleclientsite: getcontainer to obtain the iolecontainer interface of the HTML document object where the webpage is located, and then query other interfaces, such as ihtmldocument2, to access the DHTML Object Model.
Create and manipulate ie browsers
You can use cocreateinstance to create a browser object. The clsid used is clsid_internetexplorer. After the browser object is created, you can query other interfaces of the browser object, such as iwebbrowser2 and ioleobject.
Analyze webpages and automatically submit web forms
Frequently asked questions, but it is not easy to write general-purpose Web pages. In general, the document on the Chinese site of msdn is first removed from the web page.
How do I call functions in scripts on a webpage?
Ihtmldocument2: The scripts attribute indicates all Script objects in the HTML document. You can use the: getidsofnames method of the idispatch interface of the script object to "find the functions and object members, and use idispatch: invoke to access these members.
Reference
- Inside Ole, Version 2, by kraig brockschmidt (Microsoft Press)
- Understanding ActiveX and Ole, David Chappell (Microsoft Press)
- Inside comBy Dale Rogerson (Microsoft Press)
Post: http://blog.csdn.net/jiangsheng/article/details/35567