Click here to Skip to main content
15,891,184 members
Articles / Programming Languages / C#

Customized Text - Orientated Controls in C# - Part I (Label Control)

Rate me:
Please Sign up or sign in to vote.
4.89/5 (50 votes)
26 Sep 20042 min read 199.8K   14.4K   92  
Label which can display text in any orientation and shape.
using System;
using System.Data;
using System.Drawing;
using System.Drawing.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.ComponentModel;

namespace CustomControl.OrientAbleTextControls
{
	/// <summary>
	/// This is a lable, in which you can set the text in any direction/angle
	/// </summary>
	
	#region Orientation

	//Orientation of the text
	
	public enum Orientation
	{
		Circle,
		Arc,
		Rotate
	}

	public enum Direction
	{
		Clockwise,
		AntiClockwise
	}
	

	#endregion

	public class OrientedTextLabel : System.Windows.Forms.Label
	{

		#region Variables

		private double rotationAngle;
		private string text;
		private Orientation textOrientation;
		private Direction textDirection;

		#endregion

		#region Constructor

		public OrientedTextLabel()
		{
			//Setting the initial condition.
			rotationAngle = 0d;
			textOrientation = Orientation.Rotate;
			this.Size = new Size(105,12);
		}

		#endregion

		#region Properties

		[Description("Rotation Angle"),Category("Appearance")]
		public double RotationAngle
		{
			get
			{
				return rotationAngle;
			}
			set
			{
				
				rotationAngle = value; 
				this.Invalidate();
			}
		}

		[Description("Kind of Text Orientation"),Category("Appearance")]
		public Orientation TextOrientation
		{
			get
			{
				return textOrientation;
			}
			set
			{
				
				textOrientation = value; 
				this.Invalidate();
			}
		}

		[Description("Direction of the Text"),Category("Appearance")]
		public Direction TextDirection
		{
			get
			{
				return textDirection;
			}
			set
			{
				
				textDirection = value; 
				this.Invalidate();
			}
		}

		[Description("Display Text"),Category("Appearance")]
		public override string Text
		{
			get
			{
				return text;
			}
			set
			{
				text = value;
				this.Invalidate();
			}
		}

		#endregion

		#region Method
		
		protected override void OnPaint(PaintEventArgs e)
		{
			Graphics graphics = e.Graphics;
			
			StringFormat stringFormat = new StringFormat();
			stringFormat.Alignment = StringAlignment.Center;
			stringFormat.Trimming = StringTrimming.None;
			
			Brush textBrush = new SolidBrush(this.ForeColor);
			
			//Getting the width and height of the text, which we are going to write
			float width = graphics.MeasureString(text,this.Font).Width;
			float height = graphics.MeasureString(text,this.Font).Height;

			//The radius is set to 0.9 of the width or height, b'cos not to 
			//hide and part of the text at any stage
			float radius = 0f;
			if(ClientRectangle.Width<ClientRectangle.Height)
			{
				radius = ClientRectangle.Width *0.9f/2;
			}
			else
			{
				radius = ClientRectangle.Height *0.9f/2;
			}

			//Setting the text according to the selection
			switch(textOrientation)
			{
				case Orientation.Arc :
				{
					//Arc angle must be get from the length of the text.
					float arcAngle = (2*width/radius)/text.Length;
					if(textDirection == Direction.Clockwise)
					{
						for(int i=0;i<text.Length;i++)
						{
						
							graphics.TranslateTransform(
								(float)(radius*(1 - Math.Cos(arcAngle*i + rotationAngle/180 * Math.PI))),
								(float)(radius*(1 - Math.Sin(arcAngle*i + rotationAngle/180*Math.PI))));
							graphics.RotateTransform((-90 + (float)rotationAngle + 180*arcAngle*i/(float)Math.PI));
							graphics.DrawString(text[i].ToString(), this.Font, textBrush, 0, 0);
							graphics.ResetTransform();
						}
					}
					else
					{
						for(int i=0;i<text.Length;i++)
						{
						
							graphics.TranslateTransform(
								(float)(radius*(1 - Math.Cos(arcAngle*i + rotationAngle/180*Math.PI))),
								(float)(radius*(1 + Math.Sin(arcAngle*i + rotationAngle/180*Math.PI))));
							graphics.RotateTransform((-90 - (float)rotationAngle - 180*arcAngle*i/(float)Math.PI));
							graphics.DrawString(text[i].ToString(), this.Font, textBrush, 0, 0);
							graphics.ResetTransform();
					
						}
					}
					break;
				}
				case Orientation.Circle :
				{
					if(textDirection == Direction.Clockwise)
					{
							for(int i=0;i<text.Length;i++)
							{
								graphics.TranslateTransform(
									(float)(radius*(1 - Math.Cos((2*Math.PI/text.Length)*i + rotationAngle/180*Math.PI))),
									(float)(radius*(1 - Math.Sin((2*Math.PI/text.Length)*i + rotationAngle/180*Math.PI))));
								graphics.RotateTransform(-90 + (float)rotationAngle + (360/text.Length)*i);
								graphics.DrawString(text[i].ToString(), this.Font, textBrush, 0, 0);
								graphics.ResetTransform();
							}
					}
					else
					{
						for(int i=0;i<text.Length;i++)
						{
							graphics.TranslateTransform(
								(float)(radius*(1 - Math.Cos((2*Math.PI/text.Length)*i + rotationAngle/180*Math.PI))),
								(float)(radius*(1 + Math.Sin((2*Math.PI/text.Length)*i + rotationAngle/180*Math.PI))));
							graphics.RotateTransform(-90 - (float)rotationAngle - (360/text.Length)*i);
							graphics.DrawString(text[i].ToString(), this.Font, textBrush, 0, 0);
							graphics.ResetTransform();
						}
						
					}
					break;
				}
				case Orientation.Rotate :
				{
					//For rotation, who about rotation?
					double angle = (rotationAngle/180)*Math.PI;
					graphics.TranslateTransform(
						(ClientRectangle.Width+(float)(height*Math.Sin(angle))-(float)(width*Math.Cos(angle)))/2,
						(ClientRectangle.Height-(float)(height*Math.Cos(angle))-(float)(width*Math.Sin(angle)))/2);
					graphics.RotateTransform((float)rotationAngle);
					graphics.DrawString(text,this.Font,textBrush,0,0);
					graphics.ResetTransform();

					break;
				}
			}
		}
		#endregion

	}
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Singapore Singapore
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions