Click here to Skip to main content
15,894,896 members
Articles / Desktop Programming / Windows Forms

Shape Control for .NET

Rate me:
Please Sign up or sign in to vote.
4.84/5 (170 votes)
23 Mar 2017CPOL10 min read 381.4K   21.7K   301  
Implementing shape control that supports transparency, custom design-time editors and simple animation
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;


namespace TestShapeControl
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private ShapeControl.CustomControl1 shapeControl1;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;
		private System.Windows.Forms.Panel panel1;
		private Bitmap bm;
		private int sx,sy;
		private ShapeControl.CustomControl1 shapeControl3;
		private ShapeControl.CustomControl1 shapeControl4;
        private ShapeControl.CustomControl1 shapeControl5;
        private ShapeControl.CustomControl1 shapeControl7;
        private ShapeControl.CustomControl1 shapeControl6;
        private ShapeControl.CustomControl1 customControl11;
		private ShapeControl.CustomControl1 shapeControl2;


		public Form1()
		{
			
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
			bm=new Bitmap(this.panel1.Width,this.panel1.Height);
			Graphics g=System.Drawing.Graphics.FromImage(bm);
			g.FillRectangle(Brushes.LightBlue,new Rectangle(0,0,this.panel1.Width,this.panel1.Height));
			StringFormat sf=new StringFormat();
			sf.Alignment=StringAlignment.Center;
			sf.LineAlignment=StringAlignment.Center;
			g.DrawString("THIS IS THE TEST BACKGROUND",new Font("Arial",18,FontStyle.Bold),Brushes.Brown,
				        new Rectangle(new Point(0,0),new Size(this.panel1.Width,this.panel1.Height)),sf);
			this.panel1.BackgroundImage =bm;

		}

		/// <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 Windows Form 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()
		{
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
            this.panel1 = new System.Windows.Forms.Panel();
            this.shapeControl1 = new ShapeControl.CustomControl1();
            this.shapeControl7 = new ShapeControl.CustomControl1();
            this.shapeControl5 = new ShapeControl.CustomControl1();
            this.shapeControl4 = new ShapeControl.CustomControl1();
            this.shapeControl3 = new ShapeControl.CustomControl1();
            this.shapeControl2 = new ShapeControl.CustomControl1();
            this.shapeControl6 = new ShapeControl.CustomControl1();
            this.customControl11 = new ShapeControl.CustomControl1();
            this.panel1.SuspendLayout();
            this.SuspendLayout();
            // 
            // panel1
            // 
            this.panel1.Controls.Add(this.shapeControl1);
            this.panel1.Location = new System.Drawing.Point(8, 16);
            this.panel1.Name = "panel1";
            this.panel1.Size = new System.Drawing.Size(245, 280);
            this.panel1.TabIndex = 2;
            // 
            // shapeControl1
            // 
            this.shapeControl1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(255)))));
            this.shapeControl1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
            this.shapeControl1.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(98)))), ((int)(((byte)(255)))), ((int)(((byte)(0)))), ((int)(((byte)(0)))));
            this.shapeControl1.BorderStyle = System.Drawing.Drawing2D.DashStyle.Solid;
            this.shapeControl1.BorderWidth = 0;
            this.shapeControl1.CenterColor = System.Drawing.Color.FromArgb(((int)(((byte)(85)))), ((int)(((byte)(255)))), ((int)(((byte)(253)))), ((int)(((byte)(255)))));
            this.shapeControl1.Font = new System.Drawing.Font("Arial", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.shapeControl1.ForeColor = System.Drawing.Color.Yellow;
            this.shapeControl1.Location = new System.Drawing.Point(55, 39);
            this.shapeControl1.Name = "shapeControl1";
            this.shapeControl1.Shape = ShapeControl.ShapeType.Diamond;
            this.shapeControl1.ShapeImage = null;
            this.shapeControl1.Size = new System.Drawing.Size(131, 143);
            this.shapeControl1.SurroundColor = System.Drawing.Color.FromArgb(((int)(((byte)(69)))), ((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(0)))));
            this.shapeControl1.TabIndex = 0;
            this.shapeControl1.Text = "Transparency Test Drag Me Around";
            this.shapeControl1.UseGradient = true;
            this.shapeControl1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.shapeControl1_MouseMove);
            this.shapeControl1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.shapeControl1_MouseDown);
            // 
            // shapeControl7
            // 
            this.shapeControl7.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(0)))));
            this.shapeControl7.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
            this.shapeControl7.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(0)))), ((int)(((byte)(0)))));
            this.shapeControl7.BorderStyle = System.Drawing.Drawing2D.DashStyle.Solid;
            this.shapeControl7.BorderWidth = 1;
            this.shapeControl7.CenterColor = System.Drawing.Color.FromArgb(((int)(((byte)(100)))), ((int)(((byte)(255)))), ((int)(((byte)(0)))), ((int)(((byte)(0)))));
            this.shapeControl7.Font = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Bold);
            this.shapeControl7.Location = new System.Drawing.Point(343, 55);
            this.shapeControl7.Name = "shapeControl7";
            this.shapeControl7.Shape = ShapeControl.ShapeType.CustomPolygon;
            this.shapeControl7.ShapeImage = null;
            this.shapeControl7.Size = new System.Drawing.Size(20, 22);
            this.shapeControl7.SurroundColor = System.Drawing.Color.FromArgb(((int)(((byte)(100)))), ((int)(((byte)(0)))), ((int)(((byte)(255)))), ((int)(((byte)(255)))));
            this.shapeControl7.TabIndex = 8;
            this.shapeControl7.UseGradient = false;
            // 
            // shapeControl5
            // 
            this.shapeControl5.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(198)))), ((int)(((byte)(227)))), ((int)(((byte)(251)))), ((int)(((byte)(72)))));
            this.shapeControl5.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
            this.shapeControl5.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(0)))), ((int)(((byte)(0)))));
            this.shapeControl5.BorderStyle = System.Drawing.Drawing2D.DashStyle.Dot;
            this.shapeControl5.BorderWidth = 3;
            this.shapeControl5.CenterColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(0)))), ((int)(((byte)(0)))));
            this.shapeControl5.Font = new System.Drawing.Font("Arial", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.shapeControl5.Location = new System.Drawing.Point(219, 12);
            this.shapeControl5.Name = "shapeControl5";
            this.shapeControl5.Shape = ShapeControl.ShapeType.BallonSE;
            this.shapeControl5.ShapeImage = null;
            this.shapeControl5.Size = new System.Drawing.Size(144, 106);
            this.shapeControl5.SurroundColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(255)))));
            this.shapeControl5.TabIndex = 6;
            this.shapeControl5.Text = "Hello, I am Tommy";
            this.shapeControl5.UseGradient = false;
            this.shapeControl5.Visible = false;
            // 
            // shapeControl4
            // 
            this.shapeControl4.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(124)))), ((int)(((byte)(92)))), ((int)(((byte)(159)))), ((int)(((byte)(83)))));
            this.shapeControl4.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
            this.shapeControl4.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(177)))), ((int)(((byte)(131)))), ((int)(((byte)(255)))), ((int)(((byte)(4)))));
            this.shapeControl4.BorderStyle = System.Drawing.Drawing2D.DashStyle.Solid;
            this.shapeControl4.BorderWidth = 3;
            this.shapeControl4.CenterColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(0)))), ((int)(((byte)(0)))));
            this.shapeControl4.Font = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Bold);
            this.shapeControl4.Location = new System.Drawing.Point(309, 211);
            this.shapeControl4.Name = "shapeControl4";
            this.shapeControl4.Shape = ShapeControl.ShapeType.RoundedRectangle;
            this.shapeControl4.ShapeImage = null;
            this.shapeControl4.Size = new System.Drawing.Size(97, 35);
            this.shapeControl4.SurroundColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(255)))));
            this.shapeControl4.TabIndex = 5;
            this.shapeControl4.Text = "Click Me!";
            this.shapeControl4.UseGradient = false;
            this.shapeControl4.Click += new System.EventHandler(this.shapeControl4_Click);
            // 
            // shapeControl3
            // 
            this.shapeControl3.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(124)))), ((int)(((byte)(92)))), ((int)(((byte)(159)))), ((int)(((byte)(83)))));
            this.shapeControl3.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
            this.shapeControl3.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(177)))), ((int)(((byte)(131)))), ((int)(((byte)(255)))), ((int)(((byte)(4)))));
            this.shapeControl3.BorderStyle = System.Drawing.Drawing2D.DashStyle.Solid;
            this.shapeControl3.BorderWidth = 3;
            this.shapeControl3.CenterColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(0)))), ((int)(((byte)(0)))));
            this.shapeControl3.Font = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Bold);
            this.shapeControl3.Location = new System.Drawing.Point(309, 261);
            this.shapeControl3.Name = "shapeControl3";
            this.shapeControl3.Shape = ShapeControl.ShapeType.RoundedRectangle;
            this.shapeControl3.ShapeImage = null;
            this.shapeControl3.Size = new System.Drawing.Size(97, 35);
            this.shapeControl3.SurroundColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(255)))));
            this.shapeControl3.TabIndex = 4;
            this.shapeControl3.Text = "Close";
            this.shapeControl3.UseGradient = false;
            this.shapeControl3.Click += new System.EventHandler(this.shapeControl3_Click);
            // 
            // shapeControl2
            // 
            this.shapeControl2.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(255)))));
            this.shapeControl2.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
            this.shapeControl2.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(118)))), ((int)(((byte)(133)))), ((int)(((byte)(4)))), ((int)(((byte)(9)))));
            this.shapeControl2.BorderStyle = System.Drawing.Drawing2D.DashStyle.Solid;
            this.shapeControl2.BorderWidth = 0;
            this.shapeControl2.CenterColor = System.Drawing.Color.FromArgb(((int)(((byte)(90)))), ((int)(((byte)(163)))), ((int)(((byte)(126)))), ((int)(((byte)(59)))));
            this.shapeControl2.Font = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Bold);
            this.shapeControl2.Location = new System.Drawing.Point(277, 45);
            this.shapeControl2.Name = "shapeControl2";
            this.shapeControl2.Shape = ShapeControl.ShapeType.TriangleUp;
            this.shapeControl2.ShapeImage = null;
            this.shapeControl2.Size = new System.Drawing.Size(152, 32);
            this.shapeControl2.SurroundColor = System.Drawing.Color.FromArgb(((int)(((byte)(122)))), ((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(255)))));
            this.shapeControl2.TabIndex = 3;
            this.shapeControl2.UseGradient = true;
            // 
            // shapeControl6
            // 
            this.shapeControl6.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(221)))), ((int)(((byte)(152)))), ((int)(((byte)(53)))));
            this.shapeControl6.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(0)))), ((int)(((byte)(0)))));
            this.shapeControl6.BorderStyle = System.Drawing.Drawing2D.DashStyle.Solid;
            this.shapeControl6.BorderWidth = 0;
            this.shapeControl6.CenterColor = System.Drawing.Color.FromArgb(((int)(((byte)(240)))), ((int)(((byte)(202)))), ((int)(((byte)(91)))), ((int)(((byte)(171)))));
            this.shapeControl6.Font = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Bold);
            this.shapeControl6.Location = new System.Drawing.Point(421, 12);
            this.shapeControl6.Name = "shapeControl6";
            this.shapeControl6.Shape = ShapeControl.ShapeType.Ellipse;
            this.shapeControl6.ShapeImage = ((System.Drawing.Image)(resources.GetObject("shapeControl6.ShapeImage")));
            this.shapeControl6.Size = new System.Drawing.Size(117, 87);
            this.shapeControl6.SurroundColor = System.Drawing.Color.FromArgb(((int)(((byte)(53)))), ((int)(((byte)(198)))), ((int)(((byte)(74)))), ((int)(((byte)(0)))));
            this.shapeControl6.TabIndex = 9;
            this.shapeControl6.UseGradient = true;
            // 
            // customControl11
            // 
            this.customControl11.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(255)))));
            this.customControl11.BorderColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(0)))), ((int)(((byte)(0)))));
            this.customControl11.BorderStyle = System.Drawing.Drawing2D.DashStyle.Solid;
            this.customControl11.BorderWidth = 0;
            this.customControl11.CenterColor = System.Drawing.Color.FromArgb(((int)(((byte)(100)))), ((int)(((byte)(255)))), ((int)(((byte)(0)))), ((int)(((byte)(0)))));
            this.customControl11.Font = new System.Drawing.Font("Arial", 12F, System.Drawing.FontStyle.Bold);
            this.customControl11.Location = new System.Drawing.Point(309, 83);
            this.customControl11.Name = "customControl11";
            this.customControl11.Shape = ShapeControl.ShapeType.Ellipse;
            this.customControl11.ShapeImage = null;
            this.customControl11.Size = new System.Drawing.Size(93, 92);
            this.customControl11.SurroundColor = System.Drawing.Color.FromArgb(((int)(((byte)(194)))), ((int)(((byte)(255)))), ((int)(((byte)(171)))), ((int)(((byte)(11)))));
            this.customControl11.TabIndex = 9;
            this.customControl11.UseGradient = true;
            // 
            // Form1
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(543, 326);
            this.ControlBox = false;
            this.Controls.Add(this.shapeControl5);
            this.Controls.Add(this.customControl11);
            this.Controls.Add(this.shapeControl6);
            this.Controls.Add(this.shapeControl7);
            this.Controls.Add(this.shapeControl4);
            this.Controls.Add(this.shapeControl3);
            this.Controls.Add(this.shapeControl2);
            this.Controls.Add(this.panel1);
            this.Name = "Form1";
            this.Text = "Test Shape Control";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.panel1.ResumeLayout(false);
            this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		private void shapeControl1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			if(e.Button.Equals(MouseButtons.Left))
			{
				sx=e.X;
				sy=e.Y;
			}
		}

		private void shapeControl1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			if(e.Button.Equals(MouseButtons.Left))
			{
				((Control) sender).Left =((Control) sender).Left+(e.X -sx);
				((Control) sender).Top =((Control) sender).Top+(e.Y -sy);
			}
		}

		private void Form1_Load(object sender, System.EventArgs e)
		{
		
		}

		private void shapeControl4_Click(object sender, System.EventArgs e)
		{
			shapeControl5.Visible=true;
			this.Refresh();
			System.Threading.Thread.Sleep(1000);
            shapeControl5.Visible=false;
			this.Refresh();

		}

		private void shapeControl3_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}


	}
}

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 Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Singapore Singapore
Coder. Hacker. Fixer.

Comments and Discussions