Click here to Skip to main content
15,886,795 members
Home / Discussions / C#
   

C#

 
GeneralRe: Best method for syntax highlighting Pin
Luc Pattyn15-Jul-09 8:40
sitebuilderLuc Pattyn15-Jul-09 8:40 
GeneralRe: Best method for syntax highlighting Pin
WebMaster16-Jul-09 6:12
WebMaster16-Jul-09 6:12 
GeneralRe: Best method for syntax highlighting Pin
Luc Pattyn16-Jul-09 6:49
sitebuilderLuc Pattyn16-Jul-09 6:49 
GeneralRe: Best method for syntax highlighting Pin
WebMaster16-Jul-09 21:54
WebMaster16-Jul-09 21:54 
GeneralRe: Best method for syntax highlighting Pin
Luc Pattyn16-Jul-09 23:01
sitebuilderLuc Pattyn16-Jul-09 23:01 
GeneralRe: Best method for syntax highlighting Pin
WebMaster17-Jul-09 3:14
WebMaster17-Jul-09 3:14 
GeneralRe: Best method for syntax highlighting Pin
Luc Pattyn17-Jul-09 3:26
sitebuilderLuc Pattyn17-Jul-09 3:26 
GeneralRe: Best method for syntax highlighting Pin
WebMaster17-Jul-09 4:59
WebMaster17-Jul-09 4:59 
Hey dude, thanks! It seems like it gives the totally perfect coords (increases correctly and so on), however now it says char 15, line 18 instead of char 1, line 1. :s

Point p = this.PointToClient(Cursor.Position);
colNum = (p.X / charWidth) + this.stormParent.VerticalScroll.Value;
lineNum = (p.Y / (int)this.stormParent.Font.Size) + this.stormParent.HorizontalScroll.Value;


Btw: can you tell if this is correct?:

The original RichTextBox redraws the whole text every time a color has changed, so if I have 5000 words it redraws 5000 times every time I edit the text. Now, the reason this is more efficient, is because the text natively is given the intended colors instead of changing it when it has been drawn already. Now, to highlight a line, I'd use a code somewhat like this:

namespace Storm.TextBox
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Linq;
    using System.Text;

    #region Keyword class
    public class Keyword
    {
        private string _kword = "";
        private Color _kcol = SystemColors.ControlText;
        private Font _kfont = new Font(new Font("Courier New", 10), FontStyle.Regular);

        public Keyword(string keyword, Color color, Font font)
        {
            _kword = keyword;
            _kcol = color;
            _kfont = font;
        }

        public string Keyword
        {
            get { return _kword; }
            set { _kword = value; }
        }

        public Color KeywordColor
        {
            get { return _kcol; }
            set { _kcol = value; }
        }

        public Font KeywordFont
        {
            get { return _kfont; }
            set { _kfont = value; }
        }
    }
    #endregion

    public class HighlightingSetup
    {
        #region Members
        // The parent member.
        private TextEditor stormParent = null;

        // The dictionary containing data about keywords added.
        private Dictionary<string, Keyword> keywordDic;
        #endregion

        #region Methods
        public void Add(string keyword, Color color, FontStyle fontstyle) 
        {
            keywordDic.Add(keyword, new Keyword(keyword, color,
                new Font(stormParent.Font, fontstyle)));
        }

        public Keyword KeywordFromString(string keyword)
        {
            if (keywordDic.ContainsKey(keyword))
                return keywordDic[keyword];

            return null;
        }

        public void Remove(string keyword)
        {
            if (keywordDic.ContainsKey(keyword))
                keywordDic.Remove(keyword);
        }
        #endregion

        /// <summary>
        /// Initializes the HighlightingSetup.
        /// </summary>
        public HighlightingSetup(TextEditor parent)
        {
            stormParent = parent;
            keywordDic = new Dictionary<string, Keyword>();
        }
    }
}

//...

        // Temporary color and font variables 
        // for redrawing lines.
        private Color[] kColors;
        private Font[] kFonts;

        // The chars that splits a line.
        private char[] Splitters = { ' ', '(', ')', '[', ']', '{', '}', '!', '"', 
                                     '#', '$', '%', '&', '/', '=', '?', '+', '-', 
                                     '*', '^', '_', ':', ';', '<', '>', '\\'};

        // Temporary string when redrawing lines.
        private string tempLine = "";
        // An integer member for looping.
        private int maxKeywordCount = 0;

        private void HighlightLine(int lineNum)
        {
            string splitString = doc.GetLineFromIndex(lineNum);
            string[] keywords = splitString.Split(Splitters);
            int keywordCount = 0;

            foreach (string keyword in keywords)
            {
                // Set the colors and fonts by index
                // matching keywordCount, so we now
                // what colors and fonts to use for
                // each word in the line. 
                Keyword kw = setup.KeywordFromString(keyword);
                kColors[keywordCount] = kw.KeywordColor;
                kFonts[keywordCount] = kw.KeywordFont;

                tempLine += keyword + "+";

                keywordCount++;
            }

            maxKeywordCount = keywordCount;
            tempLine = tempLine.Substring(0, tempLine.Length - 1); // Remove the last "+".

            // Redraw when everything has been setup.
            this.Invalidate();
        }

        private void onRepaint(object sender, PaintEventArgs e)
        {
            string splitString = tempLine;
            string[] keywords = splitString.Split('+');
            int keywordCount = 0;

            foreach (string keyword in keywords)
            {
                Keyword kw = setup.KeywordFromString(keyword);
                Color kcolor = kw.KeywordColor;
                Font kfont = kw.KeywordFont;

                // Draw the keyword with the correct color.
                StringFormat strFormat = new StringFormat(StringFormatFlags.NoWrap);
                Rectangle posRect = new Rectangle(0, 0, 0, 0); // Use some fancy formula to figure out where to position the text.
                ControlPaint.DrawStringDisabled(e.Graphics, keyword, kfont, kcolor, 
                                                posRect, strFormat);
                ControlPaint.DrawStringDisabled(e.Graphics, " ", this.Font, kcolor,
                                                posRect, strFormat);

                keywordCount++;
            }
        }

GeneralRe: Best method for syntax highlighting Pin
Luc Pattyn17-Jul-09 11:15
sitebuilderLuc Pattyn17-Jul-09 11:15 
GeneralRe: Best method for syntax highlighting Pin
WebMaster17-Jul-09 21:39
WebMaster17-Jul-09 21:39 
GeneralRe: Best method for syntax highlighting Pin
WebMaster28-Jul-09 8:41
WebMaster28-Jul-09 8:41 
GeneralRe: Best method for syntax highlighting Pin
Luc Pattyn28-Jul-09 22:37
sitebuilderLuc Pattyn28-Jul-09 22:37 
GeneralRe: Best method for syntax highlighting Pin
Kevin Marois14-Jul-09 9:08
professionalKevin Marois14-Jul-09 9:08 
GeneralRe: Best method for syntax highlighting Pin
Adam R Harris14-Jul-09 10:32
Adam R Harris14-Jul-09 10:32 
QuestionPassing a pointer to array of structs via P/Invoke Pin
Klempie14-Jul-09 6:29
Klempie14-Jul-09 6:29 
Questioncustomizing a ToolStripTextBox Pin
Jim Crafton14-Jul-09 5:38
Jim Crafton14-Jul-09 5:38 
AnswerRe: customizing a ToolStripTextBox Pin
Henry Minute14-Jul-09 5:53
Henry Minute14-Jul-09 5:53 
GeneralRe: customizing a ToolStripTextBox Pin
Jim Crafton14-Jul-09 6:04
Jim Crafton14-Jul-09 6:04 
GeneralRe: customizing a ToolStripTextBox Pin
DaveyM6914-Jul-09 6:26
professionalDaveyM6914-Jul-09 6:26 
Questionreturn string Pin
AndyInUK14-Jul-09 5:22
AndyInUK14-Jul-09 5:22 
AnswerRe: return string Pin
Simon P Stevens14-Jul-09 5:28
Simon P Stevens14-Jul-09 5:28 
GeneralRe: return string Pin
AndyInUK14-Jul-09 5:36
AndyInUK14-Jul-09 5:36 
AnswerRe: return string PinPopular
Luc Pattyn14-Jul-09 5:42
sitebuilderLuc Pattyn14-Jul-09 5:42 
GeneralRe: return string Pin
AndyInUK14-Jul-09 6:19
AndyInUK14-Jul-09 6:19 
GeneralRe: return string Pin
Luc Pattyn14-Jul-09 6:27
sitebuilderLuc Pattyn14-Jul-09 6:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.