Use ChtmlView and HTML to make new style interface

zhaozj2021-02-16  50

Many people who have used Outlook Express are novel in the first page HTML interface, which is clear that this is using DHTML technology and has added some webpages of Java Script, but it can interact with the application. In fact, using the newly added MFC CHTMLVIEW of VC6.0, you can also achieve such an exciting program interface. This interface can take advantage of HTML, this is very meaningful, imagined, you can implement the effect in HTML, all can be placed in the program, and you do just write an HTML file and a small amount of programming.

ChtmlView is a new class that MFC added. If you look at the source code of the MFC about this class, it will find that the interface IWebBrowser2 is encapsulated inside. This interface is actually an interface of IE. That is, you can call powerful IE to display the HTML page through this class, and everyone can use this class to easily write a browser. An example of an example MFCIE written in this Class is also taken in the VC.

It is very simple to use the ChtmlView display page. As long as you add an HTML page resource in the resource, you can add the following statement in the program.

LoadFromResource (ID_xxx); // id_xxx is the definition of resources

What is the core of solving this problem How to use ChtmlView to transfer the user to the application on the HTML page. It seems very mysterious here, but in fact there is a trick, you can intercept the user's input. There is an event onBeforenaviGate2 in class chtmlView, which will activate this event when the browser is resigned. For example, whenever the user presses a hyperlink in HTML, or the user enters the new address in the address bar, there is a Navigate method that the programmer calls the interface, the browser is turned to the new address, this event will be activated. Here, when you implement the HTML interface, the user activates the command by clicking on the link on the page, so we can do some processing in this event. There are two more important in this event. LPSzurl is in the HTML page. In the address specified in the href, you can set the corresponding address to each link, and you can identify the links of the user clicks through this parameter. And PBCANCEL can specify whether to cancel the oriented, as long as writing * pbcancel = true, the orientation is canceled, and the chtmlview is displayed or the current page. Let's take a look at the example of the example I write htmlgui.

Void ChtmlguiView :: OnBeforenaviGate2 (LPCTSTSTSTSTSTSTSTSTSTSTSZTSTSTSTSZTARGETFRAMENAME, CBYTEARRAY & BAPOSTEDDATA, LPCTSTST LPSZHEADATA, LPCTSTSTSTLSZHEADATA, LPCTSTSTSTSZHEADATA, BOOL * PBCANCEL)

{

// Todo: Add Your Specialized Code Here and / or Call The Base Class

ProcessCommand (lpszurl))

{

// URL WAS Processed by The Programmer Defined Code.

// Cancel Navigation

* pbcancel = true;

}

Else

{

Chtmlview :: OnBeforenaviGate2 (LPSZURL, NFLAGS, LPSZTARGETFRAMENAME, BAPOSTEDDATA, LPSZHEADERS, PBCANCEL);

}

}

Bool ChtmlguiView :: ProcessCommand (lpctstr lpszurl) {

IF (_TCSCMP (LPSZURL, _T ("app: link1")) == 0)

{

AfxMessageBox ("Link1 Was Pressed, You CAN Process Your Command Here!");

Return True;

}

IF (_TCSCMP (LPSZURL, _T ("app: link2")) == 0)

{

AfxMessageBox ("Link2 Was Pressed, You CAN Process Your Command Here!");

Return True;

}

// Not processed by me.

IF (_TCSCMP (LPSZURL, _T ("app: about")) == 0)

{

:: SendMainWnd () -> getsafehwnd (), WM_MAINAPP_ABOUT, 0, 0);

Return True;

}

Return False;

}

Here I wrote a function BOOL ChtmlguiView :: ProcessCommand (LPCTSTR LPSZURL) to handle the mapping of the command. If the oriented address should be mapped to the program command, make the corresponding process, and return True, indicating that has been processed, Cancel the orientation. Otherwise, returning false, then call the function of the MFC parent class to perform normal operation.

In order not to reveal the flaw, it is best to shield the right mouse button, so you should also overload PretranslateMessage and add the following code.

Bool ChtmlguiView :: PretranslateMessage (MSG * PMSG)

{

// Todo: Add Your Specialized Code Here and / or Call The Base Class

// Do Not Let View Get Right Button Message.

IF ((PMSG-> Message == WM_RBUTTONDOWN) ||

(PMSG-> message == wm_rbuttondblclk))

Return True;

Else

Return ChtmlView :: PretranslateMessage (PMSG);

}

When the user presses the right button or double-click the right button, returns true, so that the ChtmlView window will not get these two messages. Of course, you can also use script in the page to implement this feature.

In addition, there are some things to pay attention to when writing the HTML page. Microsoft In order to enable access to HTML files to access, it defines something that can be called RES protocols for IE. You can enter res: // x: /xxx/htmlgui.exe/gui.htm (x: / xxx / path) in the address bar of IE, and IE should display the HTML file of my program. In fact, when you use IE to access the page, the error message that IE display is also placed in c: /windows/system/shdoclc.dll. The pictures, sounds, etc. used in HTML, obviously also to put into program resources. So these things should be all as HTML to add to resources. Moreover, it is best to use the file name directly to use the resource ID, and add the definition in the resource file.

Gui.htm html discardable "res // gui.htm"

Pic1.jpg html discardable "res // pic1.jpg" Write.gif HTML Discardable "Res // Write.gif"

Chimes.wav html discardable "res // chimes.wav"

Although the VC's resource editor will show them as text, don't worry. As long as you don't press text to edit them, ChtmlView will display them correctly when loading the page. However, don't join them as custom or other resources, if you do it, ChtmlView is loaded, but you will not know them. To ensure that HTML is displayed correctly, all images and sounds can be found, so add the following lines in the HTML file.

ChtmlView also has a function chtmlview :: gethtmlDocument you can get the iDispatch interface of the ATIVE Document object, and then you should use this interface QueryInterface out another interface, you can dynamically control the contents of the HTML page in the program. If you are interested, take a look at MSDN online help, study it.

The HTML interface is a new interface technology that is fast, convenient, easy to troubleshoot, and because Microsoft encapsulates the OLE interface IWebBrowSer2, making it the technical details of it makes it simple, let us create more cool Interface, I wish you a happy program!

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

New Post(0)