首页 | 软件中心 | Designand Inspiration
读库教程网 > 网络教程 > 技术开发 > C-C++ > RichTextBox扩充控件的中文乱码处置打算

RichTextBox扩充控件的中文乱码处置打算

添加:2010年7月23日


  在顺序中有使用RichTextBox举行一些操作的需求,无意中在CodeProject中发觉了一个封装比拟完壁的RichTextBox控件(http://www.codeproject.com/KB/edit/csexrichtextbox.aspx),控件封装的仍旧不错,测试界面成效如下:
RichTextBox 扩充控件的中文乱码处置打算[多图]图片1 
  总体来说,支持各种格式的定义以及图片的插入,成效仍旧相当不错,不过在实践中运用发觉,用代码插入的中文内容会出现乱码。
  处置办法一: 
  由于本身对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 扩充控件的中文乱码处置打算[多图]图片2
  检查原图(大图)
  内容打印:
  在以上根本上,我从别处获取了关于RichTextBox控件的打印功用代码,对该控件举行了功用扩大。打印成效如下图所示:
RichTextBox 扩充控件的中文乱码处置打算[多图]图片3
  检查原图(大图)
  出处:http://www.iqidi.com 
  本文示例源代码或素材下载

读库教程网文章由网络收集后整理发布,文章发布人拥有该内容的所有权力及责任!

如果你喜欢这页,可以按Ctrl+D收藏起来。

相关内容
上一个内容:c#借鉴httppost带cookie
下一个内容:用lambda去除Magic-String
相关评论
公益广告
精彩推荐
友情链接: 百分百青年 | 烛光信息网 | 夏布新网 | 新育互联网
管理员:QQ:27038219, E-mail:27038219@qq.com今日更新
读库教程网所有文章从网络收集所发布,文章发布人拥有该内容的所有权力及责任,转载时请注明出处!
Template designed by www.dkuu.com. Optimized for 1024x768 to Firefox,Opera and MS-IE6/IE7.