• <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>

            為生存而奔跑

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

            留言簿(5)

            我參與的團隊

            搜索

            •  

            積分與排名

            • 積分 - 328382
            • 排名 - 74

            最新評論

            閱讀排行榜

            評論排行榜

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

            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>
                    
            /// 必需的設計器變量。
                    
            /// </summary>
                    private System.ComponentModel.IContainer components = null;

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

                    
            #region 組件設計器生成的代碼

                    
            /// <summary>
                    
            /// 設計器支持所需的方法 - 不要
                    
            /// 使用代碼編輯器修改此方法的內(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: 假設“wang”是關鍵字,某一行的內(nèi)容為:hellowang  wang,則本應該在第二個“wnag”上高亮顯示,但是結果在“hellowang”中的“wang”上高亮顯示了。
            運行后界面如下:

            posted on 2009-12-19 18:31 baby-fly 閱讀(379) 評論(0)  編輯 收藏 引用 所屬分類: C#
            久久久国产精品网站| 国产精品久久久久久搜索| 久久精品国产一区二区电影| 久久www免费人成精品香蕉| 伊人精品久久久久7777| 韩国免费A级毛片久久| 久久久WWW免费人成精品| 97精品国产97久久久久久免费| 91精品国产色综合久久| 日日狠狠久久偷偷色综合免费| 久久亚洲精品国产精品| 久久精品国产清自在天天线| 久久天天躁狠狠躁夜夜网站| 欧美亚洲另类久久综合婷婷| 久久久青草久久久青草| 亚洲va中文字幕无码久久| 久久午夜综合久久| 婷婷综合久久中文字幕| 久久天天躁狠狠躁夜夜avapp| 亚洲精品久久久www| 久久青青国产| 精品人妻伦一二三区久久 | 久久93精品国产91久久综合| 久久久久久国产精品美女| 国产激情久久久久影院老熟女免费 | 国内精品久久久久久野外| 伊人久久无码中文字幕| 日本加勒比久久精品| 三级片免费观看久久| 久久亚洲国产成人精品无码区| 丁香五月网久久综合| 91精品国产高清91久久久久久| 久久亚洲中文字幕精品有坂深雪| 伊人久久大香线蕉精品不卡| 午夜精品久久影院蜜桃| 欧美日韩精品久久久久| 久久久无码精品亚洲日韩软件| 国产精品免费看久久久香蕉| 精品久久久久一区二区三区| 国产精品免费看久久久香蕉| 久久99久久无码毛片一区二区|