RegionMaster Controls

The RegionMaster Controls allow users to create non-rectangular, discontiguous top level windows without writing any code in the Visual Studio .NET form designer.

C# (360.1 KB)
 
 
 
 
 
4 Star
(1)
2,649 times
Add to favorites
2/2/2011
E-mail Twitter del.icio.us Digg Facebook

Solution explorer

C#
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace TestApp
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private Microsoft.Samples.CurvedPanel curvedPanel1;
		private Microsoft.Samples.RegionBuilder regionBuilder1;
		private System.Windows.Forms.Timer timer1;
		private System.ComponentModel.IContainer components;
		private Microsoft.Samples.CurvedPanel curvedPanel2;
		private System.Windows.Forms.ContextMenu contextMenu1;
		private System.Windows.Forms.MenuItem menuItemShowBackground;
		private System.Windows.Forms.MenuItem menuItemTopMost;
		private System.Windows.Forms.MenuItem menuItemTransparency;
		private System.Windows.Forms.MenuItem menuItem20;
		private System.Windows.Forms.MenuItem menuItem30;
		private System.Windows.Forms.MenuItem menuItem40;
		private System.Windows.Forms.MenuItem menuItem50;
		private System.Windows.Forms.MenuItem menuItem60;
		private System.Windows.Forms.MenuItem menuItem70;
		private System.Windows.Forms.MenuItem menuItem80;
		private System.Windows.Forms.MenuItem menuItem90;
		private System.Windows.Forms.MenuItem menuItemOpaque;
		private Microsoft.Samples.SolidClock solidClock1;

		// Fields
		private bool increasing;
		private double baseOpacity;
		private Point formMove;
		private bool mouseDown;

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

			increasing = true;
			baseOpacity = .5f;
			formMove = Point.Empty;
			mouseDown = false;
		}

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

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

		#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()
		{
			this.components = new System.ComponentModel.Container();
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
			this.curvedPanel1 = new Microsoft.Samples.CurvedPanel();
			this.solidClock1 = new Microsoft.Samples.SolidClock();
			this.regionBuilder1 = new Microsoft.Samples.RegionBuilder(this.components);
			this.timer1 = new System.Windows.Forms.Timer(this.components);
			this.curvedPanel2 = new Microsoft.Samples.CurvedPanel();
			this.contextMenu1 = new System.Windows.Forms.ContextMenu();
			this.menuItemShowBackground = new System.Windows.Forms.MenuItem();
			this.menuItemTopMost = new System.Windows.Forms.MenuItem();
			this.menuItemTransparency = new System.Windows.Forms.MenuItem();
			this.menuItem20 = new System.Windows.Forms.MenuItem();
			this.menuItem30 = new System.Windows.Forms.MenuItem();
			this.menuItem40 = new System.Windows.Forms.MenuItem();
			this.menuItem50 = new System.Windows.Forms.MenuItem();
			this.menuItem60 = new System.Windows.Forms.MenuItem();
			this.menuItem70 = new System.Windows.Forms.MenuItem();
			this.menuItem80 = new System.Windows.Forms.MenuItem();
			this.menuItem90 = new System.Windows.Forms.MenuItem();
			this.menuItemOpaque = new System.Windows.Forms.MenuItem();
			this.curvedPanel1.SuspendLayout();
			this.SuspendLayout();
			// 
			// curvedPanel1
			// 
			this.curvedPanel1.AddToFormRegion = true;
			this.curvedPanel1.BackColor = System.Drawing.Color.Transparent;
			this.curvedPanel1.BottomMargin = 0;
			this.curvedPanel1.Controls.AddRange(new System.Windows.Forms.Control[] {
																					   this.solidClock1});
			this.curvedPanel1.FillColor = System.Drawing.SystemColors.Control;
			this.curvedPanel1.FillTransparency = 0.5;
			this.curvedPanel1.FormBehavior = Microsoft.Samples.FormBehaviors.Drag;
			this.curvedPanel1.LeftMargin = 0;
			this.curvedPanel1.LineThickness = 0;
			this.curvedPanel1.Location = new System.Drawing.Point(8, 32);
			this.curvedPanel1.LowerLeftCornerType = Microsoft.Samples.CornerTypes.Curve;
			this.curvedPanel1.LowerRightCornerType = Microsoft.Samples.CornerTypes.Curve;
			this.curvedPanel1.Name = "curvedPanel1";
			this.curvedPanel1.RightMargin = 0;
			this.curvedPanel1.Size = new System.Drawing.Size(224, 216);
			this.curvedPanel1.TabIndex = 0;
			this.curvedPanel1.Text = "curvedPanel1";
			this.curvedPanel1.TopMargin = 0;
			this.curvedPanel1.UpperLeftCornerType = Microsoft.Samples.CornerTypes.Curve;
			this.curvedPanel1.UpperRightCornerType = Microsoft.Samples.CornerTypes.Curve;
			this.curvedPanel1.MouseEnter += new System.EventHandler(this.curvedPanel1_MouseEnter);
			this.curvedPanel1.MouseLeave += new System.EventHandler(this.curvedPanel1_MouseLeave);
			// 
			// solidClock1
			// 
			this.solidClock1.ClockInterval = Microsoft.Samples.ClockInterval.HoursAndMinutes;
			this.solidClock1.ForeColor = System.Drawing.SystemColors.ControlDarkDark;
			this.solidClock1.IntraArcPadding = 9;
			this.solidClock1.Location = new System.Drawing.Point(16, 16);
			this.solidClock1.Name = "solidClock1";
			this.solidClock1.OuterArcThickness = 19;
			this.solidClock1.Size = new System.Drawing.Size(192, 184);
			this.solidClock1.TabIndex = 0;
			this.solidClock1.Text = "solidClock1";
			this.solidClock1.Paint += new System.Windows.Forms.PaintEventHandler(this.solidClock1_Paint);
			this.solidClock1.MouseEnter += new System.EventHandler(this.solidClock1_MouseEnter);
			this.solidClock1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.solidClock1_MouseUp);
			this.solidClock1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.solidClock1_MouseMove);
			this.solidClock1.MouseLeave += new System.EventHandler(this.curvedPanel2_MouseLeave);
			this.solidClock1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.solidClock1_MouseDown);
			// 
			// regionBuilder1
			// 
			this.regionBuilder1.MakeFormRegion = true;
			this.regionBuilder1.ParentForm = this;
			// 
			// timer1
			// 
			this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
			// 
			// curvedPanel2
			// 
			this.curvedPanel2.BackColor = System.Drawing.Color.Transparent;
			this.curvedPanel2.BottomMargin = 0;
			this.curvedPanel2.FillColor = System.Drawing.SystemColors.Control;
			this.curvedPanel2.FillTransparency = 0.5;
			this.curvedPanel2.ForeColor = System.Drawing.SystemColors.ControlDarkDark;
			this.curvedPanel2.FormBehavior = Microsoft.Samples.FormBehaviors.Close;
			this.curvedPanel2.FormBehaviorGlyphSize = 17;
			this.curvedPanel2.LeftMargin = 0;
			this.curvedPanel2.LineThickness = 0;
			this.curvedPanel2.Location = new System.Drawing.Point(224, 0);
			this.curvedPanel2.LowerLeftCornerRadius = 78;
			this.curvedPanel2.LowerLeftCornerType = Microsoft.Samples.CornerTypes.Curve;
			this.curvedPanel2.LowerRightCornerRadius = 78;
			this.curvedPanel2.LowerRightCornerType = Microsoft.Samples.CornerTypes.Curve;
			this.curvedPanel2.Name = "curvedPanel2";
			this.curvedPanel2.RightMargin = 0;
			this.curvedPanel2.Size = new System.Drawing.Size(40, 40);
			this.curvedPanel2.TabIndex = 1;
			this.curvedPanel2.Text = "curvedPanel2";
			this.curvedPanel2.TopMargin = 0;
			this.curvedPanel2.UpperLeftCornerRadius = 78;
			this.curvedPanel2.UpperLeftCornerType = Microsoft.Samples.CornerTypes.Curve;
			this.curvedPanel2.UpperRightCornerRadius = 78;
			this.curvedPanel2.UpperRightCornerType = Microsoft.Samples.CornerTypes.Curve;
			this.curvedPanel2.MouseEnter += new System.EventHandler(this.curvedPanel2_MouseEnter);
			this.curvedPanel2.MouseLeave += new System.EventHandler(this.curvedPanel2_MouseLeave);
			// 
			// contextMenu1
			// 
			this.contextMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																						 this.menuItemShowBackground,
																						 this.menuItemTopMost,
																						 this.menuItemTransparency});
			// 
			// menuItemShowBackground
			// 
			this.menuItemShowBackground.Checked = true;
			this.menuItemShowBackground.Index = 0;
			this.menuItemShowBackground.Text = "Show Background";
			this.menuItemShowBackground.Click += new System.EventHandler(this.menuItemShowBackground_Click);
			// 
			// menuItemTopMost
			// 
			this.menuItemTopMost.Checked = true;
			this.menuItemTopMost.Index = 1;
			this.menuItemTopMost.Text = "Top Most";
			this.menuItemTopMost.Click += new System.EventHandler(this.menuItemTopMost_Click);
			// 
			// menuItemTransparency
			// 
			this.menuItemTransparency.Index = 2;
			this.menuItemTransparency.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
																								 this.menuItem20,
																								 this.menuItem30,
																								 this.menuItem40,
																								 this.menuItem50,
																								 this.menuItem60,
																								 this.menuItem70,
																								 this.menuItem80,
																								 this.menuItem90,
																								 this.menuItemOpaque});
			this.menuItemTransparency.Text = "Transparency";
			// 
			// menuItem20
			// 
			this.menuItem20.Index = 0;
			this.menuItem20.Text = "20%";
			// 
			// menuItem30
			// 
			this.menuItem30.Index = 1;
			this.menuItem30.Text = "30%";
			// 
			// menuItem40
			// 
			this.menuItem40.Index = 2;
			this.menuItem40.Text = "40%";
			// 
			// menuItem50
			// 
			this.menuItem50.Checked = true;
			this.menuItem50.Index = 3;
			this.menuItem50.Text = "50%";
			// 
			// menuItem60
			// 
			this.menuItem60.Index = 4;
			this.menuItem60.Text = "60%";
			// 
			// menuItem70
			// 
			this.menuItem70.Index = 5;
			this.menuItem70.Text = "70%";
			// 
			// menuItem80
			// 
			this.menuItem80.Index = 6;
			this.menuItem80.Text = "80%";
			// 
			// menuItem90
			// 
			this.menuItem90.Index = 7;
			this.menuItem90.Text = "90%";
			// 
			// menuItemOpaque
			// 
			this.menuItemOpaque.Index = 8;
			this.menuItemOpaque.Text = "Opaque";
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.BackgroundImage = ((System.Drawing.Bitmap)(resources.GetObject("$this.BackgroundImage")));
			this.ClientSize = new System.Drawing.Size(272, 254);
			this.ContextMenu = this.contextMenu1;
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.curvedPanel2,
																		  this.curvedPanel1});
			this.Name = "Form1";
			this.Opacity = 0.5;
			this.ShowInTaskbar = false;
			this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
			this.Text = "Form1";
			this.TopMost = true;
			this.Load += new System.EventHandler(this.Form1_Load);
			this.curvedPanel1.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		#region Event handlers
		private void curvedPanel1_MouseEnter(object sender, System.EventArgs e)
		{
			increasing = true;
			this.timer1.Enabled = true;
		}

		private void curvedPanel1_MouseLeave(object sender, System.EventArgs e)
		{
			increasing = false;
			this.timer1.Enabled = true;
		}

		private void curvedPanel2_MouseEnter(object sender, System.EventArgs e)
		{
			increasing = true;
			this.timer1.Enabled = true;
		}

		private void curvedPanel2_MouseLeave(object sender, System.EventArgs e)
		{
			increasing = false;
			this.timer1.Enabled = true;
		}

		private void Form1_Load(object sender, System.EventArgs e)
		{
			// Set location based on desktop size
			this.Location = new Point(SystemInformation.PrimaryMonitorMaximizedWindowSize.Width - this.Width, SystemInformation.PrimaryMonitorMaximizedWindowSize.Height - this.Height);

			// Hook up the transparency menu item event handlers
			foreach(MenuItem mi in this.menuItemTransparency.MenuItems) 
			{
				mi.Click += new EventHandler(this.menuItemTransparancy_Click);
			}

			// Invoke the event handler for the "Show Background" menu item.
			// This disbales the background on start up
			this.menuItemShowBackground_Click(this.menuItemShowBackground, new EventArgs());
		}

		private void menuItemShowBackground_Click(object sender, System.EventArgs e)
		{
			MenuItem mi = sender as MenuItem;
			if (mi != null) 
			{
				mi.Checked = !mi.Checked;

				if(!mi.Checked) 
				{
					this.curvedPanel1.AddToFormRegion = false;
					this.curvedPanel1.Visible = false;
					this.solidClock1.Location = new Point(this.solidClock1.Left + this.curvedPanel1.Left, this.solidClock1.Top + this.curvedPanel1.Top);
					this.curvedPanel2.Location = new Point(this.curvedPanel2.Left - 32, this.curvedPanel2.Top + 32);
					this.Controls.Add(this.solidClock1);
					this.solidClock1.AddToFormRegion = true;
					
				}
				else 
				{
					this.solidClock1.Location = new Point(this.solidClock1.Left - this.curvedPanel1.Left, this.solidClock1.Top - this.curvedPanel1.Top);
					this.curvedPanel2.Location = new Point(this.curvedPanel2.Left + 32, this.curvedPanel2.Top - 32);
					this.curvedPanel1.Controls.Add(this.solidClock1);
					this.solidClock1.AddToFormRegion = false;
					this.curvedPanel1.Visible = true;
					this.curvedPanel1.AddToFormRegion = true;
				}
			}
		}

		private void menuItemTopMost_Click(object sender, System.EventArgs e)
		{
			MenuItem mi = sender as MenuItem;
			if (mi != null) 
			{
				mi.Checked = !mi.Checked;
				this.TopMost = mi.Checked;
			}
		}

		private void menuItemTransparancy_Click(object sender, EventArgs e) 
		{
			MenuItem mi = sender as MenuItem;
			if (mi != null) 
			{
				mi.Checked = true;
				this.menuItemTransparency.MenuItems[((int)(baseOpacity * 10) - 2)].Checked = false; 
				baseOpacity = (mi.Index + 2f) / 10;
				this.Opacity = baseOpacity;
			}
		}

		private void solidClock1_MouseEnter(object sender, System.EventArgs e)
		{
			increasing = true;
			this.timer1.Enabled = true;
		}

		private void solidClock1_MouseLeave(object sender, System.EventArgs e)
		{
			increasing = false;
			this.timer1.Enabled = true;
		}

		private void solidClock1_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			mouseDown = true;
			formMove = new Point(e.X,e.Y);
		}

		private void solidClock1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			if (mouseDown)
			{
				this.Location = new Point(this.Left - (formMove.X - e.X), this.Top - (formMove.Y - e.Y));
			}
		}

		private void solidClock1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
		{
			mouseDown = false;
		}

		private void solidClock1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
		{
			this.regionBuilder1.Invalidate();
		}

		private void timer1_Tick(object sender, System.EventArgs e)
		{
			if (!this.curvedPanel2.AddToFormRegion) 
			{
				this.curvedPanel2.AddToFormRegion = true;
				this.Refresh();
			}

			if (increasing) 
			{
				if (this.Opacity >= 1) 
				{
					this.timer1.Enabled = false;
				}
				else 
				{
					this.Opacity += .1;
				}
			}
			else 
			{
				if (this.Opacity <= baseOpacity) 
				{
					this.timer1.Enabled = false;
					this.curvedPanel2.AddToFormRegion = false;
					this.Refresh();
				}
				else 
				{
					this.Opacity -= .1;
				}
			}
		}
		#endregion
	}
}