zoukankan      html  css  js  c++  java
  • C# javascript 查找字体突出显示(改变大小、颜色)

    启动类: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;           
     }

    }

  • 相关阅读:
    Mysql外键和表关系
    列类型-字符类型
    列类型-日期时间型
    列类型
    破解MySQL的root密码
    数据库简单使用
    数据库简介
    socketserver
    python网络编程-粘包问题的解决
    python-网络编程,简单模型
  • 原文地址:https://www.cnblogs.com/zengwei/p/1155820.html
Copyright © 2011-2022 走看看