啟動類:Program.cs
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace FontColor
{
static class Program
{
/// <summary>
/// 應用程式的主進入點。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new frmColorWords());
}
}
}
尋找類:frmColorWords.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
namespace FontColor
{
public partial class frmColorWords : Form
{
ArrayList list = new ArrayList();
public frmColorWords()
{
InitializeComponent();
}
//索引單詞
private void btnSearch_Click(object sender, EventArgs e)
{
if (this.rtxtText.Text.Equals(""))
{
MessageBox.Show("請先載入要索引的文本!", "訊息");
this.btnLoadText.Focus();
return;
}
if (this.txtSearchWords.Text.Equals(""))
{
MessageBox.Show("請輸入您要索引的單詞!", "訊息");
this.txtSearchWords.Focus();
return;
}
int place = 0;
this.rtxtText.SelectAll();
this.rtxtText.SelectionColor = Color.Black;
this.rtxtText.SelectionFont = new Font("宋體", 9);
string text = " " + this.rtxtText.Text.ToLower() + " ";
string word = this.txtSearchWords.Text.ToLower().Trim();
int s = 0;//單詞出現的個數
if (text.IndexOf(word, place + 1) == -1)
{
MessageBox.Show("沒有找到!");
}
else
{
try
{
for (int i = 0; i < text.Length; i++)
{
int start = text.IndexOf(word, place + 1);
if (start == -1)
{
MessageBox.Show("索引單詞 " + word + " 完畢!\n總共索引到 " + s + " 處!", "訊息");
break;
}
string front = text.Substring(start - 1, 1);
string back = text.Substring(start + word.Length, 1);
if (list.Contains(front) && list.Contains(back))
{
s++;
this.rtxtText.Select(start - 1, word.Length);
this.rtxtText.SelectionColor = Color.Blue;
this.rtxtText.SelectionFont = new Font("Blod", 15);
}
place = start;
}
}
catch { }
}
}
//載入文本
private void btnLoadText_Click(object sender, EventArgs e)
{
OpenFileDialog openFile = new OpenFileDialog();
openFile.Filter = "文字檔(*.txt)|*.txt|所有檔案(*.*)|*.*";
if (openFile.ShowDialog() == DialogResult.OK)
{
this.rtxtText.Clear();
string fileName = openFile.FileName;
this.rtxtText.LoadFile(fileName, RichTextBoxStreamType.PlainText);
this.txtSearchWords.Focus();
}
}
//退出
private void btnExit_Click(object sender, EventArgs e)
{
this.Dispose();
}
//介面載入
private void frmColorWords_Load(object sender, EventArgs e)
{
string[] str = { " ", "!", ",", "@", "#", "$", ";", ":", "(", ")", "_", "+", "-", "*", "/", "?", "<", ">", "/", "\\", "{", "}", "?", "=", "&", "%", "~", ".", "\"", "\'", "[", "]" };
for (int i = 0; i < str.Length; i++)
{
list.Add(str[i]);
}
}
}
}
-----javascript:
高亮顯示查詢字元
// JavaScript Document
function WordHight(){
var obj = document.getElementById("objContent");
if(obj){
var div = document.getElementById("objContent");
if(document.getElementById("KeyWord"));
var keyWord=document.getElementById("KeyWord").value;
MarkHighLight(div,keyWord);
}
}
/*----------------------------------------*\
* 使用 js 標記高亮關鍵詞 by markcxz(markcxz@aol.com)
* 參數說明:
* obj: 對象, 要進行高亮顯示的html標籤節點.
* hlWords: 字串, 要進行高亮的關鍵詞詞, 使用 豎杠(|)或空格分隔多個詞 .
* cssClass: 字串, 定義關鍵詞反白風格的css偽類.
* 參考資料: javascript HTML DOM 高亮顯示頁面特定字詞 By shawl.qiu
\*----------------------------------------*/
function MarkHighLight(obj,hlWords,cssClass){
hlWords=AnalyzeHighLightWords(hlWords);
if(obj==null || hlWords.length==0)
return;
if(cssClass==null)
cssClass="highlight";
MarkHighLightCore(obj,hlWords);
//------------執行高亮標記的核心方法----------------------------
function MarkHighLightCore(obj,keyWords){
var re=new RegExp(keyWords, "i");
for(var i=0; i<obj.childNodes.length; i++){
var childObj=obj.childNodes[i];
if(childObj.nodeType==3){
if(childObj.data.search(re)==-1)continue;
var reResult=new RegExp("("+keyWords+")", "gi");
var objResult=document.createElement("span");
objResult.innerHTML=childObj.data.replace(reResult,"<span class='"+cssClass+"'>$1</span>");
if(childObj.data==objResult.childNodes[0].innerHTML) continue;
obj.replaceChild(objResult,childObj);
}else if(childObj.nodeType==1){
MarkHighLightCore(childObj,keyWords);
}
}
}
//----------分析關鍵詞----------------------
function AnalyzeHighLightWords(hlWords){
if(hlWords==null) return "";
hlWords=hlWords.replace(/\s+/g,"|").replace(/\|+/g,"|");
hlWords=hlWords.replace(/(^\|*)|(\|*$)/g, "");
if(hlWords.length==0) return "";
var wordsArr=hlWords.split("|");
if(wordsArr.length>1){
var resultArr=BubbleSort(wordsArr);
var result="";
for(var i=0;i<resultArr.length;i++){
result=result+"|"+resultArr[i];
}
return result.replace(/(^\|*)|(\|*$)/g, "");
}else{
return hlWords;
}
}
//-----利用冒泡排序法把長的關鍵詞放前面-----
function BubbleSort(arr){
var temp, exchange;
for(var i=0;i<arr.length;i++){
exchange=false;
for(var j=arr.length-2;j>=i;j--){
if((arr[j+1].length)>(arr[j]).length){
temp=arr[j+1]; arr[j+1]=arr[j]; arr[j]=temp;
exchange=true;
}
}
if(!exchange)break;
}
return arr;
}
}