Click here to Skip to main content
15,884,962 members
Articles / Desktop Programming / MFC
Article

An example editor with table and image support

Rate me:
Please Sign up or sign in to vote.
3.28/5 (15 votes)
14 Jul 20051 min read 152.3K   4.9K   52   44
Use the new version rich edit control 4.1 to support table and image in your editor.

Sample screenshot

Introduction

TapEditor is just an editor application to show how to using the new documented msfedit.dll to support our editor to add a table.

There are some new features that the rich edit control has supported in this new version. Table is one of them. Now we use the code below to help your editor support table. For more information about richedit control 4.1, you can follow this link.

Using the code

  1. Create an editor where the base View is CrichEditView.
  2. In the InitInstance() function of your editor, add the following code to load msfedit.dll:
    // Initialize RichEdit control
    if (LoadLibrary(_T("MSFTEDIT.DLL")) == NULL)
    {
        AfxMessageBox(_T("Could not load the RichControl Dll."), 
                                      MB_OK|MB_ICONEXCLAMATION);
        return FALSE;
    }
  3. In the PreCreateWindow function of the View, specify like this to tell the View to create the class RICHEDIT50W of the new richedit control version:
    BOOL CTapEditorView::PreCreateWindow(CREATESTRUCT& cs)
    {
        BOOL bRes = CRichEditView::PreCreateWindow(cs);
        cs.style |= ES_SELECTIONBAR;
        cs.lpszClass = szClassRE; // Create RICHEDIT50W class
        return bRes;
    }

    Please add this code in the top of the CPP View file.

    static const TCHAR szClassRE[] = TEXT("RICHEDIT50W");
  4. Now to insert table, add a new function like OnInsertTable for example, and specify code like this:
    void CTapEditorView::OnInsertTable() 
    {
        CInsertTableDlg dlg;
    
        if(dlg.DoModal() == IDCANCEL)
            return;
    
        int rows = dlg.m_nRows,
        cols = dlg.m_nColumns;
    
        CString s = "{\\rtf1";
        CString s = "{\\rtf1";
        CString sTable = s + 
            "file://ansi//ansicpg1252//deff0//deflang1033{//fonttbl"
            "{\\f0\\froman\\fprq2\\fcharset0 Times New Roman;}"
            "{\\f1\\fswiss\\fcharset0 Arial;}}"
            "{\\*\\generator Msftedit 5.41.15.1503;}\\viewkind4\\uc1";
        CString row,col;
        row = "file://trowd//trgaph108//trleft8//"  
              "trbrdrl//brdrs//brdrw10 file://trbrdrt//"
              "brdrs\\brdrw10 file://trbrdrr//brdrs//" 
              "brdrw10 file://trbrdrb//brdrs//brdrw10 \\"
              "trpaddl108\\trpaddr108\\trpaddfl3\\trpaddfr3";
        col = "file://clbrdrl//brdrw10//brdrs//clbrdrt//brdrw10//brdrs//clbrdrr//"
            "brdrw10\\brdrs\\clbrdrb\\brdrw10\\brdrs\\cellx";
        CString endcell = "file://cell/";
        CString endrow = "file://row/";
        int i,j;
        int width = 8748/cols;
        CString sColw;
     
        // Loop for numbers of rows
        for(i=0;i<rows;i++)
        {
            sTable += row;
    
            // Loop for number of columns
            for(j=0;j<cols;j++)
            {
                sTable += col;
                sColw.Format(_T("%d"),width *(j+1));
                sTable += sColw;
            }
            sTable += "file://pard//intbl";
            for(j=0;j<cols;j++)
            {
                sTable += endcell;
            }
            sTable += endrow;
        }
        sTable += "file://par}";/
    
    #ifdef _UNICODE
        LONG len = sTable.GetLength() * 2;
        char* data = new char[len + 1];
        ClearString(sTable, data);
        SETTEXTEX st;
        st.codepage = 1200; 
        st.flags = ST_SELECTION | ST_KEEPUNDO;
        SendMessage(EM_SETTEXTEX, (WPARAM)&st, (LPARAM)(LPCTSTR)data);
        delete data;
    #else
        SetTextEX(m_hWnd, sTable, ST_SELECTION|ST_KEEPUNDO, 1200);
    #endif
    
    }
  5. To insert image, do again like above with the new function OnInsertImage, with the code below:
    void CTapEditorView::OnInsertImage()
    {
        CString sFilter = "All image file|*.bmp;*.jpg;*.gif|"
           "Bitmap Files (*.bmp)|*.bmp|JPEG Files (*.jpg)|*.jpg|"
           "GIF Files (*.gif)|*.gif|";
        CFileDialog dlg(TRUE, NULL, NULL, 
           OFN_FILEMUSTEXIST|OFN_READONLY, sFilter);
        if(dlg.DoModal() == IDOK)
        {
            CTapBitmap bmp;
            if(bmp.Load(dlg.GetPathName())==FALSE)
            {
                AfxMessageBox(_T("Could not load image."));
                return;
            }
            CEnBitmap enBitmap;
            CBitmap Bitmap;
            if (enBitmap.Attach(bmp.GetBMP(), 0))
            {
                Bitmap.DeleteObject();
                Bitmap.Attach(enBitmap.Detach());
    
                IRichEditOle *pRichEditOle;
                pRichEditOle = GetRichEditCtrl().GetIRichEditOle();
                HBITMAP hBitmap = (HBITMAP)Bitmap;
                if(hBitmap)
                {
                    CImageDataObject::InsertBitmap(pRichEditOle, hBitmap);
                }
            }
        }
    }
  6. Finally don’t forget to include the headers file.

    Include tapex.h file for inserting table.

    #include "tapex.h"

    And include TapBitmap.h, EnBitmap.h and ImageDataObject.h for inserting image.

    #include "TapBitmap.h"
    #include "EnBitmap.h"
    #include "ImageDataObject.h"

This example applies for VC++ 6 so you don’t need to add the richedit.h header file if you don’t want to have errors.

If you use Visual Studio .NET or VC++ 7, you don’t need to use tapex.h and tapex.cpp files. For more information, please use this link.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Vietnam Vietnam
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: How to directly output the table Pin
taphan10-Aug-05 0:45
taphan10-Aug-05 0:45 
GeneralRe: How to directly output the table Pin
qqz10-Aug-05 4:19
qqz10-Aug-05 4:19 
GeneralRe: How to directly output the table Pin
taphan10-Aug-05 16:19
taphan10-Aug-05 16:19 
GeneralHave some bugs Pin
Schniddel17-Jul-05 0:27
Schniddel17-Jul-05 0:27 
GeneralRe: Have some bugs Pin
taphan17-Jul-05 17:52
taphan17-Jul-05 17:52 
GeneralRe: Have some bugs Pin
liberdong6-Aug-06 7:40
liberdong6-Aug-06 7:40 
GeneralRe: Have some bugs Pin
linkeryang21-Dec-09 19:45
linkeryang21-Dec-09 19:45 
AnswerRe: Have some bugs Pin
Member 335764019-Jan-11 6:53
Member 335764019-Jan-11 6:53 
GeneralYour Links and Image doesn't work Pin
Schniddel15-Jul-05 7:02
Schniddel15-Jul-05 7:02 
GeneralRe: Your Links and Image doesn't work Pin
Steve Mayfield15-Jul-05 7:50
Steve Mayfield15-Jul-05 7:50 
GeneralRe: Your Links and Image doesn't work Pin
Schniddel15-Jul-05 7:55
Schniddel15-Jul-05 7:55 
GeneralRe: Your Links and Image doesn't work Pin
Steve Mayfield15-Jul-05 7:59
Steve Mayfield15-Jul-05 7:59 
GeneralTechnical problems? Pin
fwsouthern15-Jul-05 8:31
fwsouthern15-Jul-05 8:31 
GeneralRe: Technical problems? Pin
Steve Mayfield15-Jul-05 8:58
Steve Mayfield15-Jul-05 8:58 
GeneralRe: Your Links and Image doesn't work Pin
Schniddel15-Jul-05 8:40
Schniddel15-Jul-05 8:40 
GeneralRe: Your Links and Image doesn't work Pin
Chetan Sheladiya11-Jul-06 0:11
professionalChetan Sheladiya11-Jul-06 0:11 
GeneralPlease edit before posting Pin
fwsouthern15-Jul-05 3:27
fwsouthern15-Jul-05 3:27 
GeneralRe: Please edit before posting Pin
Steve Mayfield15-Jul-05 8:02
Steve Mayfield15-Jul-05 8:02 
GeneralRe: Please edit before posting Pin
taphan15-Jul-05 15:45
taphan15-Jul-05 15:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.