Selasa, 13 Mei 2008

HTML Editor for VC++ 6.0

Introduction
Today, applications often need a rich user interface. One of them is ability to compose or edit HTML documents in an easy, effective, and reliable way. This article and source code attached give you a nice control to perform this task. And now, the best part: This control works fine with Microsoft VC++ 6.0!
Background
I'm author of the Tray Helper application. For a long time, I wanted to add a feature to send colorful HTML mail. After some research, I found that Microsoft did a nice thing implementing IHTMLDocument2 interface (and a few others related to it), but working with raw COM interfaces is not very easy, effective, or programmer-friendly. I think someone from Microsoft also realized that and in MFC 7.0 there is a very nice wrapper for COM: CHtmlEditCtrlBase class. After examining code from this class, I decided to port it to MFC 4.0 and VC++ 6.0.
Using the Code
The CHtmlEditCtrl2 class I want to introduce there is not only ported from CHtmlEditCtrlBase—I did some other improvements and changes to the orginal code—so it not only compiles under VC++ 6.0 but also has some new features and is even easier to use in existing projects. The main change was that CHtmlEditCtrl2 class derives from CWebBrowser2. It means that if your projects already use a standard Web browser control to display a HTML documents, adding editing capabilities would be trivial (read below for full instructions).
Adding HTML Editor to Your Projects
Step 1: Adding an ordinary Web browser control to your project
If you already use a Web browser control in your project, skip this paragraph and start reading from "Step 2."
Let's assume that you want add a HTML edit control to your dialog-based application. First, you need to add a Web browser control that will contain your HTML documents.
Go to the dialog template editor of Visual Studio and click on "Project / Add to project / Components and controls". From the resulting pop-up dialog, select "Registered active X controls" and finally find a select "Web Browser control" (the name of the control depends on language of your PC; for example, on my Polish version of Windows it is "Przegladarka Przeglądarka sieci Web firmy Microsoft"). To be sure that you're selecting a correct control, check the description below the file list; it should be "WebBrowser control".
After selecting a control, a dialog box with the CWebBrowser2 class to be created should appear. Click "OK" without any modifications.
Note that the new position should be added to the toolbar with a list of controls available to insert into the dialog. So, select it and just add a Web browser!
Finally, use a class wizard (Ctrl+W) to add a member variable of type CWebBrowser2 to the newly addeded item.
Next, you can add an OnInitdialog method to your dialog and request a browser to load a HTML document using the Navigate method of the CWebBrowser2 class.
Step 2: Adding edit capabilities to the Web browser
It's VERY easy. Just follow those few steps:
Copy the HtmlEditCtrl2.H and HtmlEditCtrl2.cpp directories with your source files and add them to your project.
Modify the header file where you have a member variable of CWebBrowser2 declared and change it to CHtmlEditCtrl2.
Change #include "WebBrowser2.H" to #include "HtmlEditCtrl2.H".
After loading an HTML document (using, for example, the Navigate method) call the SetDesignMode(TRUE) method to enable edit mode.
If, after editing is complete, you want to get a source of the HTML document, call the GetDocumentHTML function.

more

Komentar: Posting Komentar

Berlangganan Posting Komentar [Atom]





<< Beranda

This page is powered by Blogger. Isn't yours?

Berlangganan Postingan [Atom]