如何在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);
 

如何在CRichEditCtrl控件中直接读如RTF格式的文件(这个是通过流的方式来读取文件)的更多相关文章

  1. wpf 保存控件中的内容为图片格式

    黄色的是wpf控件的名称! //保存到特定路径            FileStream fs = new FileStream(@"C:\image.png", FileMod ...

  2. 如何在WindowsPhone Bing Map控件中显示必应中国中文地图、谷歌中国中文地图。

    原文:如何在WindowsPhone Bing Map控件中显示必应中国中文地图.谷歌中国中文地图. 最近正好有点业余时间,所以在做做各种地图.Bing Map控件本身就能显示必应地图,但是很遗憾微软 ...

  3. MFC 对话框Picture Control(图片控件)中静态和动态显示Bmp图片

    版权声明:本文为博主原创文章,转载请注明CSDN博客源地址! 共同学习,一起进步~ https://blog.csdn.net/Eastmount/article/details/26404733   ...

  4. 在MonthCalendar控件中选中日期

    Calendar.MONTH Calendar now=Calendar.getInstance();System.out.print(now.get(Calendar.MONTH));得到的月份少1 ...

  5. winform窗体(六)——DataGridView控件及通过此控件中实现增删改查

    DataGridView:显示数据表,通过此控件中可以实现连接数据库,实现数据的增删改查 一.后台数据绑定:    List<xxx> list = new List<xxx> ...

  6. Winform(DataGridView)控件及通过此控件中实现增删改查

    DataGridView:显示数据表,通过此控件中可以实现连接数据库,实现数据的增删改查 一.后台数据绑定:    List<xxx> list = new List<xxx> ...

  7. .net dataGridView当鼠标经过时当前行背景色变色;然后【给GridView增加单击行事件,并获取单击行的数据填充到页面中的控件中】

    1.首先在前台dataGridview属性中增加onRowDataBound属性事件 2.然后在后台Observing_RowDataBound事件中增加代码 protected void Obser ...

  8. 服务器控件中使用<%#...>, JS和html控件中使用<%=...>

    //在服务器控件的属性中,需要用<%#...>来绑定其他控件的ID, 并且要在页面初始方法中,执行Page.DataBind(); <asp:ImageButton ID=" ...

  9. 在DataGridView控件中加入ComboBox下拉列表框的实现

    在DataGridView控件中加入ComboBox下拉列表框的实现 转自:http://www.cnblogs.com/luqingfei/archive/2007/03/28/691372.htm ...

随机推荐

  1. 使用wireshark在windows平台下捕获HTTP协议数据包中的帐号密码信息

    1.打开wireshark软件,从Interface List中选择相应的网卡,例如我的PC机上是“本地连接”,然后选择”Start”启动抓包程序. 2.打开学校主页,输入账号和密码登录校内邮箱. 3 ...

  2. 【模板篇】Link Cut Tree模板(指针)

    网上一片一片的LCT都是数组写的 orz 用指针写splay的人想用指针写LCT找板子都不好找QAQ 所以能A题了之后自然要来回报社会, 把自己的板子丢上来(然而根本没有人会看) LCT讲解就省省吧, ...

  3. Mnesia动态添加节点杂记

    FAQ List: 1. 如果动态的添加一个节点到Mnesia cluster中 2. 如何动态的从mnesia cluster中删除一个节点 3. 在一个节点上演示将当前已有的表格分片fragmen ...

  4. python_django__验证码

    验证码:在用户注册/登陆时使用,为了防止暴力请求,减轻服务器压力,也是防止csrf的一种方式. 运行环境:python django 对应template模块htm函数: 登陆页面: <!DOC ...

  5. ubuntu 16.04 pecl 不能安裝 mcrypt

    vagrant@ubuntu-xenial:/etc/apt$ sudo pecl install mcrypt-1.0.1 downloading mcrypt-1.0.1.tgz ... Star ...

  6. Eclipse+Marven + spring mvc 新建一个 Hello world 项目

    1. 打开Eclipse,菜单 File->New->Marven Project.               2. 点击 Next,                3. 选择 marv ...

  7. ps使logo背景色透明

    方法一:魔法工具(对复杂的logo误差较大) 魔法工具--左键点击选区--delete--保存 方法二:拾色器 1.有的站上的素材图片不能直接用,需要先变成rgb图像,可这样操作:图像\模式,选择rg ...

  8. leetcode-264-丑数

    题目描述: 方法一:堆 O(nlogn) class Solution: def nthUglyNumber(self, n: int) -> int: import heapq heap = ...

  9. delphi 文件存取方法与文件管理组件

    9.2  文件存取方法与文件管理组件 9.2.1  存取文件的类方法 Delphi在许多需要与文件打交道的类中定义了文件存取方法,使用这些方法可以非常方便地将类中的数据保存到文件中,或从文件中读取所需 ...

  10. 【原理】LVM(Logical Volume Manager)动态卷管理

    一张图让你学会LVM   导读 随着科技的进步,人们不知不觉的就进入了大数据的时代,数据的不断增加我们发现我们的磁盘越来越不够用了,接下来就是令人头疼的事情--加硬盘,数据的备份与还原.LVM就是Li ...