Solved

Save CEdit Content as RTF File

Posted on 2007-11-16
4
322 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 500 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:DeepuAbrahamK
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Permutation and Combination 9 132
Websocket through haproxy reused connection causes "Not a valid frame" error. 4 145
Why isn't object file created? 6 104
Problem to App source 6 116
One of Google's most recent algorithm changes affecting local searches is entitled "The Pigeon Update." This update has dramatically enhanced search inquires for the keyword "Yelp." Google searches with the word "Yelp" included will now yield Yelp a…
Prime numbers are natural numbers greater than 1 that have only two divisors (the number itself and 1). By “divisible” we mean dividend % divisor = 0 (% indicates MODULAR. It gives the reminder of a division operation). We’ll follow multiple approac…
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 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.

739 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