Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Save CEdit Content as RTF File

Posted on 2007-11-16
4
Medium Priority
?
327 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 2000 total points
ID: 20297709
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:Deepu Abraham
ID: 20298424
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
ID: 20299768
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
ID: 31409537
I code it myself, this was really the best way. Thanks!
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
Article by: Nadia
Linear search (searching each index in an array one by one) works almost everywhere but it is not optimal in many cases. Let's assume, we have a book which has 42949672960 pages. We also have a table of contents. Now we want to read the content on p…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

963 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