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

            為生存而奔跑

               :: 首頁 :: 聯系 :: 聚合  :: 管理
              271 Posts :: 0 Stories :: 58 Comments :: 0 Trackbacks

            留言簿(5)

            我參與的團隊

            搜索

            •  

            積分與排名

            • 積分 - 326993
            • 排名 - 74

            最新評論

            閱讀排行榜

            評論排行榜

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

            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>
                    
            /// 設計器支持所需的方法 - 不要
                    
            /// 使用代碼編輯器修改此方法的內容。
                    
            /// </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;
                }
            }

            現在程序還有bug: 假設“wang”是關鍵字,某一行的內容為:hellowang  wang,則本應該在第二個“wnag”上高亮顯示,但是結果在“hellowang”中的“wang”上高亮顯示了。
            運行后界面如下:

            posted on 2009-12-19 18:31 baby-fly 閱讀(372) 評論(0)  編輯 收藏 引用 所屬分類: C#
            久久久久亚洲av成人网人人软件| 久久婷婷色综合一区二区| 久久精品国产亚洲精品2020| 久久水蜜桃亚洲av无码精品麻豆| 久久99国产精一区二区三区| 久久人人超碰精品CAOPOREN| 欧美丰满熟妇BBB久久久| 亚洲国产精品婷婷久久| 久久人妻无码中文字幕| 国产精品99久久不卡| 中文字幕人妻色偷偷久久| 色综合久久久久网| 97精品国产97久久久久久免费| 国内精品免费久久影院| 亚洲AV日韩精品久久久久久| 久久97久久97精品免视看| 久久天天躁狠狠躁夜夜躁2O2O| 亚洲欧美国产日韩综合久久| 97久久精品国产精品青草| 久久久久久国产精品美女| 久久se精品一区二区影院| 国产三级久久久精品麻豆三级| 99精品国产综合久久久久五月天| 久久精品夜色噜噜亚洲A∨ | 色99久久久久高潮综合影院| 麻豆亚洲AV永久无码精品久久| 久久久久久国产精品美女| 91精品国产9l久久久久| 久久亚洲欧美国产精品| 人人妻久久人人澡人人爽人人精品| 国产A级毛片久久久精品毛片| 色偷偷偷久久伊人大杳蕉| 久久香综合精品久久伊人| 欧美午夜A∨大片久久| 久久高清一级毛片| 国内精品免费久久影院| 久久99精品久久久久久野外| 伊人色综合久久天天| 99久久免费只有精品国产| 国产99久久九九精品无码| 国产精品久久久天天影视香蕉 |