Draw the Text in C#

 

In C#, we can display the text by using label or button in its own format. But sometimes we may want to display text in our own format. We can display the text in our own format by using Graphics class.

 

In this article we display the text in our own format by using Graphics class through Label control. We need some control to change display format of text, for that we will use Label control.

 

Create Windows Application and add Label control to the form. We have to write the event for Label control Paint method as shown below.

 

using System.Drawing;

using System.Drawing.Text;

using System.Windows.Forms;

 

namespace CSharpDrawText

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

 

        private void lbl1_Paint(object sender, PaintEventArgs e)

        {

            Draw((Label)sender, e.Graphics);

        }

 

        private void Draw(Label label, Graphics g)

        {

            Rectangle r = label.ClientRectangle;

            r.X -= 1;

            r.Y -= 1;

            g.DrawString(label.Text, label.Font, Brushes.Red, r, GetFormat(label));

        }

 

        private StringFormat GetFormat(Label label)

        {

            StringFormat format = new StringFormat();

            SetTextLineAlignment(label.TextAlign, format);

            SetTextAlignment(label, format);

            //SetFlags(label.RightToLeft, format);

            //SetMnemonic(label.UseMnemonic, format);

            return format;

        }

 

        private void SetTextLineAlignment(ContentAlignment TextAlign, StringFormat format)

        {

            switch (TextAlign)

            {

                case ContentAlignment.BottomLeft:

                    format.LineAlignment = StringAlignment.Far;

                    break;

                case ContentAlignment.MiddleLeft:

                    format.LineAlignment = StringAlignment.Center;

                    break;

 

                case ContentAlignment.TopLeft:

                    format.LineAlignment = StringAlignment.Near;

                    break;

 

                case ContentAlignment.BottomCenter:

                    format.LineAlignment = StringAlignment.Far;

                    break;

 

                case ContentAlignment.MiddleCenter:

                    format.LineAlignment = StringAlignment.Center;

                    break;

 

                case ContentAlignment.TopCenter:

                    format.LineAlignment = StringAlignment.Near;

                    break;

                case ContentAlignment.BottomRight:

                    format.LineAlignment = StringAlignment.Far;

                    break;

 

                case ContentAlignment.MiddleRight:

                    format.LineAlignment = StringAlignment.Center;

                    break;

 

                case ContentAlignment.TopRight:

                    format.LineAlignment = StringAlignment.Near;

                    break;

            }

        }

 

        private void SetTextAlignment(Label label, StringFormat format)

        {

            switch (label.TextAlign)

            {

                case ContentAlignment.BottomLeft:

                    format.Alignment = StringAlignment.Near;

                    break;

                case ContentAlignment.MiddleLeft:

                    format.Alignment = StringAlignment.Near;

                    break;

 

                case ContentAlignment.TopLeft:

                    format.Alignment = StringAlignment.Near;

                    break;

 

                case ContentAlignment.BottomCenter:

                    format.Alignment = StringAlignment.Center;

                    break;

 

                case ContentAlignment.MiddleCenter:

                    format.Alignment = StringAlignment.Center;

                    break;

 

                case ContentAlignment.TopCenter:

                    format.Alignment = StringAlignment.Center;

                    break;

 

                case ContentAlignment.BottomRight:

                    format.Alignment = StringAlignment.Far;

                    break;

 

                case ContentAlignment.MiddleRight:

                    format.Alignment = StringAlignment.Far;

                    break;

 

                case ContentAlignment.TopRight:

                    format.Alignment = StringAlignment.Far;

                    break;

            }

        }

 

        private void SetTextFlags(RightToLeft rightToLeft, StringFormat format)

        {

            if (rightToLeft == RightToLeft.Yes)

                format.FormatFlags |= StringFormatFlags.DirectionRightToLeft;

        }

 

        private void SetTextMnemonic(bool UseMnemonic, StringFormat format)

        {

            if (UseMnemonic)

                format.HotkeyPrefix = HotkeyPrefix.Show;

        }

    }

}

 

The output of the application is as shown below.


 

                                                                                                                        CSharpDrawText.zip (43.19 kb)