Solved

Save CEdit Content as RTF File

Posted on 2007-11-16
4
315 Views
Last Modified: 2013-11-20
Hello,

I have a ownerdraw CEdit in my app. I can read out the data line by line. Now I want to save the data to a rtf file, cause I need a format where I can set a Fixed System Font like Courier.
How I can write a "text" buffer to a rtf File without using the rtf control?

Thanks

Ingo
0
Comment
Question by:Ocrana
4 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
Comment Utility
Indeed you don't need the Rich Text control at all.
RTF is a format similar to HTML, you can compose it by yourself by writting directly to a text file, including some formatting tags. If you want to know more about RTF format, have a look to:
http://en.wikipedia.org/wiki/Rich_Text_Format
http://desktoppub.about.com/od/textacquisition/a/richtextformat.htm
http://www.biblioscape.com/rtf15_spec.htm

But the best way to learn to use RTF is to use the Windows' WordPad, save in RTF format and read the file with the Notepad to see the codes.
0
 
LVL 11

Expert Comment

by:DeepuAbrahamK
Comment Utility
CRichEditCtrl will also help

Have a look at this article:
http://www.codeproject.com/richedit/rulerricheditctrl.asp?print=true

Best Regards,
DeepuAbrahamK
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
Use an 'invisible' RTF control for that purpose, e.g.

typedef struct  _tagRTFCNV
{
    CStdioFile* pfileIn;
    CStdioFile* pfileOut;
    CString     strLine;

}   RTFCNV, *PRTFCNV;

static
DWORD
CALLBACK
ReadStreamCallBack  (   DWORD   dwCookie,
                        LPBYTE  pbBuff,
                        LONG    cb,
                        LONG*   pcb
                    )
{
    BOOL    bRC =   TRUE;

    PRTFCNV pCnv    =   ( PRTFCNV) dwCookie;

    CStdioFile* pfile   =   pCnv->pfileIn;  

    if  (   pCnv->strLine.IsEmpty   ())
        {
            try
            {
                bRC =   pfile->ReadString   (   pCnv->strLine);
            }
            catch   (   CFileException* e)
            {

                e->Delete   ();

                return  (   1);
            }
   
        }

    if  (   pCnv->strLine.GetLength ()  <   cb)
        {
            *pcb    =   pCnv->strLine.GetLength();

            CopyMemory  (   pbBuff, ( LPCSTR) pCnv->strLine, *pcb);

            pCnv->strLine.Empty();  
        }  
     else  
        {
            *pcb    =   cb;

            CopyMemory  (   pbBuff, ( LPCSTR) pCnv->strLine,    *pcb);

            pCnv->strLine   =   pCnv->strLine.Right (       pCnv->strLine.GetLength ()  
                                                        -   cb
                                                    );
        }

    if  (   !bRC)  
            return  (   1);

    return  (   0);
}

static
DWORD
CALLBACK
WriteStreamCallBack (   DWORD   dwCookie,
                        LPBYTE  pbBuff,
                        LONG    cb,
                        LONG*   pcb
                    )
{  

    if  (   !cb)    return  (   1);

    PRTFCNV     pCnv    =   ( PRTFCNV) dwCookie;
    CStdioFile* pfile   =   pCnv->pfileOut;
    char*       pc  =   new char    [   cb  +   1];

    ZeroMemory  (   pc, cb  +   1);

    CopyMemory  (   pc, pbBuff, cb);

    pCnv->strLine   =   pc;


    try
    {
        pfile->WriteString  (   pCnv->strLine);
    }
    catch   (   CFileException* e)
    {

        e->Delete   ();
        delete  pc;

        return  (   1);
    }

    *pcb    =   cb;

    delete  pc;

    return  (   0);
}

HRESULT ConvertFile (   PRTFCNV     pCnv)
{
    long            lBytes;
    BOOL            bCtrlReady;

    CRichEditCtrl*  pCtrl;
    EDITSTREAM      esIn;
    EDITSTREAM      esOut;

    RECT            rect;

    ZeroMemory  (   &rect,  sizeof  (   RECT));

    pCtrl   =   new CRichEditCtrl   ();

    bCtrlReady  =   pCtrl->Create   (   WS_CHILD | ES_MULTILINE,
                                        rect,
                                        CWnd::GetForegroundWindow   (),
                                        4711
                                    );

    esIn.dwCookie       =   ( DWORD)    pCnv;
    esIn.pfnCallback    =   ReadStreamCallBack;

    esOut.dwCookie      =   ( DWORD)    pCnv;
    esOut.pfnCallback   =   WriteStreamCallBack;

    lBytes  =   pCtrl->StreamIn (   SF_RTF, esIn);


    if  (   !lBytes)
        {
            pCtrl->DestroyWindow    ();

            delete  pCtrl;

            return  (   E_FAIL);
        }

    lBytes  =   pCtrl->StreamOut    (   SF_TEXT,    esOut);


    if  (   !lBytes)
        {
            pCtrl->DestroyWindow    ();

            delete  pCtrl;

            return  (   E_FAIL);
        }

    pCnv->pfileOut->SeekToBegin ();

    pCtrl->DestroyWindow    ();

    delete  pCtrl;

    return  (   NOERROR);
}
0
 

Author Closing Comment

by:Ocrana
Comment Utility
I code it myself, this was really the best way. Thanks!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now