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

Text Alignment Control for .NET

Rate me:
Please Sign up or sign in to vote.
4.22/5 (15 votes)
15 Sep 20042 min read 64.7K   2K   37  
An article on creating Text Alignment Control for .net
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;

namespace WTest
{
	/// <summary>
	/// Summary description for Canvas.
	/// </summary>
	public class Canvas : System.Windows.Forms.UserControl
	{
		/// <summary> 
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		private string _text = "Sample Text";
		private int _angle = 0;

		public Canvas()
		{
			// This call is required by the Windows.Forms Form Designer.
			InitializeComponent();

			SetStyle(ControlStyles.AllPaintingInWmPaint, true);
			SetStyle(ControlStyles.DoubleBuffer, true);
			SetStyle(ControlStyles.ResizeRedraw, true);
			SetStyle(ControlStyles.UserPaint, true);
			UpdateStyles();

		}

		public override string Text
		{
			get
			{
				return _text;
			}

			set
			{
				_text = value;
				Invalidate();

			}
		}

		public int Angle 
		{
			get
			{
				return _angle;
			}

			set
			{
				_angle = value;		
				Invalidate();
			}
		}

		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			// 
			// Canvas
			// 
			this.BackColor = System.Drawing.SystemColors.Window;
			this.Name = "Canvas";
			this.Size = new System.Drawing.Size(224, 128);
			this.Paint += new System.Windows.Forms.PaintEventHandler(this.Canvas_Paint);

		}
		#endregion

		private void Canvas_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
		{

			Point ptCenter = new Point (ClientRectangle.Width / 2, ClientRectangle.Height / 2);
		
			StringFormat format = new StringFormat(StringFormatFlags.NoClip);
			format.Alignment = StringAlignment.Center;
			format.LineAlignment = StringAlignment.Center;

			e.Graphics.TranslateTransform (ptCenter.X, ptCenter.Y);
			e.Graphics.RotateTransform (-_angle);

			e.Graphics.DrawString(_text, this.Font, Brushes.Black, 0, 0, format);
		}
	}
}

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
Software Developer (Senior) Software Kinetics
United Kingdom United Kingdom




Software Kinetics
are experts in developing customised and bespoke applications and have expertise in the development of desktop, mobile and internet applications on Windows.


We specialise in:

  • User Interface Design
  • Desktop Development
  • Windows Phone Development
  • Windows Presentation Framework
  • Windows Forms
  • Windows Communication Framework
  • Windows Services
  • Network Applications
  • Database Applications
  • Web Development
  • Web Services
  • Silverlight
  • ASP.net


Visit Software Kinetics

Comments and Discussions