Browser Integration Teaching - Add Macro Support in the browser program

zhaozj2021-02-08  263

Browser Integration Teaching - Add Macro Support in the browser program

This tutorial provides a method of adding macro support in a browser program, you will see how to add macro support to the MFC program. This article also discusses how to extend the ability of the CHTMLVIEW in the VC6, how to implement the MDI structure browser, and how to analyze the DHTML document structure.

Click here to download the code of this article

This article is divided into the following

Prerequisites and Requirements Introduction Active script is an application to add script to support security ChtmlView's enhancement script example conclude

Premise and demand

Before reading this article, it is recommended to

For Microsoft Basics (MFC) and Component Object Models (COM), familiar with the Active Template Library (ATL) Installing Microsoft's Internet Explorer (IE) 6.0 or later has IE6.0 or higher in the development environment Head file and library file

Introduction

Integrated Browser Controls For Quick Application Development (RAD) is a powerful tool; you can display your user interface using dynamic HTML (DHTML), or Scalable Markup Language (XML). A usual use is to use it to display the form and then process the form by analyzing the form page and processing the submitted event. However, if you want to analyze the form of the form, the analysis is completely dependent on the structure of the page, that is, if you analyze the web page through the interface provided in the application, then for each web structure, you have to write and compile once. Code. This is completely no problem with the application and form page, but is on the remote server, but sometimes modify, or if you want to make the application valid for other websites, you must change and Reissue the app. To avoid repeatedly modifying applications, you can use-

Active script

Use the activity script to write flexible processing code without recompilating the program. You may have already seen the active script in many applications, such as IE, Microsoft Office, and Visual Studio. In the Microsoft Windows Script Interface Interface Introduction, the Microsoft Windows Script Interface Interface Text Introduction, describes the concept, background, architecture, and call steps of the event script.

If sample code is required, you can find KBAXScript keywords in Microsoft Knowledge Bases (KB). Here are some examples

Q223139 HOWTO: Add Support for Hosting VBScript to an ATL Application Q183698 SAMPLE: AXSH.exe Demonstrates Implementing ActiveX Script Hosts Q168214 SAMPLE: MFCAxs.exe Implements an ActiveX Script Host Using MFC Q223389 FILE: Scriptng.exe Contains the Files Necessary for Implementing ActiveX Script Hosts and Engines Q232394 HOWTO: CATCH RUN-TIME ERRORS in An ActiveX Script Host

The sample code herein is based on MFC6, so the code provided in Q168214 is used.

Add script support for your application

Automation object

In order to implement scripting support, we need to let the application have automation server support. The easiest way to achieve this support is to select the Automation support in the MDI wizard when you create an IEAUTOMATION MDI application using the Application Wizard, an Application Wizard.

Wizard automatically generated CHTMLVIEW Derife CieAutomationView is not an automation object, so I delete the CieautomationView's definition and implementation files, then delete information in the class wizard, recreate the CIEAUTOMATIONVIEW class, specified when creating The base class is ChtmlView and supports automation. The script interpreter in CIEAUTOMATIONVIEW is modified from the sample code of Q168214, removes some objects, increasing the implementation of the DOM extension object.

Scripter object

Script Engine objects can be accessed with name scripter. Provide methods for creating objects.

Webbrowser object

The browser control object can be accessed with the name WebBrowser. You can use to access the document object model. Viewing the source code feature is also enhanced to display the document object model. For more information, you can view the reference to the article.

External object

DOM expansion objects can be accessed with name external. Objects for the expansion of the document object model for extending the browser. In this example, I also forwarded the event of the WebBrowser object with this object. Although most of the functions are implemented, the auto-completion function seems to have a bit problem, it seems to be related to ISHELLUIHELPER AutoCompleteTtatch. (In fact, browser control needs to implement idochostuihandler :: gethostinfo, refer to http://blog.joycode.com/jiangsheng/archive/2004/01/08/11037.aspx)

Type library support

The type library information for the script is required to access the properties, methods, and events of the object. By default, the class directly derived from the CCOMTARGET is unable to add and delete events, and the CCOMTARGETEX class "Simulates" the partial feature of ActiveX, and deceives the class wizard to do this. When adding type information to an object, refer to the method in Q185720 HOWTO: Provide Type Information from AN MFC Automation Server, add the application's type library to the resource.

safety

Although the automated browser can provide more flexibility, this also exposes part of the application to the user. For example, the user may modify the script so that the application cannot work properly. Also, if the user can view the script, you can understand the program structure, and it can take this attack without a careful design.

Use scripts to create objects or security issues. Some objects are not secure, such as malicious or incorrect COM objects.

If the DOM is extended, the script on the web can access the application's feature, then make sure that the script is secure or from a dependent site. The following function is used to check security before accessing the extension attribute in the DOM.

BOOL CIEAutomationView :: CanAccessExternal () {// if the dispatch we have is safe, // we allow accessif (IsExternalDispatchSafe ()) return TRUE; // the external dispatch is not safe, so we check // whether the current zone allows for scripting // of objects that are not safe for scriptingif (m_spHtmlDoc == NULL) return FALSE; CComPtr spSecMan; m_spHtmlDoc-> QueryInterface (IID_IInternetHostSecurityManager, (void **) & spSecMan); if (spSecMan == NULL) return FALSE; HRESULT hr = spSecMan-> ProcessUrlAction (URLACTION_ACTIVEX_OVERRIDE_OBJECT_SAFETY, NULL, 0, NULL, 0, 0, PUAF_DEFAULT); if (hr == S_OK) return TRUE; return FALSE;} script default settings, can be accessed on a general web Page on the same site. CHTMLVIEW enhancement

Using advanced host features

The benefits of using advanced host features can be found in the 9CBS Document Center Article Custom Browser. In the sample code of this article, I use this feature to extend the DHTML document structure model (DOM), so that the scripts in the web page can access the application. Implementation of offline browsing functions can also refer to this article.

In order to use advanced host feature custom browsers on the MFC6's chtmlView, you need to overload the default control client site (this code is only necessary in MFC6, and MFC7's ChtmlView has supported advanced host features). Because MFC6 cannot overload CWnd's virtual function CreateControlsite to create a custom customer site, use Q236312 HOWTO: Disable The Default Pop-Up Menu for ChtmlView This article, overloads the default control Customer Site Manager. Then save the pointer of the control host in the overloaded default control client site

CCUSTOMCONTROLSITE :: CCustomTMControlsite (ColecontrolContainer * PCNT)

: Colecontrolsite (PCNT)

{

m_pcustomimpl = null;

CWND * PWND = PCNT-> m_pwnd;

IF (PWND) {

IF (PWND-> iskindof (runtime_class (cieautomationView)))) {

CIEAUTOMATIONVIEW * PVIEW = (CIEAUTOMATIONVIEW *) PWND;

m_pcustomimpl = pVIEW;

}

}

This can call the corresponding processing of the control host in the IDOCHOSTUIHANDLER2 implementation of the control customer site, for example

HRESULT FAR EXPORT CCUSTOMCONTROLSITE :: xdochostuihandler2 :: gethostinfo (DochostuiInfo * Pinfo)

{

Method_ProLogue (CCustomControlsite, DochostuiHandler2)

IF (pthis-> m_pcustomimpl) {

Return PTHIS-> M_PCUSTOMPL-> gethostinfo (PINFO);

Return S_OK;

}

One of the applications of advanced host features is to extend the DOM, so that scripts on the web page can use Window.external to access the DOM extension object. The DOM extension object that IE implemented has a Menuarguments property and the ISHELLUIHELPER interface.

Control new window

By default, when the browser receives a new window request, open a new window in IE. You can handle the NewWindow2 event to open the requested page in your designated window.

Void CieautomationView :: OnNewindow2 (lpdispatch * ppdisp, bool * ca Zan)

{

// Get a Pointer to the application object.

CWINAPP * PAPP = AFXGetApp ();

// Get The Correct Document Template.

Position POS = PAP-> getFirstDoctemplatePosition ();

CDOCTemplate * pdoctemplate = PAPP-> GetNextDocTemplate (POS);

// CREATE A New Frame.

CframeWnd * pframe = pdoCtemplate-> CreateNewFrame

GetDocument (),

(Cframewnd *) AFXGETMAINWND ());

// Activate the frame.

PDOCTemplate-> InitialUpdateFrame (Pframe, NULL);

CieautomationView * PView = (CIEAUTOMATIONVIEW *) Pframe-> getActiveView ();

// Pass Pointer Of WebBrowser Object.

PView-> SetRegisterasBrowser (TRUE);

* ppdisp = pView-> getApplication ();

}

If you need more information, see Q184876 HOWTO: Use The WebBrowser Control NewWindow2 Event. .

MDI browser

The sample code of this article is based on a browser. In order to save the efforts, modifications are made directly on the MFCIE example of the MFC, and it is changed to the MDI structure. Mfcie itself is a simple browser, but after moving the code from the main frame to the child framework, the dynamically established Favorites menu is invisible. This is because the MDI framework menu replacement mechanism restores the default menu when the framework is activated, so I overloaded CDocument :: getDefaultMenu to recover my modified menu when the MDI framework is replaced (see). In order to obtain a document pointer during the creation of the subframe to modify the menu saved in the document, you can get the MDI to create the context from the creation structure.

Mdicreatestruct * pmdicreatestruct = (mdicreateStruct *) lpcreateStruct-> lpcreateparams;

CcreateContext * pcreateContext = (ccreatecontext *) pmdicreatestruct-> lparam;

Pmenu = (CieautomationDoc *) PcreateContext-> m_pcurrentdoc) -> m_menudefault.getsubmenu (3);

Favorites

The MFCIE example demonstrates how to build a favorites menu, but when the Show Favorites in the Transplant Toolbar to the MDI subframe, you encounter a problem, the location of the dynamically created favorites menu is not fixed. But by finding the location of the new "Add to Favorites" command, you can determine the location of this menu. The function added to the favorites and manages favorites is implemented by creating a shelluiHelper object. automatic completion

For easy use, I also added automatic completion functions in the application. The automatic completion of the address bar is relatively simple, and the API Shautocomplete of the calling system is OK. In the case where I have expanded the DOM, the automatic completion of the form seems to have some problems.

If you need more information, you can refer to the 9CBS Document Center article I translated to integrate automatic completion in the application.

Access to the site that needs to be authorized

Some sites need to verify user identity when access, but by default, the browser control does not prompt the user to enter the username and password when the user cannot verify the user. Implement the IServiceProvider interface through the customer site of the control, and simultaneously implement the IAUThenticate interface, the application has the function of entering authentication information. For more information, see Microsoft Knowledge Base Article Q329802 Error: Agent authentication performed by IAUThenticate may fail on the security URL.

Commonly used command processing

To use convenience, add the call lookup dialog, view the source code, and set the Internet option. This is achieved by querying the iOleCommandTarget interface of the browser control, execute the command group CGID_WEBBROWSER command. The method of implementing such a command is not only one, for example, can load INETCPL.CPL, call the function launchinternetControlPanel to implement the Internet option; perform search, positioning, and selection using the IMARKUPServices interface, and the content of the web page to get / set the web page. In the sample code, demonstrate how to analyze document structures, and how to edit the selected web page elements of HTML code or the source file of the frame.

MFC6Bug's repair

Although the application can have a relatively complete feature, in order to make the application can work for a long time, you need to fix some of the questions contained in the MFC6. I only list the title title here. If you are interested, you can go to see the Microsoft Knowledge Base article or this document.

Q241750 BUG: CHtmlView Leaks Memory by Not Releasing BSTRs in Several Methods Q220021 BUG: CHtmlView Does Not Repaint Correctly in an AppWizard-Generated MDI Application Q253219 PRB: WebBrowser Control Disappears When Script Calls window.close ()

Script example

WebBrowser.naVigate "About:

this is a test "

DIM MSWORD

SET MSWORD = Scripter.hostcreateObject ("word.basic")

Msword.Appshow

Msword.filenew

Msword.insert "Hello"

Sub External_BeforenaviGate2 (PDISP, URL, Flags, TargetFramen, PostData, Headers, Cancel)

MSGBOX URL

End Sub

If you write a script for an event for the webbrowser object, you will find that these event processing code will not be executed, because ChtmlView has processed these events. You can trigger the corresponding event of custom objects in your CHTMLVIEW derived class. In the sample code, I forwarded the event to the BeforenaviGate2 event to the custom object. in conclusion

Adding scripts to applications can significantly increase the flexibility and scalability of the program. Although some performance, security, and increased code quantity can be sacrificed, but many times this sacrifice is worth it.

Although I didn't forward the DocumentComplete event in the sample code, this is only based on security considerations. Automated browsers can easily implement advertising window filtration, automatically fill out the form, page analysis and other scripts. How to write these scripts depends on your own needs.

reference

Browser

About The Browser Reusing Mshtml Webbrowser Customization (Custom Browser) Loading HTML Content from A Stream

About Cross-Frame Scripting and Security Document Object Model

About The DHTML Object Model About The W3c Document Object Model Working with Windows, Frames, and Dialog Boxes

Using AutoComplete In Html Forms Integrates Auto Finishing Features in your application

转载请注明原文地址:https://www.9cbs.com/read-1865.html

New Post(0)