HTML window event handling
What is missing in the CHTMLVIEW class?
Typical program processing scripts are assumed to receive events or data inputs from each element (such as button) of the program interface. So in our program, you also need to solve the information interaction between the HTML interface and the MFC background program. We don't have to take a headache. In fact, this problem is not complicated, we can use the OnBeForenaviGate2 function in the ChtmlView class to pass the events in the HTML interface to the MFC background code.
There is also an event in HTML. There are a lot of event models in DHTML.
The occurrence of HTML events can be converted to a call to window.navigate (% line%). The MFC background code can intercept the onbeForenaviGate2 function to the message called by parameter% line%. With parameter% line% We can pass any parameters on any HTML to the MFC background code to facilitate program processing related users. For example, the event is passed to the background when the user clicks the OK button, and the content in the corresponding text box will also be passed to the background. The following is an example:
- Code in the page -
.
.
.
Function onbtnok () {
VAR txt = txtbox.value; // the line from textbox
WINDOW.NAVIGATE ("App: 1005 @" txt);
// "App: 1005 @" - this is The MFC Code Command Prefix.
// txt - Data can be transmitted along with the evenet.
}
Script>;
.
.
.
// The Button Has An Event Handler - The OnbTnok () Script Function
.
.
.
Body>
Html>
- The corresponding processing code in the MFC background -
Void Chtmlctrl :: OnBeforenaviGate2 (LPCTSTSTR LPSZURL,
DWORD NFLAGS,
LPCTSTR LPSZTARGETFRAMENAME,
CByteArray & baposteddata,
LPCTSTR LPSZHEADERS,
BOOL * PBCANCEL)
{
Const char App_Protocol [] = "app:";
INT LEN = _TCSLEN (App_Protocol);
IF (_TCSNICMP (LPSZURL, APP_PROTOCOL, LEN) == 0) {
// There is a specific Application's Reaction there.
ONAppCMD (LPSZURL LEN);
// Event Cancellation, Otherwise An Error Will Occur. * Pbcancel = True;
}
ChtmlView :: OnBeforenaviGate2 (LPSZURL, NFLAGS,
LPSZTARGETFRAMENAME, BAPOSTEDDATA,
Lpszheaders, Pbcancel;
}
Because in addition to the HTML interface, the MFC itself may become an event source, so there must be corresponding MFC code to pass the data to the HTML interface. In order to achieve this requirement, we can use an approach to call the script function in the HTML, which will pass the data to be passed to these functions. This good idea is made by Eugene Khodakovsky.
Here is an example:
Void Chtmlctrl :: OndocumentComplete (LPCTSTR LPSZURL)
{
.
.
.
HRESULT HR;
HR = gethtmldocument () -> queryinterface (IID_IHTMLDocument,
(void **) & m_pdocument);
IF (! successted (hr)) {
m_pdocument = NULL;
Return;
}
Idispatch * DISP;
m_pdocument-> get_script (& DISP); // Get Script Object
.
.
.
}
- MFC code used to call HTML script functions and pass parameters -
.
.
.
CstringArray strRay;
Strarray.Add ("Parameter 1");
Strarray.Add ("Parameter 2");
Strarray.Add ("Parameter 3");
// the call of "setParameters" Function
// from the script, (Passing Array Of Strings)
M_htmlctrl.calljscript2 ("setParameters", Strarray;
// inside the calljscript2 function:
// getidsofnames () - get the id number of the script function
// invoke () - Call the script-function by the number
.
.
.
The HTML script is a powerful and easy-to-use tool, and the interface of the entire program and the corresponding user operation response can be done. Use this web-style interface to make programming easier. This approach to the response and processing of the MFC to the user interface event to the HTML script. HTML's writing and programming of the program background has a benefit: Even if HTML code is changed, you can also avoid recompilation of the MFC program code. (Here, you need to explain it, don't recompile the code, but the link resource is still needed to re-perform