C# Oriented label! [Custom control] - EXAMPLE

MysteryGuest

Registered Member
Joined
Mar 7, 2013
Messages
64
Reaction score
18
Hey guys, here yet another example! haven't be able to post more example because of some projects that had to be done, anyway let's get started shall we?

This custom control can do cool things, basically it's a label which has some extra feature's like; Clock Wise, Anti-Clock Wise, Rotate, Arc, Circle... You can also change the angle of the text. It's definitely not a super release but it's nice to have:)

I will post the download link of the file ass well just include it and build you project, after you will see it in your toolbox!

I also will post the source down here.
Code:
using System;
using System.Drawing;
using System.Globalization;
using System.Windows.Forms;
using System.ComponentModel;

namespace AS___MysteryGuest_INC.GRAPHIC.LABEL
{
	#region Orientation
	public enum Orientation
	{
		Circle,
		Arc,
		Rotate
	}

	public enum Direction
	{
		Clockwise,
		AntiClockwise
	}
	#endregion

	public class OrientedTextLabel : Label
	{

		#region Variables
		private double _rotationAngle;
		private string _text;
		private Orientation _textOrientation;
		private Direction _textDirection;
		#endregion

		#region Constructor
		public OrientedTextLabel()
		{
			_rotationAngle = 0d;
			_textOrientation = Orientation.Rotate;
			Size = new Size(105,12);
		}
		#endregion

		#region Properties
		[Description("Rotation Angle"),Category("Appearance")]
		public double RotationAngle
		{
			get
			{
				return _rotationAngle;
			}
			set
			{
				
				_rotationAngle = value; 
				Invalidate();
			}
		}

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

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

		[Description("Display Text"),Category("Appearance")]
		public override string Text
		{
			get
			{
				return _text;
			}
			set
			{
				_text = value;
				Invalidate();
			}
		}
		#endregion

		#region Method
		protected override void OnPaint(PaintEventArgs e)
		{
			var graphics = e.Graphics;
			
			new StringFormat {Alignment = StringAlignment.Center, Trimming = StringTrimming.None};
		    Brush textBrush = new SolidBrush(ForeColor);
			var width = graphics.MeasureString(_text,Font).Width;
			var height = graphics.MeasureString(_text,Font).Height;
		    var radius = ClientRectangle.Width < ClientRectangle.Height
		                       ? ClientRectangle.Width*0.9f/2
		                       : ClientRectangle.Height*0.9f/2;
			switch(_textOrientation)
			{
				case Orientation.Arc :
				{
					var arcAngle = (2*width/radius)/_text.Length;
					if(_textDirection == Direction.Clockwise)
					{
						for(var 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(CultureInfo.InvariantCulture), Font, textBrush, 0, 0);
							graphics.ResetTransform();
						}
					}
					else
					{
						for(var 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(CultureInfo.InvariantCulture), Font, textBrush, 0, 0);
							graphics.ResetTransform();
						}
					}
					break;
				}
				case Orientation.Circle :
			        {
			            switch (_textDirection)
			            {
			                case Direction.Clockwise:
			                    for(var 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(CultureInfo.InvariantCulture), Font, textBrush, 0, 0);
			                        graphics.ResetTransform();
			                    }
			                    break;
			                default:
			                    for(var 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(CultureInfo.InvariantCulture), Font, textBrush, 0, 0);
			                        graphics.ResetTransform();
			                    }
			                    break;
			            }
			            break;
			        }
			    case Orientation.Rotate :
				{
					var 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,Font,textBrush,0,0);
					graphics.ResetTransform();

					break;
				}
			}
		}
		#endregion

	}
}


DOWNLOAD LINK: http://mysteryguest-inc.com/BHW/C#/OrientedTextLabel.zip
 
Back
Top