Solved

Save CEdit Content as RTF File

Posted on 2007-11-16
4
323 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

Technology Partners: 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

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…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

631 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