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

原文地址:https://www.cnblogs.com/blogpro/p/11426850.html

时间: 2024-10-10 00:18:23

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

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

黄色的是wpf控件的名称! //保存到特定路径            FileStream fs = new FileStream(@"C:\image.png", FileMode.Create);            //对象转换成位图            RenderTargetBitmap bmp = new RenderTargetBitmap((int)this.mediaElement1.ActualWidth, (int)this.mediaElement1.Act

MFC中如何在TreeCtrl控件中获取鼠标左键消息时的有效点击位置

MFC中,当在TreeCtrl控件中添加鼠标左键消息时,就意味着,凡是在该控件中点击左键后即会响应鼠标左键的消息,可有时我们只想记下有效的鼠标左键点击的事件,如图所示: 只想在CheckBox中点击鼠标左键时,将已选中的Item的句柄存入到链表中,那么如何只将有效的鼠标左键的消息存入链表中呢? 下面是响应鼠标左键消息时从所有鼠标左键消息都响应到只响应有效的左键消息的三个版本: 当响应鼠标左键的消息时,第一个版本: 获取鼠标的点击位置(注意:此时获取的是针对于当前屏幕的鼠标点击位置,所以需要将其转

[转载]C#如何在webBrowser1控件通过TagName,Name查找元素(没有ID时)遍历窗体元素

//防止页面多次刷新页面执行 if (num == 1) { string GetUserName = System.Configuration.ConfigurationSettings.AppSettings["Y2000UserName"].ToString(); string GetUserPassword = System.Configuration.ConfigurationSettings.AppSettings["Y2000UserPassword"

C#在listview控件中显示数据库数据

一.了解listview控件的属性 view:设置为details columns:设置列 items:设置行 1.将listview的view设置为details 2.设置列属性 点击添加,添加一列 设置一列的Text属性,这就是列名 添加三列 3.编辑items属性,添加一行数据 编辑Text属性,添加一行的第一个数据 编辑subitems属性,添加一行中的其他数据 添加两个数据 填写结果 二.在listview中显示数据库数据 //在listview中显示数据库数据 private voi

[Android] 拍照、截图、保存并显示在ImageView控件中

最近在做Android的项目,其中部分涉及到图像处理的内容.这里先讲述如何调用Camera应用程序进行拍照,并截图和保存显示在ImageView控件中以及遇到的困难和解决方法. PS:作者购买了本<Android第一行代码 著:郭霖>,参照里面的内容完成(推荐该书,前面的布局及应用非常不错).网上这类资料非常多,作者仅仅分享给初学者同时在线记录些内容,希望对大家有所帮助. 首先,设置activity_main.xml为LinearLayout布局且 android:orientation=&q

C# dataGridView控件中加入comboBox控件及注意事项

DataGridViewComboBoxColumn pCombo; private void Teaching_Add_Load(object sender, EventArgs e) { MyDBase DB = new MyDBase(DBUser.sserver,DBUser.DBName, DBUser.suser, DBUser.spasswd); DataSet DS= DB.GetRecordset("select * from view_teach_tmp"); da

C#跨进程读取listview控件中的数据

http://www.cnblogs.com/Charltsing/p/slv32.html 欢迎交流:QQ564955427 读取标准的32位listview控件中的数据,网上已经有很多代码了.今天有空也做了个测试.读取TcpEye软件中的数据. 具体见程序附件.  下载(本程序可以自己修改主窗体类名和子窗体类名,抓取其他的软件数据.当然,窗体层次必须是2层,而且不能有多个同类名的子窗体,因为这只是个示例程序) *****************************************

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

DataGridView:显示数据表,通过此控件中可以实现连接数据库,实现数据的增删改查 一.后台数据绑定:    List<xxx> list = new List<xxx>();      dataGridView1.DataSource = list;      //设置不自动生成列,此属性在属性面板中没有      dataGridView1.AutoGenerateColumns = false;      //取消加载默认选中第一行      dataGridView1

【转】PropertyGrid控件中的多级显示

运行效果: 解决方案: MainForm.cs public partial class MainForm : Form { public MainForm() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { CProvincialCapital proCap = new CProvincialCapital("南京市", 10000000); CProvince pro