我在dialog中用了两个richedit控件,我想从richedit1获得rtf格式的数据,把获得的数据写入richedit2中,怎么做,请出示具体代码。

解决方案 »

  1.   


    BOOL   COleRichEditCtrl::CopyFile(   
                                      const   char   *lpSrcFilename,   
                                      const   char   *lpDestFilename,   
                                      int   nBuffersize   )   
    {   
        CFile   Out,   In;   
        int   Size,nFilesize,total_size;   
        char   *lpBuffer;       //   Attempt   to   open   the   input   file.   
        if(!In.Open(lpSrcFilename,CFile::modeRead)){   
            AfxMessageBox("Could   not   open   the   input   file."   );   
            return(FALSE);   
        }       //   Attempt   to   create   the   output   file.   
        if(!Out.Open(lpDestFilename,   
            CFile::modeWrite|CFile::modeCreate)   ){   
                AfxMessageBox(   "Could   not   open   the   output   file."   );   
                return FALSE;   
        }       //   Create   the   copy   buffer.   
        lpBuffer=new char [nBuffersize];   
        if(lpBuffer==NULL){   
            AfxMessageBox(   "Could   not   allocate   the   copy   buffer."   );   
            return(   FALSE   );   
        }       CFileStatus   rStatus;   
        In.GetStatus(   lpSrcFilename,   rStatus   );   
        nFilesize   =   In.GetLength();   
        total_size   =   nFilesize;   
        char   *   lpBuffer_total   =   new   char   [nFilesize];//xyh   
        Size=0;   
        while(nFilesize>0){           int nSize=nBuffersize;   
            if(nSize > nFilesize   )   
                nSize=nFilesize;           try{   
                In.Read(   lpBuffer,   nSize   );   
                //memcpy(lpBuffer,lpBuffer,nSize);   
                memcpy(lpBuffer_total+Size,lpBuffer,nSize);   
                Size=Size+nSize;   
                //lpBuffer_total=lpBuffer_total   +   nSize;   
            }   
            catch(CFileException   *e   ){   
                char *lpMsgBuf;   
                if(FormatMessage(   
                    FORMAT_MESSAGE_ALLOCATE_BUFFER   |   
                    FORMAT_MESSAGE_FROM_SYSTEM,   
                    NULL,   e->m_lOsError,   
                    MAKELANGID(LANG_NEUTRAL,   
                    SUBLANG_DEFAULT),   
                    (LPSTR)&lpMsgBuf,0,NULL) > 0 ){   
                        AfxMessageBox(lpMsgBuf);   
                        LocalFree(lpMsgBuf);   
                }               //   Free   the   exception   and   
                //   return.   
                e->Delete();   
                return FALSE;   
            }           e->Delete();   
            return  FALSE;   
        }   //while    nFilesize   -=   nSize;       try{   
            Out.Write(   lpBuffer_total,   total_size   );   
        }       catch(   CFileException   *e   ){           char   *lpMsgBuf;   
            if(   FormatMessage(   
                FORMAT_MESSAGE_ALLOCATE_BUFFER   |   
                FORMAT_MESSAGE_FROM_SYSTEM,   
                NULL,   e->m_lOsError,   
                MAKELANGID(   LANG_NEUTRAL,   
                SUBLANG_DEFAULT   ),   
                (LPSTR)   &lpMsgBuf,   0,   NULL   )   >   0   ){   
                    AfxMessageBox(   lpMsgBuf   );   
                    LocalFree(   lpMsgBuf   );   
            }           e->Delete();   
            return(   FALSE   );   
        }   
        //Out.Write(   lpBuffer_total,   total_size   );Õâ¸öÒ²¿ÉÒÔ   
        EDITSTREAM   es;   
        es.pfnCallback   =   readFunction;   
        es.dwError   =   0;   
        es.dwCookie   =   (DWORD)lpBuffer_total;   
        StreamIn(   SF_RTF,   es   );   
        //   Close   the   output   file   so   that   the   
        //   SetStatus()   function   won't   fail.   
        Out.Close();   
        CFile::SetStatus(   lpDestFilename,   rStatus   );       //   Delete   the   buffer.   
        delete   []lpBuffer;   
        delete   []lpBuffer_total;       return   TRUE;   }   //这是关键的回调函数   
    DWORD  CALLBACK  COleRichEditCtrl::readFunction(DWORD   dwCookie,   
                                                      LPBYTE   lpBuf, //   the   buffer   to   fill   
                                                      LONG   nCount, //   number   of   bytes   to   read   
                                                      LONG*   nRead) //   number   of   bytes   actually   read   
    {       char *pstr=(char *)dwCookie;    
        if((LONG)strlen(pstr)<nCount)   
        {   
            *nRead   =   (LONG)strlen(pstr);   
            memcpy(lpBuf,pstr,   *nRead);   
            //pstr->Empty();   
        }       else   
        {   
            LONG   nSize   =   0;   
            LONG   i   =   0;   
            LONG   j   =   0;   
            LONG   readBuff=(LONG)strlen(pstr);   
            //*nRead   =   nCount;   
            i=*nRead;   
            j=nCount;   
            memcpy(lpBuf,pstr+nSize,nCount);
            nSize   =   nSize   +   nCount;   
            i=*nRead;   
            j=nCount;   
            //readBuff=readBuff-nCount;   
            //*pstr=pstr->Right(pstr->GetLength()-nCount);   
        }       return   0;   
    }
      

  2.   

    可以参考http://hi.baidu.com/smlib/blog/item/1bd7193ea9bfcaf9828b13f9.html