Click here to Skip to main content
15,884,838 members
Articles / Desktop Programming / Windows Forms

Fast Colored TextBox for Syntax Highlighting

Rate me:
Please Sign up or sign in to vote.
4.97/5 (878 votes)
24 Oct 2014LGPL323 min read 7.1M   104.2K   1.3K  
Custom text editor with syntax highlighting
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using FastColoredTextBoxNS;
using System.Text.RegularExpressions;
using System.Drawing.Imaging;
using System.Timers;

namespace Tester
{
    public partial class GifImageDrawingSample : Form
    {
        GifImageStyle style;
        static string RegexSpecSymbolsPattern = @"[\^\$\[\]\(\)\.\\\*\+\|\?\{\}]";

        public GifImageDrawingSample()
        {
            InitializeComponent();

            style = new GifImageStyle(fctb);
            style.ImagesByText.Add(@":bb", Properties.Resources.bye);
            style.ImagesByText.Add(@":D", Properties.Resources.lol);
            style.ImagesByText.Add(@"8)", Properties.Resources.rolleyes);
            style.ImagesByText.Add(@":@", Properties.Resources.unsure);
            style.ImagesByText.Add(@":)", Properties.Resources.smile_16x16);
            style.ImagesByText.Add(@":(", Properties.Resources.sad_16x16);

            style.StartAnimation();

            fctb.OnTextChanged();
        }

        private void fctb_TextChanged(object sender, FastColoredTextBoxNS.TextChangedEventArgs e)
        {
            if (style == null) return;
            e.ChangedRange.ClearStyle(StyleIndex.All);
            foreach (var key in style.ImagesByText.Keys)
            {
                string pattern = Regex.Replace(key, RegexSpecSymbolsPattern, "\\$0");
                e.ChangedRange.SetStyle(style, pattern);
            }
        }
    }

    /// <summary>
    /// This class is used as text renderer for smiles
    /// </summary>
    class GifImageStyle : TextStyle
    {
        public Dictionary<string, Image> ImagesByText { get; private set; }
        FastColoredTextBox parent;
        System.Windows.Forms.Timer timer;

        public GifImageStyle(FastColoredTextBox parent)
            : base(null, null, FontStyle.Regular)
        {
            ImagesByText = new Dictionary<string, Image>();
            this.parent = parent;

            //create timer
            timer = new System.Windows.Forms.Timer();
            timer.Interval = 100;
            timer.Tick += (EventHandler)delegate
            {
                ImageAnimator.UpdateFrames();
                parent.Invalidate();
            };
            timer.Start();
        }

        public void StartAnimation()
        {
            foreach (var image in ImagesByText.Values)
                if (ImageAnimator.CanAnimate(image))
                    ImageAnimator.Animate(image, new EventHandler(OnFrameChanged));
        }

        void OnFrameChanged(object sender, EventArgs args)
        {
        }

        public override void Draw(Graphics gr, Point position, Range range)
        {
            string text = range.Text;
            int iChar = range.Start.iChar;

            while (text != "")
            {
                bool replaced = false;
                foreach (var pair in ImagesByText)
                {
                    if (text.StartsWith(pair.Key))
                    {
                        float k = (float)(pair.Key.Length * range.tb.CharWidth) / pair.Value.Width;
                        if (k > 1) 
                            k = 1f;
                        //
                        text = text.Substring(pair.Key.Length);
                        RectangleF rect = new RectangleF(position.X + range.tb.CharWidth * pair.Key.Length / 2 - pair.Value.Width * k/2, position.Y, pair.Value.Width * k, pair.Value.Height * k);
                        gr.DrawImage(pair.Value, rect);
                        position.Offset(range.tb.CharWidth * pair.Key.Length, 0);
                        replaced = true;
                        iChar+=pair.Key.Length;
                        break;
                    }
                }
                if (!replaced && text.Length>0)
                {
                    Range r = new Range(range.tb, iChar, range.Start.iLine, iChar+1, range.Start.iLine);
                    base.Draw(gr, position, r);
                    position.Offset(range.tb.CharWidth, 0);
                    text = text.Substring(1);
                }
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Software Developer Freelancer
Ukraine Ukraine
I am Pavеl Tоrgаshоv, and I live in Kyiv, Ukraine.
I've been developing software since 1998.
Main activities: processing of large volumes of data, statistics, computer vision and graphics.

Comments and Discussions