Use browser to implement the process interface and implementation

xiaoxiao2021-03-06  233

Keyword webbrowser, idochostuihandler, getExternal

1 Introduction When you write a Windows application when you use Delphi, Visual Basic, you will have a few questions: 1) I hope the program interface is beautiful. In Delphi, developers typically use various controls to realize the stylization of the interface, but the disadvantage is that the application is large, and it is often plagued by the problem that the control version is not compatible with the Delphi version when the upgrade is often incompatible. 2) I hope that the application has different interface styles in the case of failure. This often achieves the technology of "skin" technology, but the volume of controls that achieve "skinned" function is large, and the interface response speed is slow, and "skin" is more troublesome. 3) Maintenance difficulties in the program interface. In order for the interface to achieve "skin contact", it is often used to use the technique of design mode, which is more difficult to use developers who are unfamiliar with design patterns.

Microsoft is expected to release the next-generation operating system (development code for Longhorn) in 2006. The structure and deployment of the application will have major changes, one is to describe the application's interface completely in XML an extension XAML language. In order to achieve the height of the interface to be customizable. This will undoubtedly easily solve the above problems. The question is whether there is any similar approach now? The answer is to use the browser control. Microsoft's web browser Internet Explorer's core is designed to be able to embed an ActiveX component that can be reused in the application. It has strong programmable ability and ability to interact with containers, making developers quickly develop powerful application. From the architecture chart of Internet Explorer, we can see that our usual running ipplorer.exe is actually just a shell, real browsing web page, record history, etc. WEBBROWSER CONTROL in ShDocvw.dll Finished.

The function of SHDOCVW.DLL is to call mshtml.dll to resolve the web, and embed other active document components in its window (such as Microsoft Office, Adobe Acrobat, etc.) can be viewed in the browser window. On the other hand, the MSHTML.dll handles HTML analysis and the host of the script engine, Java virtual machine, ActiveX control, plugin, on the other hand, it implements the active document server interface, allowing the application to embed it in a standard COM interface. The web page and web page elements are accessed in the program and through the exposed interface. Through SHDOCVW.DLL, the elements in the web page can access the properties and methods provided by the shell application (such as Window.External.AddFavorite (Location.href, Document.title) that calls the IE's AddFavorite method to add the current page. To a favorites, the enclosure application can access the properties, methods, behaviors, events, and more in the web page through the interface provided by MSHTML.DLL. The method of solving several problems presented in the beginning of the article is based on ShDocvw.dll and MSHTML.DLL. Some famous software, such as: Microsoft Money, Microsoft Visual Studio .NET, Macromedia Dreamweaver MX 2004, etc. use this technology.

2 Principles 1) The interface of the program is completely made by making a web page. The web page has powerful performance capabilities in text, image, sound, etc., using the web page production tool you have obtained, easy to make a graphic and tiny web page. As the interface of the program, its effect is better than any interface control. 2) "Skating" function is easy to implement. Simply make different types of program interfaces with different styles. 3) The function of the program is implemented in the application within the application, and the element calls are provided to the web page via an automation interface. This realizes the separation of program interfaces and code, and changes in web layout and style do not affect the implementation of the program. 3 Attributes and methods of calling the housing from the web page 3.1 GetExternal Interface Method WebBrowser Control provides the enclosure application to extend the IE object model (DOM) with its own object, method, and attribute, etc. to achieve personalized customization . Accessing the extension of the housing application in the web page is implemented by the "External" object of the document, as the shell provides a method called AddFavorite, the web page is called via Window.external.Addfavorite (). The core of implementing this is the getExternal method for the IDochostuihandler interface: HRESULT getExternal (iDispatch ** ppdispatch); Implement the IDOCHOSTUIHANDLER interface in custom webbrowser control, when web elements access the properties and methods of the enclosure through the "External" object, The getExternal method will be called, and the automation interface of the enclosure properties and methods can be passed to PPDispatch in this method. Customwebrowser Control sample code is as follows, where getExternal is packaged to an ONGETEXTERNAL event for external programs. There are 15 ways of IDochostuiHandler interface. Here we only care about the getExternal method, which is slightly 14 (omitted code). Unit ZocWebBrowser;

Interface

Uses Variants, Ieconst, Windows, Sysutils, Classes, Shdocvw, Activex, Shlobj, Mshtml, ComoBJ;

type ...... TGetExternalEvent = function (out ppDispatch: IDispatch): HRESULT of object; // define OnGetExternal event type TZoCWebBrowser = class (TWebBrowser, IDocHostUIHandler) private ...... FOnGetExternal: TGetExternalEvent; protected ...... function GetExternal (out ppDispatch: IDispatch): HRESULT; stdcall; published ...... property OnGetExternal: TGetExternalEvent read FOnGetExternal write FOnGetExternal; end; ...... implementation ...... function TZoCWebBrowser.GetExternal (out ppDispatch: IDispatch): HRESULT; begin if Assigned (FOnGetExternal) then Result: = FOnGetExternal (ppDispatch) Else Result: = S_FALSE; END;

Initialization Oleinitialize (NIL); Finalization Try OleunInitialize; Except End; END.3.2 Implementing the Cabiner Extension Automation Interface In Delphi's "New Items" dialog box, switch to "ACTIVEX" page, select "Automation Object", create a new automation object And fill in the interface name "MyExternal", "instancing" in the "COCLASS NAME" column, "INSTANCIING" is selected, indicating that the object can only be created inside the program, and the external program cannot be created directly. When you click the "OK" button, add two methods for the iMyexternal interface for the iMyexternal interface in the Type Library Edit dialog. At this point, the code is roughly as follows:

Unit myexternalImpl;

{WARN SYMBOL_PLATFORM OFF}

Interface

Uses Comobj, ActiveX, Project1_TLB, STDVCL;

TYPE TMYEXTERNAL = Class (Tautoobject, iMyexternal) protected; saFecall; process; switchui;

IMPLEMENTATION

Uses Comserv; Procedure Tmyexternal.showAboutBox; Begin MessageBox (Mainform.handle, 'getExternal Demo', 'ZocWebBrowser', MB_OK or MB_ICONASTERISK);

Procedure Tmyexternal.Switchui; Begin Showswitchuiform; // Displays the Switch Interface Dialog End;

Initialization TautoObjectFactory.create (COMSERVER, TMYEXTERNAL, CLASS_MYEXTERNAL, CIINTERNAL, TMAPARTMENT); END.

3.3 Calling the Shell From the web page Place a custom webbrowser control in the program main window, named ZocWebBrowser, write its OnGeTexternal event (trigger by Window.external calls in the web), the code is as follows:

function TMainForm.ZoCWebBrowserGetExternal (out ppDispatch: IDispatch): HRESULT; var MyExternal: TMyExternal; begin MyExternal: = TMyExternal.Create; // Create automation interface objects ppDispatch: = MyExternal; // object is passed to the interface WebBrowser Control // This is really calling the TMYEXTERNAL object we implemented in the "External" object.

Suppose we have made two top-style web style1.html and style2.html as the program interface, which have two buttons (or other web elements), HTML code examples, such as: About switching interface When the program starts running, let the webbrowser control are full form, and the Style1.html page is displayed, then the program will display a switching interface when clicking on the "About" button, and click the "Switch Interface" button to display the switching interface. The dialog box, select Style2.html and let the WebBrowser Control are displayed to get a completely different interface, but is functionally identical to Style1.html. 4 Summary From the above example, we can see that our simple ways achieve the separation of the program interface and implementation, which is conducive to the maintenance and expansion of the program. In the traditional way, interface design and encoding are usually done by programmers, and the programmer is heavier, and the second is difficult to guarantee the quality of the interface. Using the above method, the program interface can be designed by professional artists, he can design a complete interface if you don't know how the program is implemented, and programmers only focus on code writing, and will be necessary and attributes Exposes by an automation interface. When mergen, place the desired button or other web page elements in a suitable location in the web page, and give a simple script call.

(The above code is debugged in the Windowsxp Delphi 7 environment)

5 References "MSDN Library - July 2003"

6 Reference address Using the browser to implement the segmentation and implementation

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

New Post(0)