Create a web-style graphical user interface in MFC programming (2)

Source: Internet
Author: User

HTML window event processing

What is missing in the chtmlview class?

TypicalProgramThe processing script assumes that the event or data input can be received from each element (such as a button) of the program interface. Therefore, in our program, we also need to solve the problem of information interaction between the HTML interface and the MFC background program. We don't have to worry about it. In fact, this problem is not complicated. We can use the onbeforenavigate2 function in the chtmlview class to pass the HTML interface events to the MFC background.Code.

The event concept also exists in HTML. There may be a large number of event models in DHTML.

HTML events can be converted into calls to window. navigate (% line %. The MFC background code can intercept messages called with the parameter % line % in the onbeforenavigate2 function. With the % line % parameter, we can pass any HTML parameters to the MFC background Code to facilitate the program to process related user operations. For example, when the user clicks the OK button, the event will be passed to the background, and the content in the corresponding text box will also be passed to the background. The following is an instance:

-- Code in the webpage --

... & Lt; script language = <SPAN class = "CPP-string"> "jscript" </span> & gt; <SPAN class = "CPP-keyword"> function </span> onbtnok () {<SPAN class = "CPP-keyword"> var </span> TXT = txtbox. value; <SPAN class = "CPP-comment"> // the line from textbox </span> window. navigate (<SPAN class = "CPP-string"> "app: 1005 @" </span> + txt ); <SPAN class = "CPP-comment"> // "app: 1005 @"-this is the MFC code command prefix. </span> <SPAN class = "CPP-comment"> // TXT-data can be transmitted along with the event. </SPAN >}& lt;/script & gt; & lt; Body & gt ;... & lt; input type = text style = <SPAN class = "CPP-string"> "width: 50" </span> id = txtbox & gt; & lt; input type = button value = <SPAN class = "CPP-string"> "OK" </span> onclick = <SPAN class = "CPP-string"> "onbtnok () "</span> style = <SPAN class =" CPP-string ">" width: 45% "</span> & gt; <SPAN class = "CPP-comment"> // The button has an event handler-The onbtnok () Script Function </span>... & lt;/Body & gt; & lt;/html & gt;
  -- the corresponding processing code of the MFC background --  
<SPAN class = "CPP-keyword"> void </span> chtmlctrl, bool * pbcancel) {<SPAN class = "CPP-keyword"> const </span> <SPAN class = "CPP-keyword"> char </span> app_protocol [] = <SPAN class =" CPP-string ">" app: "</span>; <SPAN class =" CPP-keyword "> int </span> Len = _ tcslen (app_protocol ); <SPAN class = "CPP-keyword"> If </span> (_ tcsnicmp (lpszurl, app_protocol, Len) ==< SPAN class = "CPP-literal"> 0 </span>) {<SPAN class = "CPP-comment"> // there is a specific application's reaction there. </span> onappcmd (lpszurl + Len); <SPAN class = "CPP-comment"> // event cancellation, otherwise an error will occur. </span> * pbcancel = true;} chtmlview: onbeforenavigate2 (lpszurl, nflags, lpsztargetframename, baposteddata, lpszheaders, pbcancel );}
 

In addition to the HTML interface, MFC itself may also become the event source, so there must be appropriate MFC code that can pass data to the HTML interface. To meet this requirement, we can call the script functions in HTML to pass the data to these functions as parameters. This idea was proposed by Eugene hodakovsky.

 

The following is an example:

 
VoidChtmlctrl: ondocumentcomplete (lpctstr lpszurl) {... hresult hr; HR = gethtmldocument ()-> QueryInterface (iid_ihtmldocument ,(Void**) & M_pdocument );If(! Succeeded (HR) {m_pdocument = NULL;Return;} Idispatch * disp; m_pdocument-> get_script (& disp );// Get script object...}

-- The MFC code used to call HTML Script functions and PASS Parameters --

 
... Cstringarray strarray; 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...

HTML Script is a powerful and easy-to-use tool. It can be used for the entire program interface and corresponding user operation responses. This web-style interface makes programming easier. In this way, the MFC responds to and processes user interface events to process HTML scripts. There is one benefit of separating HTML coding from program background writing: Even if HTML code is changed, you can avoid re-compiling the MFC program code. (The Code does not need to be re-compiled, but the link resource step must be re-compiled)

Use the chtmldialog class to create a dialog window and display HTML

In addition to the main window, most applications also need to create other session windows. These session windows may also require complicated interface design, not only in appearance, but also in response to user operations. Therefore, it is necessary to use DHTML to design user interfaces in these areas. For details about how to use the cview derived class to display the content of the session window, Paul dilascia provides a solution in msdn. After improving the chtmlview, we need to use the cview derived class chtmldialog to display the dialog window content. Chtmlclass solves two problems for us: Setting the name and size of the dialog window. The specific parameters are described on the HTML page.

Steps for using the chtmldialog class:

Insert the dialog into the resource file and display the static elements in the dialog interface (these elements will be replaced by HTML elements). Then, create an MFC base class (derived from cdialog ).

In the header file, a class (such as dlg4.h) is derived from chtmldialog ).

// Inheriting the class from chtmldialog
Class cdlg4: Public chtmldialog
{
// Construction
Public:

Make sure that the base class chtmldialog constructor runs (such as dlg4.cpp) before running the constructor of the derived class cdlg4 ).

Cdlg4: cdlg4 (cwnd * pparent/* = NULL */)
: Chtmldialog (cdlg4: IDD, pparent, idr_html4,
Idc_static1) // The HTML page resource Transmission
{
// {Afx_data_init (cdlg4)
// Note: The classwizard will add member initialization here
//} Afx_data_init
}

The chtmldialog class also allows you to change the size of the session box. See the following two screens.

After reading the previous notes, it seems that all the problems have been solved? No, there are two other issues that leave us some little trouble.

1. When the HTML interface displayed in the window is used, setting the external properties of the window through the setwindowlong function does not take effect.

2. When the user changes the IE settings, that is, the HTML format display settings, the program interface will change.

If most people can tolerate the first question, the second question is unacceptable. This will lead to a different appearance of our program under different user iesettings.

For the second problem, there is also a more advanced solution called Advanced hosting interfaces (AHI ). Ethan akhgari provides us with good examples to introduce this better solution.

Http://www.codeproject.com/dialog/web_gui.asp

Note:

I have translated this article as a good expert who will learn how to program the interface.ArticleI would like to share with you, but what is counterproductive is that my English level is really too limited. Maybe I cannot even understand my translation. Therefore, the original address is attached here:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.