非IT企業に勤める中年サラリーマンのIT日記

非IT企業でしかもITとは全く関係ない部署にいる中年エンジニア。唯一の趣味がプログラミングという”自称”プログラマー。

C#: RichTextBoxで特定の文字の色を高速で変換する方法

      2016/06/11

[ad#top-1]
 
C#のRichTextBoxコントロールで特定の文字の色を変えるためには、一般的にFindメソッドを使って検索してから変換するのが一般的ですが、このやり方だと、変換する文字の数が多かったり文字数が多かったりすると変換までに時間がかかってしまいます。私自身、プログラミング用のエディタを作りたかったので、キーボードをタイピングしながら文字色を高速に変換させる必要がありました。当然、速度アップは喫緊の課題でしたが、RTFフォーマットを直接いじることで解決しました。その方法つかったクラスを作ったので紹介します。

  1. まず、色を変えたい文字列を List<string> keywordに仕込んでおきます。
  2. RichTextBox textBox  = new RichTextBox(); でRichTextBoxをフォームに貼り、
  3. textBox.Rtf = TextColorSet.keyword(textBox.Text, keyword); とやれば完了。

TextColorSet(TextColorSet.cs)のソースコードは以下の通りです。

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
class TextColorSet{
   public static string keyword(string text, List<string> keyword){
      text = text.Replace(“\\”, “\\\\”);
      text = “\\cf3 “ + text;
      text = text.Replace(“\n”, “\a\n\a”);
      text = text.Replace(“\r”, “\a\r\a”);
      text = text.Replace(“\t”, “\a\t\a”);
      text = text.Replace(“-“, “\a-\a”);
      text = text.Replace(“=”, “\a=\a”);
      text = text.Replace(“(“, “\a(\a”);
      text = text.Replace(“)”, “\a)\a”);
      text = text.Replace(“[“, “\a[\a”);
      text = text.Replace(“]”, “\a]\a”);
      text = text.Replace(“{“, “\a{\a”);
      text = text.Replace(“}”, “\a}\a”);
      text = text.Replace(“<“, “\a<\a”);
      text = text.Replace(“>”, “\a>\a”);
      text = text.Replace(“.”, “\a.\a”);
      text = text.Replace(“,”, “\a,\a”);
      text = text.Replace(“;”, “\a;\a”);
      text = text.Replace(” “, “\a”);
      for(int i=0; i<keyword.Count; i++){
         text = text.Replace(“\a”+keyword[i]+“\a”, “\a\\cf1 “+keyword[i]+“\\cf3 \a”);
      }
      text = text.Replace(“\a\n\a”, “\n”);
      text = text.Replace(“\a\r\a”, “\r”);
      text = text.Replace(“\a\t\a”, “\t”);
      text = text.Replace(“\a-\a”, “-“);
      text = text.Replace(“\a=\a”, “=”);
      text = text.Replace(“\a(\a”, “(“);
      text = text.Replace(“\a)\a”, “)”);
      text = text.Replace(“\a[\a”, “[“);
      text = text.Replace(“\a]\a”, “]”);
      text = text.Replace(“\a{\a”, “{“);
      text = text.Replace(“\a}\a”, “}”);
      text = text.Replace(“\a<\a”, “<“);
      text = text.Replace(“\a>\a”, “>”);
      text = text.Replace(“\a.\a”, “.”);
      text = text.Replace(“\a,\a”, “,”);
      text = text.Replace(“\a;\a”, “;”);
      text = text.Replace(“\a”, ” “);
      text = text.Replace(“{“, “\\{“);
      text = text.Replace(“}”, “\\}”);
      text = TextColorSet.header() + text + “\n}”;
      text = text.Replace(“\n”, “\\par\n”);
      return text;
   }
   private static string header(){
      string header=“”;
      header += “{\\rtf1\\ansi\\deff0{\\fonttbl{\\f0\\fnil\\fcharset128 \\’82\\’6c\\’82\\’72 \\’83\\’53\\’83\\’56\\’83\\’62\\’83\\’4e;}}”;
      //header += “{\\colortbl ;\\red0\\green0\\blue255;\\red255\\green255\\blue255;\\red0\\green0\\blue0;\\red255\\green0\\blue255;\\red0\\green128\\blue0;}”;
      header += “{\\colortbl ;\\red255\\green0\\blue0;\\red255\\green255\\blue255;\\red0\\green0\\blue0;\\red255\\green0\\blue255;\\red0\\green128\\blue0;}”;
      header += “\\viewkind4\\uc1\\pard\\li75\\tx345\\tx690\\tx1020\\tx1365\\tx1710\\tx2055\\tx2385\\tx2730\\tx3075\\tx3420\\tx3750\\tx4095”;
      header += “\\tx4440\\tx4785\\tx5115\\tx5460\\tx5805\\tx6150\\tx6480\\tx6825\\tx7170\\tx7515\\tx7845\\tx8190\\tx8535\\tx8880\\tx9210”;
      header += “\\tx9555\\tx9900\\tx10245\\tx10575\\tx10920\\cf1\\highlight2\\lang1041\\f0\\fs20 “;
      return header;
   }
}

 

こちらが、サンプル。キーワードをkeyword.txtに保存しておいて、それを読み込んでキーワードの文字色を赤くするようになっています。サンプルプログラムでは文字を打ちながら文字を打ちながら特定文字を検出して色付けします。

a

 

 

サンプルプログラムはこちらからダウンロードできます。

DOWNLOAD ※右クリックではなく普通のクリックで。ダウンロードページに飛びます。
 
[ad#ad-1]

スポンサーリンク

 - C#応用編