C# javascript 尋找字型反白(改變大小、顏色)

來源:互聯網
上載者:User

啟動類: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;           
 }

}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.