• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            為生存而奔跑

               :: 首頁(yè) :: 聯(lián)系 :: 聚合  :: 管理
              271 Posts :: 0 Stories :: 58 Comments :: 0 Trackbacks

            留言簿(5)

            我參與的團(tuán)隊(duì)

            搜索

            •  

            積分與排名

            • 積分 - 330206
            • 排名 - 74

            最新評(píng)論

            閱讀排行榜

            評(píng)論排行榜

            該控件可以作為一個(gè)簡(jiǎn)易的代碼編輯器,可以實(shí)現(xiàn)代碼的高亮顯示,代碼行號(hào)等。

            CodeRichText.cs

            using System;
            using System.Collections.Generic;
            using System.ComponentModel;
            using System.Drawing;
            using System.Data;
            using System.Text;
            using System.Windows.Forms;
            using System.Runtime.InteropServices;
            using HWND = System.IntPtr;

            namespace CodeRichText
            {
                public partial class CodeRichText : UserControl
                {
                    private string[] keywords ={ };
                    private string[] dividers ={ };

                    public CodeRichText()
                    {
                        InitializeComponent();
                        UpdateLineNo();
                    }

                    [DllImport("user32")]
                    private static extern int SendMessage(HWND hwnd, int wMsg, int wParam, IntPtr lParam);
                    private const int WM_SETREDRAW = 0xB;

                    public string[] KeyWords
                    {
                        get { return keywords; }
                        set 
                        {
                            keywords = value;
                            ColorAllText();
                        }
                    }

                    public string[] Dividers
                    {
                        get { return dividers; }
                        set
                        {
                            dividers = value;
                            ColorAllText();
                        }
                    }

                    public string CodeText
                    {
                        get { return this.richTextBoxSourceCode.Text; }
                        set { this.richTextBoxSourceCode.Text = value; }
                    }

                    public Font CodeFont
                    {
                        get { return this.richTextBoxSourceCode.Font; }
                        set { this.richTextBoxSourceCode.Font = value; }
                    }

                    public void LoadFile(string path)
                    {
                        richTextBoxSourceCode.LoadFile(path,RichTextBoxStreamType.PlainText);
                    }

                    public void SaveFile(string path)
                    {
                        richTextBoxSourceCode.SaveFile(path,RichTextBoxStreamType.PlainText);
                    }

                    private void ColorAllText()
                    {
                        if (richTextBoxSourceCode.Text == string.Empty) return;
                        for (int i = 0; i < richTextBoxSourceCode.Lines.Length;i++ )
                            ColorCurrentText(i);
                    }

                    private void ColorCurrentText(int lineNum)
                    {
                        if (richTextBoxSourceCode.Text == string.Empty) return;

                        string lineStr = richTextBoxSourceCode.Lines[lineNum];
                        int selectStart = richTextBoxSourceCode.SelectionStart;
                        int lineStart=0;
                        for(int i=0;i<lineNum;i++)
                            lineStart+=richTextBoxSourceCode.Lines[i].Length+1;

                        SendMessage(richTextBoxSourceCode.Handle, WM_SETREDRAW, 0, IntPtr.Zero);

                        richTextBoxSourceCode.SelectionStart = lineStart;
                        richTextBoxSourceCode.SelectionLength = lineStr.Length;
                        richTextBoxSourceCode.SelectionColor = Color.Black;
                        richTextBoxSourceCode.SelectionStart = selectStart;
                        richTextBoxSourceCode.SelectionLength = 0;
                        richTextBoxSourceCode.SelectionColor = Color.Black;

                        string[] words = lineStr.Split(dividers,StringSplitOptions.RemoveEmptyEntries);
                        int lineIndex = 0;
                        foreach (string word in words)
                        {
                            if (IsKeyWord(word))
                            {
                                lineIndex = lineStr.IndexOf(word, lineIndex);
                                richTextBoxSourceCode.SelectionStart = lineStart + lineIndex;
                                richTextBoxSourceCode.SelectionLength = word.Length;
                                richTextBoxSourceCode.SelectionColor = Color.Blue;

                                richTextBoxSourceCode.SelectionStart = selectStart;
                                richTextBoxSourceCode.SelectionLength = 0;
                                richTextBoxSourceCode.SelectionColor = Color.Black;

                                lineIndex += word.Length + 1;
                            }
                            else if (IsNumber(word))
                            {
                                lineIndex = lineStr.IndexOf(word, lineIndex);
                                richTextBoxSourceCode.SelectionStart = lineStart + lineIndex;
                                richTextBoxSourceCode.SelectionLength = word.Length;
                                richTextBoxSourceCode.SelectionColor = Color.Tomato;

                                richTextBoxSourceCode.SelectionStart = selectStart;
                                richTextBoxSourceCode.SelectionLength = 0;
                                richTextBoxSourceCode.SelectionColor = Color.Black;

                                lineIndex += word.Length + 1;
                            }
                        }

                        SendMessage(richTextBoxSourceCode.Handle, WM_SETREDRAW, 1, IntPtr.Zero);
                        richTextBoxSourceCode.Refresh();
                    }

                    private bool IsKeyWord(string word)
                    {
                        foreach (string s in keywords)
                        {
                            if (string.Compare(word, s) == 0)
                                return true;
                        }
                        return false;
                    }

                    private bool IsNumber(string word)
                    {
                        foreach (char ch in word)
                        {
                            if (!(ch >= '0' && ch <= '9'))
                                return false;
                        }
                        return true;
                    }

                    private void richTextBoxSourceCode_TextChanged(object sender, EventArgs e)
                    {
                        if (richTextBoxSourceCode.Text == string.Empty) return;
                        int lineNum = richTextBoxSourceCode.GetLineFromCharIndex(richTextBoxSourceCode.SelectionStart);
                        UpdateLineNo();
                        ColorCurrentText(lineNum);
                    }

                    private void UpdateLineNo()
                    {
                        SendMessage(richTextBoxLineNo.Handle, WM_SETREDRAW, 0, IntPtr.Zero);

                        //get index of first visible char and number of first visible line
                        Point pos = new Point(0, 0);
                        int firstIndex = richTextBoxSourceCode.GetCharIndexFromPosition(pos);
                        int firstLine = richTextBoxSourceCode.GetLineFromCharIndex(firstIndex);

                        //get index of last visible char and number of last visible line
                        pos.X = ClientRectangle.Width;
                        pos.Y = ClientRectangle.Height;
                        int lastIndex = richTextBoxSourceCode.GetCharIndexFromPosition(pos);
                        int lastLine = richTextBoxSourceCode.GetLineFromCharIndex(lastIndex);

                        //this is the point position of last visible char, use its Y value for calculating numberLabel size
                        pos = richTextBoxSourceCode.GetPositionFromCharIndex(lastIndex);

                        //finally, update line number
                        StringBuilder lineNo = new StringBuilder();
                        for (int i = firstLine; i <= lastLine + 1; i++)
                        {
                            lineNo.Append((i + 1).ToString() + "\n");
                        }
                        richTextBoxLineNo.Text = lineNo.ToString();

                        SendMessage(richTextBoxLineNo.Handle, WM_SETREDRAW, 1, IntPtr.Zero);
                        richTextBoxLineNo.Refresh();
                    }

                    private void CodeRichText_Scroll(object sender, ScrollEventArgs e)
                    {

                    }

                    private void richTextBoxSourceCode_VScroll(object sender, EventArgs e)
                    {
                        //move location of line number for amount of pixels caused by scrollbar
                        int d = richTextBoxSourceCode.GetPositionFromCharIndex(0).Y % (richTextBoxSourceCode.Font.Height + 1);
                        richTextBoxLineNo.Location = new Point(0, d);
                        UpdateLineNo();
                    }

                    private void richTextBoxLineNo_Enter(object sender, EventArgs e)
                    {
                        richTextBoxSourceCode.Focus();
                    }

                    private void richTextBoxSourceCode_FontChanged(object sender, EventArgs e)
                    {
                        richTextBoxLineNo.Font = new Font(richTextBoxSourceCode.Font, FontStyle.Regular);
                    }
                }
            }

             
            CodeRichText.Designer.cs

            namespace CodeRichText
            {
                partial 
            class CodeRichText
                {
                    
            /// <summary>
                    
            /// 必需的設(shè)計(jì)器變量。
                    
            /// </summary>
                    private System.ComponentModel.IContainer components = null;

                    
            /// <summary>
                    
            /// 清理所有正在使用的資源。
                    
            /// </summary>
                    
            /// <param name="disposing">如果應(yīng)釋放托管資源,為 true;否則為 false。</param>
                    protected override void Dispose(bool disposing)
                    {
                        
            if (disposing && (components != null))
                        {
                            components.Dispose();
                        }
                        
            base.Dispose(disposing);
                    }

                    
            #region 組件設(shè)計(jì)器生成的代碼

                    
            /// <summary>
                    
            /// 設(shè)計(jì)器支持所需的方法 - 不要
                    
            /// 使用代碼編輯器修改此方法的內(nèi)容。
                    
            /// </summary>
                    private void InitializeComponent()
                    {
                        
            this.richTextBoxLineNo = new System.Windows.Forms.RichTextBox();
                        
            this.richTextBoxSourceCode = new System.Windows.Forms.RichTextBox();
                        
            this.SuspendLayout();
                        
            // 
                        
            // richTextBoxLineNo
                        
            // 
                        this.richTextBoxLineNo.BackColor = System.Drawing.SystemColors.ControlLight;
                        
            this.richTextBoxLineNo.BorderStyle = System.Windows.Forms.BorderStyle.None;
                        
            this.richTextBoxLineNo.Cursor = System.Windows.Forms.Cursors.Arrow;
                        
            this.richTextBoxLineNo.Dock = System.Windows.Forms.DockStyle.Left;
                        
            this.richTextBoxLineNo.Font = new System.Drawing.Font("Courier New", 10F);
                        
            this.richTextBoxLineNo.ForeColor = System.Drawing.SystemColors.GradientActiveCaption;
                        
            this.richTextBoxLineNo.Location = new System.Drawing.Point(00);
                        
            this.richTextBoxLineNo.Name = "richTextBoxLineNo";
                        
            this.richTextBoxLineNo.ReadOnly = true;
                        
            this.richTextBoxLineNo.ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.None;
                        
            this.richTextBoxLineNo.Size = new System.Drawing.Size(41408);
                        
            this.richTextBoxLineNo.TabIndex = 1;
                        
            this.richTextBoxLineNo.Text = "";
                        
            this.richTextBoxLineNo.Enter += new System.EventHandler(this.richTextBoxLineNo_Enter);
                        
            // 
                        
            // richTextBoxSourceCode
                        
            // 
                        this.richTextBoxSourceCode.BorderStyle = System.Windows.Forms.BorderStyle.None;
                        
            this.richTextBoxSourceCode.Dock = System.Windows.Forms.DockStyle.Fill;
                        
            this.richTextBoxSourceCode.Font = new System.Drawing.Font("Courier New", 10F);
                        
            this.richTextBoxSourceCode.Location = new System.Drawing.Point(410);
                        
            this.richTextBoxSourceCode.Name = "richTextBoxSourceCode";
                        
            this.richTextBoxSourceCode.Size = new System.Drawing.Size(639408);
                        
            this.richTextBoxSourceCode.TabIndex = 0;
                        
            this.richTextBoxSourceCode.Text = "";
                        
            this.richTextBoxSourceCode.VScroll += new System.EventHandler(this.richTextBoxSourceCode_VScroll);
                        
            this.richTextBoxSourceCode.TextChanged += new System.EventHandler(this.richTextBoxSourceCode_TextChanged);
                        
            // 
                        
            // CodeRichText
                        
            // 
                        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
                        
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
                        
            this.Controls.Add(this.richTextBoxSourceCode);
                        
            this.Controls.Add(this.richTextBoxLineNo);
                        
            this.Name = "CodeRichText";
                        
            this.Size = new System.Drawing.Size(680408);
                        
            this.Scroll += new System.Windows.Forms.ScrollEventHandler(this.CodeRichText_Scroll);
                        
            this.ResumeLayout(false);

                    }

                    
            #endregion

                    
            private System.Windows.Forms.RichTextBox richTextBoxLineNo;
                    
            private System.Windows.Forms.RichTextBox richTextBoxSourceCode;
                }
            }

            現(xiàn)在程序還有bug: 假設(shè)“wang”是關(guān)鍵字,某一行的內(nèi)容為:hellowang  wang,則本應(yīng)該在第二個(gè)“wnag”上高亮顯示,但是結(jié)果在“hellowang”中的“wang”上高亮顯示了。
            運(yùn)行后界面如下:

            posted on 2009-12-19 18:31 baby-fly 閱讀(382) 評(píng)論(0)  編輯 收藏 引用 所屬分類(lèi): C#
            久久发布国产伦子伦精品| 波多野结衣久久| 久久水蜜桃亚洲av无码精品麻豆| 久久精品国产欧美日韩| 久久久99精品一区二区| 欧美黑人激情性久久| 久久久久久无码国产精品中文字幕 | 久久精品免费大片国产大片| 色婷婷狠狠久久综合五月| 人妻少妇久久中文字幕 | 久久亚洲精品中文字幕| 日本精品久久久久中文字幕8| 久久亚洲AV无码精品色午夜麻豆 | 香蕉久久夜色精品国产小说| 亚洲国产精品一区二区三区久久 | 怡红院日本一道日本久久 | a级毛片无码兔费真人久久| 亚洲人成精品久久久久 | 久久精品国产亚洲AV无码麻豆| 国产成人久久精品二区三区| 久久AV高清无码| 亚洲国产另类久久久精品| 伊人久久五月天| 免费一级欧美大片久久网 | 久久综合综合久久狠狠狠97色88 | 国产精品一区二区久久精品涩爱| 中文字幕亚洲综合久久2| 国内精品久久久久| 97久久超碰成人精品网站| 蜜臀av性久久久久蜜臀aⅴ麻豆| 久久精品一区二区三区AV| 久久人人青草97香蕉| 久久国产AVJUST麻豆| 国产免费久久精品99re丫y| 99久久香蕉国产线看观香| 99久久做夜夜爱天天做精品| 久久久久久久女国产乱让韩| 一本色道久久HEZYO无码| 国产人久久人人人人爽| 久久青草国产精品一区| 狠狠色综合网站久久久久久久 |