Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Save CEdit Content as RTF File

Posted on 2007-11-16
4
Medium Priority
?
326 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

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

Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
In this post we will learn different types of Android Layout and some basics of an Android App.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

670 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