Click here to Skip to main content
15,891,184 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.2M   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;

namespace Tester
{
    public partial class JokeSample : Form
    {
        public JokeSample()
        {
            InitializeComponent();
            fctb.DefaultStyle = new JokeStyle();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            fctb.Invalidate();
        }
    }

    /// <summary>
    /// This class is used as text renderer
    /// </summary>
    class JokeStyle : TextStyle
    {
        public JokeStyle():base(null, null, FontStyle.Regular)
        {
        }

        public override void Draw(Graphics gr, Point position, Range range)
        {
            foreach (Place p in range)
            {
                int time = (int)(DateTime.Now.TimeOfDay.TotalMilliseconds/2);
                int angle = (int)(time % 360L);
                int angle2 = (int)((time - (p.iChar - range.Start.iChar)*20) % 360L)*2;
                int x =  position.X + (p.iChar - range.Start.iChar) * range.tb.CharWidth;
                Range r = range.tb.GetRange(p, new Place(p.iChar+1, p.iLine));
                Point point = new Point(x, position.Y + (int)(5 + 5 * Math.Sin(Math.PI * angle2 / 180)));
                gr.ResetTransform();
                gr.TranslateTransform(point.X + range.tb.CharWidth / 2, point.Y +range.tb.CharHeight / 2);
                gr.RotateTransform(angle);
                gr.ScaleTransform(0.8f, 0.8f);
                gr.TranslateTransform(- range.tb.CharWidth / 2, -range.tb.CharHeight / 2);
                base.Draw(gr, new Point(0, 0), r);
            }
            gr.ResetTransform();
        }
    }
}

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