如何在CRichEditCtrl控件中直接读如RTF格式的文件

如何在CRichEditCtrl控件中直接读如RTF格式的文件

           Inserting an RTF string using StreamIn

--------------------------------------------------------------------------------

When inserting Rich Text Formatted text into the control there are two approaches you can take. Insert the control into the text, then select it and then format it. This can result in a lot of code and not very clean code at that. The other approach is that you can format the text into a CString variable and insert that in one fell swoop. This is much faster and reduces the number of lines of code.
Step 1: Define the EditStreamCallBack() callback function
When we stream in data into the rich edit control, we have to define a callback function that is called by the control to supply the actual data. This callback function can be called repeatedly by control till the function indicates that there is no more data.
One of the arguments passed to the callback function is an application defined value. We will use this value to pass in a CString object's address. The second argument is the address of the buffer where the data is to be written by the function, the third argument specifies the number of bytes requested by the rich edit control. The final argument is pointer to a long value. The callback function should set this value to the number of bytes actually copied to the buffer. If this value is less than the number of bytes requested by the control, then the control assumes that there is no more text available and it will stop calling this function.

We have defined the EditStreamCallBack() function as a file static function. This makes the function local to the file. We can define a function with the same name in another file. We could have defined this function as a class function but it would have had to be a class static function. Note the type CALLBACK. Forgetting to specify this can be cause for major headaches.

If you are a performance freak, you'll notice that this function is not very efficient for long strings. Also, this function modifies the string. You might want to enhance this if it is important to your application. For strings less than about 4KB this function is OK since it gets called only once.

static DWORD CALLBACK EditStreamCallBack(DWORD dwCookie, LPBYTE pbBuff, LONG cb,
     LONG *pcb)
{
 CString *pstr = (CString *)dwCookie;

 if( pstr->GetLength() < cb )
 {
  *pcb = pstr->GetLength();
  memcpy(pbBuff, (LPCSTR)*pstr, *pcb );
  pstr->Empty();
 }
 else
 {
  *pcb = cb;
  memcpy(pbBuff, (LPCSTR)*pstr, *pcb );
  *pstr = pstr->Right( pstr->GetLength() - cb );
 }
 return 0;
}

Step 2: Call StreamIn() with the right arguments
When inserting the RTF string, the information in the string should be complete otherwise it could mess up the formatting of the text in the control. That is, the string should contain the font information, the tab stops, the language, the font size etc. I won't go into the RTF format codes but the format code used in the sample code below should not be very difficult to decifer.
To build the RTF string, we use a prefix string with the preliminary information such as the font table, font size etc. We append our text to this string and at the end we add the postfix string, that completes the RTF string.

Here's the code snippet that calls the StreamIn() function. Note that in the call to StreamIn(), the first argument is a combination of SF_RTF and SFF_SELECTION. The first flag indicates that the text inserted into the rich edit control contains rich text formatting. The second flag indicates that the control should replace the selection with the inserted text. If you don't specify the SFF_SELECTION flag any previous text in the rich edit control will be cleared out.

The second argument to the function is the EDITSTREAM structure. This structure has three members. The first member is simply a value that is passed on to the callback function we defined. This member will contain the address of our string variable. The second argument is the error code returned by EditStreamCallBack() via the StreamIn() function. The last member is a pointer to EditStreamCallBack() - the callback function.

 CString rtfPrefix, rtfPostfix;
 rtfPrefix = "{//rtf1//ansi//deff0//deftab720{//fonttbl{//f0//froman "
  "Times New Roman;}}/n{//colortbl//red0//green0//blue0;}/n"
  "//deflang1033//pard//tx360//tx720//tx1080//tx1440//tx1800"
  "//tx2160//tx2520//tx2880//tx3240//tx3600//tx3960//tx4320"
  "//tx4680//tx5040//tx5400//tx5760//tx6120"
  "//tx6480//plain//f3//fs20 ";
 rtfPostfix = "/n//par }";


 // The rtfString contains the word Bold in bold font.
 CString rtfString = rtfPrefix + "//b Bold//b0" + rtfPostfix;

 EDITSTREAM es = {(DWORD)&rtfString, 0, EditStreamCallBack};

 // richEd is the rich edit control
 richEd.StreamIn(SF_RTF | SFF_SELECTION, es);

 

#1楼   2008-03-28 17:16 zhyang [未注册用户]
如果rtf中有图片的话,
可以参考CRichEditDoc::UpdateObjectCache()及CRichEditCntrItem的实现。

CRichEditView::OnTimer()会调用它们。见MSDN的WORDPAD例子。
  
回复    引用      
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
VB访问word书签。 '实现代码如下 Dim cn As New ADODB.Connection Dim AdoRs As New ADODB.Recordset Dim WordTemps As New Word.Application Private Sub Form_Load() If cn.State = 1 Then cn.Close End If cn.CursorLocation = adUseClient cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\db1.mdb" End Sub '开始导出数据 Private Sub Command1_Click() Dim strSQl As String Dim REC As Integer Dim i As Integer WordTemps.Documents.Add App.Path + "\货物合同.doc", False WordTemps.Selection.GoTo wdGoToBookmark, , , "合同标题" WordTemps.Selection.TypeText "关于冬季货物的成交合同" WordTemps.Selection.GoTo wdGoToBookmark, , , "合同编号" WordTemps.Selection.TypeText "2004000001" WordTemps.Selection.GoTo wdGoToBookmark, , , "签约单位" WordTemps.Selection.TypeText "宏大科技公司,天天科技公司" WordTemps.Selection.GoTo wdGoToBookmark, , , "签约地址" WordTemps.Selection.TypeText "北京关村大厦" WordTemps.Selection.GoTo wdGoToBookmark, , , "签约时间" WordTemps.Selection.TypeText fromat(Now, "yyyy-mm-dd") strSQl = "select * from Matrixs" AdoRs.Open strSQl, cn, adOpenKeyset, adLockOptimistic REC = AdoRs.RecordCount If REC < 1 Then MsgBox "无商品记录!", vbOKOnly, "提示" AdoRs.Close Exit Sub Else AdoRs.MoveFirst WordTemps.Selection.GoTo wdGoToBookmark, , , "货物清单" For i = 1 To REC WordTemps.Selection.TypeText AdoRs!名称 WordTemps.Selection.MoveRight unit:=wdCharacter, Count:=1 '右移一格 WordTemps.Selection.TypeText AdoRs!数量 WordTemps.Selection.MoveRight unit:=wdCharacter, Count:=1 '右移一格 WordTemps.Selection.TypeText AdoRs!规格 AdoRs.MoveNext If AdoRs.EOF = False Then WordTemps.Selection.InsertRowsBelow 1 '表格换行 End If Next i AdoRs.Close WordTemps.Visible = True '显示WORD窗口 End If End Sub '实现代码如下 Dim cn As New ADODB.Connection Dim AdoRs As New ADODB.Recordset Dim WordTemps As New Word.Application Private Sub Form_Load() If cn.State = 1 Then cn.Close End If cn.CursorLocation = adUseClient cn.Open "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\db1.mdb" End Sub '开始导出数据 Private Sub Command1_Click() Dim strSQl As String Dim REC As Integer Dim i As Integer WordTemps.Documents.Add App.Path + "\货物合同.doc", False WordTemps.Selection.GoTo wdGoToBookmark, , , "合同标题" WordTemps.Selection.TypeText "关于冬季货物的成交合同" WordTemps.Selection.GoTo wdGoToBookmark, , , "合同编号" WordTemps.Selection.TypeText "2004000001" WordTemps.Selection.GoTo wdGoToBookmark, , , "签约单位" WordTemps.Selection.TypeText "宏大科技公司,天天科技公司" WordTemps.Selection.GoTo wdGoToBookmark, , , "签约地址" WordTemps.Selection.TypeText "北京关村大厦" WordTemps.Selection.GoTo wdGoToBookmark, , , "签约时间" WordTemps.Selection.TypeText fromat(Now, "yyyy-mm-dd") strSQl = "select * from Matrixs" AdoRs.Open strSQl, cn, adOpenKeyset, adLockOptimistic REC = AdoRs.RecordCount If REC < 1 Then MsgBox "无商品记录!", vbOKOnly, "提示" AdoRs.Close Exit Sub Else AdoRs.MoveFirst WordTemps.Selection.GoTo wdGoToBookmark, , , "货物清单" For i = 1 To REC WordTemps.Selection.TypeText AdoRs!名称 WordTemps.Selection.MoveRight unit:=wdCharacter, Count:=1 '右移一格 WordTemps.Selection.TypeText AdoRs!数量 WordTemps.Selection.MoveRight unit:=wdCharacter, Count:=1 '右移一格 WordTemps.Selection.TypeText AdoRs!规格 AdoRs.MoveNext If AdoRs.EOF = False Then WordTemps.Selection.InsertRowsBelow 1 '表格换行 End If Next i AdoRs.Close WordTemps.Visible = True '显示WORD窗口 End If End Sub end
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值