Basic use of CDHtmlDialog

Source: Internet
Author: User

This is because I am the only one in my department, and I am the only one in C ++ and others in C #), so I will discuss with the technical consultant how to present the client. He said that, based on previous experience in the previous system), the page is frequently upgraded. We recommend that you use embedded HTML to display the page. In this way, you only need to upgrade HTML later. After hearing this, I lost my teeth in half. After I dragged the dialog box for half a month, I gave it a question.

Then I began to look for methods for displaying HTML pages in MFC, found some custom HTML classes, and found Webbrowser Control. I haven't found the Control yet ), finally, the CDHtmlDialog class is locked.

This category has just begun to shine recently. In the vc6.0 era, it is hard to see what is made with this stuff. It is a win2k style. What kind of win7 style can be used now? Maybe the Metro style can be used in vs11 .. After a simple experiment, I found that it was still usable and I decided to use it.

Create a new MFC Application project. In this step, mark the HTML Dialog

650) this. width = 650; "src =" http://img1.51cto.com/attachment/201205/170610941.png "border =" 0 "alt =" "/> you can see that the dialog box is derived from the CDHtmlDialog class.

After the creation is complete, observe the differences between the Code and the previous one. Four points may be found ):

 
 
  1. class CTestHtmlDialogDlg : public CDHtmlDialog 

It indicates that the dialog box class does inherit CDHtmlDialog.

 
 
  1. // Dialog Data 
  2.     enum { IDD = IDD_TESTHTMLDIALOG_DIALOG, IDH = IDR_HTML_TESTHTMLDIALOG_DIALOG }; 

The enumeration here is changed to two, that is, there is a normal dialog box and an HTML dialog box.

 
 
  1. CTestHtmlDialogDlg::CTestHtmlDialogDlg(CWnd* pParent /*=NULL*/) 
  2.     : CDHtmlDialog(CTestHtmlDialogDlg::IDD, CTestHtmlDialogDlg::IDH, pParent) 

In the CTestHtmlDialogDlg constructor, A CTestHtmlDialogDlg: IDH is added to the member initialization list of CDHtmlDialog.

 
 
  1. ///////////////////////////////////////////////////////////////////////////// 
  2. // 
  3. // HTML 
  4. // 
  5.  
  6. IDR_HTML_TESTHTMLDIALOG_DIALOG    HTML         "TestHtmlDialog.htm" 

In the resourcefile, the following line is used to define the html file resource of the html dialog box. You can modify it to a specific name, such as index.html. Of course, the html file in the project must also be changed to the corresponding name.

Let's do a test. Modify testhtmldialog.htm.

 
 
  1. <HTML> 
  2. <HEAD> 
  3. </HEAD> 
  4. <BODY ID=CTestHtmlDialogDlg style="font-family:MS Shell Dlg;font-size:8"> 
  5.  
  6. </BODY> 
  7. </HTML> 

The result is as follows:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1A4424522-1.png "border =" 0 "alt =" "/>

How is it? It is much easier to drag a dialog box, drag a control, Load a resource, modify an ID number, associate a variable, and Load various shows?

I have a problem here. It has not been solved by the time of publication. Please reply if you have any suggestions. I can only use absolute path c: \ test.jpg, but cannot use relative paths. test.jpg ,.. // test.jpg ,.. /// Debug // test.jpgand so on, and I put test.jpg in the project directory, and it cannot be loaded. It always shows a small Red Cross ..

------------------------------- I am a gorgeous split line --------------------------------------

Continue our experiment. The page cannot be displayed only. What interaction do you have to make? So I'm glad that this feature also supports Javascript.

Use the HTML editor that comes with vs2010to edit testhtmldialog.htm. Here, I would like to like VS2010's HTML editor, which is very user-friendly and can basically be comparable to Dreamweaver. Double-click the button and you can directly write The onclick event. The default is Javascript.

We drag a button above, change the name, save and double-click the button to write the onclick event

 
 
  1. Function Button1_onclick (){
  2. Alert ("Hello, what are you doing ");
  3. }

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1A4422019-2.png "border =" 0 "alt =" "/>

The alert information is displayed.

------------------------------- I am a gorgeous split line --------------------------------------

Next I came up with the idea: Can Javascript call functions in the local dialog box? I went to Baidu to Google for a while and found that it was okay. The method is also relatively simple.

 
 
  1. // 1. Enable CTestHtmlDialog to support automation
  2. // Add it in OnInitDialog ()
  3. EnableAutomation ();
  4.  
  5. // 2. expose itself to Javascript
  6. // Add it in OnInitDialog ()
  7. SetExternalDispatch (GetIDispatch (TRUE); // set the extension interface of the browser control to the IDispatch of the dialog box.
  8.  
  9. // 3. Declare DISPATCH_MAP
  10. // In TestHtmlDialog. h
  11. DECLARE_DISPATCH_MAP ()
  12.  
  13. // 4. Define DISPATCH_MAP
  14. // In TestHtmlDialog. cpp
  15. BEGIN_DISPATCH_MAP (CMyDHtmlDialog, CDHtmlDialog)
  16. DISP_FUNCTION (CMyDHTMLDialog, "SayHello", func, VT_EMPTY, VTS_NONE)
  17. END_DISPATCH_MAP ()

We can see that it is a bit like the message ing mechanism of MFC.

Next, write a function:

 
 
  1. MessageBox(_T("SayHello Func"), _T("Hello~~")); 

Modify the button and click the event:

 
 
  1. Function Button1_onclick (){
  2. // Alert ("Hello, what are you doing ");
  3. External. SayHello ();
  4. }

Good. Compile and run it. Click the button to bring up something:

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1A4422113-3.png "border =" 0 "alt =" "/>

Because the program should be exposed internally, Windows thinks this may be insecure. Click "yes" and you will see the effect of the function you just wrote.

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/1A4423H5-4.png "border =" 0 "alt =" "/>

But I think it's safe. I don't want to see this thing .. What should I do? Find the solution.

In CTestHtmlDialog, The CanAccessExternal () function is reloaded.

 
 
  1. BOOL CWebbrowserDlg::CanAccessExternal() 
  2.     return TRUE; 

Now, the nasty security prompt is missing ~

------------------------------- I am a gorgeous split line --------------------------------------

Can I access Javascript Functions in the dialog box? Of course, you can. In this way, you can only use Javascipt to determine the validity of data, so that C ++ can focus on logical operations.

This information is relatively small. I have found one and it is not completely understood. Think about it for a moment, there is little need for this, so there is not much information. I found something in this aspect on codeproject. If you are interested, please wait and see ~ Time-Space Shuttle Portal

 

 

 

 

This article from the "front of the tradeford reverse tairuibao blog, please be sure to keep this source http://serious.blog.51cto.com/242085/864132

Related Article

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.