RichTextBox扩展控件的中文乱码解决方案

在程序中有利用RichTextBox进行一些操作的需求,无意中在CodeProject中发现了一个封装比较完整的RichTextBox控件(http://www.codeproject.com/KB/edit/csexrichtextbox.aspx),控件封装的还是不错,测试界面效果如下:

 

总体来说,支持各种格式的定义以及图片的插入,效果还是相当不错,不过在实际中使用发现,用代码插入的中文内容会出现乱码。

解决方法一: 

由于本身对RichTextBox的应用以及RTF格式不是很熟悉,搜索很久不得要领,没有找到好的解决方案,后来发现有一个比较取巧的方法,就是重新利用RichTextBox的机制来生成RTF文档内容,然后传入RTF格式内容给控件实现,代码如下所示:

 

            RichTextBox rtb = new RichTextBox();
            rtb.Font 
= _font;
            rtb.Text 
= _text;
            AppendRtf(rtb.Rtf);

 

 

 上面部分代码用来替换

 public void InsertTextAsRtf(string _text, Font _font, RtfColor _textColor, RtfColor _backColor) 

 

 函数部分的this.SelectedRtf = _rtf.ToString(); 即可。

 修改后完整的函数代码如下所示:

        public void InsertTextAsRtf(string _text, Font _font, RtfColor _textColor, RtfColor _backColor) {
            StringBuilder _rtf 
= new StringBuilder();
            
// Append the RTF header
            _rtf.Append(RTF_HEADER);

            
// Create the font table from the font passed in and append it to the
            
// RTF string
            _rtf.Append(GetFontTable(_font));

            
// Create the color table from the colors passed in and append it to the
            
// RTF string
            _rtf.Append(GetColorTable(_textColor, _backColor));

            
// Create the document area from the text to be added as RTF and append
            
// it to the RTF string.
            _rtf.Append(GetDocumentArea(_text, _font));

            
//this.SelectedRtf = _rtf.ToString();

            RichTextBox rtb 
= new RichTextBox();
            rtb.Font 
= _font;
            rtb.Text 
= _text;
            AppendRtf(rtb.Rtf);
        }

 

 步骤二:

上面那种方法应该是一种取巧的方法,后来学习了几篇文章关于RichTextBox格式介绍,发现还可以通过修改里面的一些编码格式实现中文的处理效果的。

 RichTextBox格式相关的介绍文章如下所示:

 RTF文件格式研究报告(http://www.cnpopsoft.com/article.asp?id=11)

关于RTF(富文本格式)的使用(http://blog.sina.com.cn/s/blog_5d2a73550100bcth.html)

使用C# 编程对RTF 文档进行操作(http://www.webjx.com/htmldata/2007-10-22/1193038864.html)

使用RichTextBox的一点心得(http://blog.csdn.net/jiangxinyu/archive/2008/06/16/2552150.aspx)

 

看了以上文章,你会发现,文章介绍的ExRichTextBox控件的编码头部内容以及字体内容是设置成了支持英语(美国)以及ASCII码,所以中文无法显示正常导致的乱码,如果修改了RichTextBox的编码头部内容和字体内容,应该就没有问题了。原来的RichTextBox头部内容如下:

         /* RTF HEADER

         * ----------
         * 
         * \rtf[N]        - For text to be considered to be RTF, it must be enclosed in this tag.
         *                  rtf1 is used because the RichTextBox conforms to RTF Specification
         *                  version 1.
         * \ansi        - The character set.
         * \ansicpg[N]    - Specifies that unicode characters might be embedded. ansicpg1252
         *                  is the default used by Windows.
         * \deff[N]        - The default font. \deff0 means the default font is the first font
         *                  found.
         * \deflang[N]    - The default language. \deflang1033 specifies US English.
         * 
*/
        
private const string RTF_HEADER = @"{\rtf1\ansi\ansicpg1252\deff0\deflang1033";

 

如果修改下即可支持中文:

 private const string RTF_HEADER = @"{\rtf1\ansi\ansicpg936\deff0\deflang1033\deflangfe2052";

 

因为介绍内容如下:

1、文件基本属性:

{\rtf1 RTF版本\ansi字符集\ansicpg936简体中文\deff0默认字体0\deflang1033美国英语\deflangfe2052中国汉语

 

同样,把字体部分稍微调整下:

// \fcharset specifies the character set of a font in the font table.
            
// 0 is for ANSI.
            _fontTable.Append(@"\fcharset0 ");

 

修改后变为以下代码即可:

            // \fcharset specifies the character set of a font in the font table.
            
// 0 is for ANSI.
            
//_fontTable.Append(@"\fcharset0 ");
            _fontTable.Append(@"\fcharset134 ");

 

调整后测试效果如下图所示,同样可以实现和步骤一的效果:

 

 

内容打印

在以上基础上,我从别处获取了关于RichTextBox控件的打印功能代码,对该控件进行了功能扩充。打印效果如下图所示:

 

 

 整合后的控件源码地址:https://files.cnblogs.com/wuhuacong/ExRichTextBox.rar

 

posted on 2010-07-20 19:28  伍华聪  阅读(12654)  评论(6编辑  收藏  举报

导航