Click here to Skip to main content
15,885,214 members
Articles / Programming Languages / C#

A Floating Popup Control

Rate me:
Please Sign up or sign in to vote.
3.64/5 (11 votes)
4 May 2006CPOL2 min read 115.8K   2.9K   68  
A floating popup control which can display any form.
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace Calculator
{
	/// <summary>
	/// Summary description for frmFloatingBase.
	/// </summary>
	public class frmFloatingBase : System.Windows.Forms.Form,IFloatingPopup
	{
		private System.ComponentModel.IContainer components;
		[DllImport("user32.dll")]
		public extern static int GetWindowRect(IntPtr hWnd,out Rect lpRect);

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

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <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()
		{
			this.components = new System.ComponentModel.Container();
			this.tmrForceActivate = new System.Windows.Forms.Timer(this.components);
			// 
			// tmrForceActivate
			// 
			this.tmrForceActivate.Tick += new System.EventHandler(this.tmrForceActivate_Tick);
			// 
			// frmFloatingBase
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(152, 144);
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
			this.Name = "frmFloatingBase";
			this.ShowInTaskbar = false;
			this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
			this.Text = "frmFloatingBase";
			this.Leave += new System.EventHandler(this.CheckToHide);
			this.Deactivate += new System.EventHandler(this.CheckToHide);

		}
		#endregion

		private void CheckToHide(object sender, EventArgs e)
		{
			this.Hide();
		}

		private System.Windows.Forms.Timer tmrForceActivate;
		System.Windows.Forms.UserControl userControl;
		protected virtual void OnPopupHiding(CancelEventArgs e)
		{
			if(PopupHiding!=null)
				PopupHiding(this,e);
			if(e.Cancel==false)
			{
				base.Hide();
				OnPopupHidden(new EventArgs());
				if(this.Owner!=null)
					this.Owner.BringToFront();
			}
			else
			{
				this.tmrForceActivate.Enabled=true;
			}
			
		}
		protected virtual void OnPopupShowing(CancelEventArgs e)
		{
			if(PopupShowing!=null)
			{
				PopupShowing(this,e);
			}
			if(e.Cancel==false)
			{
				SetAutoLocation();
				base.Show();
				OnPopupShown(new EventArgs());
			}
		}
		protected virtual void OnPopupHidden(EventArgs e)
		{
			if(PopupHidden!=null)
				PopupHidden(this,e);
		}
		protected virtual void OnPopupShown(EventArgs e)
		{
			if(PopupShown!=null)
				PopupShown(this,e);
		}


		private void tmrForceActivate_Tick(object sender, System.EventArgs e)
		{
			this.tmrForceActivate.Enabled=false;
			this.Activate();
		}
		

		#region IFloatingPopup Members

		public event System.ComponentModel.CancelEventHandler PopupHiding;

		public event System.ComponentModel.CancelEventHandler PopupShowing;

		public event System.EventHandler PopupHidden;

		public event System.EventHandler PopupShown;

		public new void Show()
		{
			// TODO:  Add frmFloatingBase.Show implementation
			OnPopupShowing(new CancelEventArgs());
		}

		public new void Hide()
		{
			// TODO:  Add frmFloatingBase.Hide implementation
			OnPopupHiding(new CancelEventArgs());
		}

		public void ForceShow()
		{
			// TODO:  Add frmFloatingBase.ForceShow implementation
			this.tmrForceActivate.Enabled=true;
		}

		public UserControl UserControl
		{
			get
			{
				// TODO:  Add frmFloatingBase.UserControl getter implementation
				return userControl;
			}
			set
			{
				// TODO:  Add frmFloatingBase.UserControl setter implementation
				userControl=value;
			}
		}

		public void SetAutoLocation()
		{
			// TODO:  Add frmFloatingBase.SetAutoLocation implementation
			Rect rect;
			GetWindowRect(UserControl.Handle,out rect);
			Point tergatePoint;
			tergatePoint = new Point(rect.left ,rect.top + UserControl.Height );
			if(rect.left + UserControl.Width - this.Width < 0)
			{
				tergatePoint.X=0;
			}
			else
			{
				tergatePoint.X=rect.left - this.Width + UserControl.Width;
			}
			if(tergatePoint.X+this.Width>System.Windows.Forms.SystemInformation.WorkingArea.Right)
			{
				tergatePoint.X=System.Windows.Forms.SystemInformation.WorkingArea.Right-this.Width;
			}
			else if(tergatePoint.X<0)
				tergatePoint.X=0;
			if(tergatePoint.Y+this.Height>System.Windows.Forms.SystemInformation.WorkingArea.Bottom)
			{
				tergatePoint.Y=rect.top - this.Height;
			}
			if (tergatePoint.Y<0)
			{
				tergatePoint.Y=0;
			}
			if (tergatePoint.X<0)
			{
				tergatePoint.X=0;
			}
			this.Location=tergatePoint;

		}
		public Form GetPopupForm()
		{
			return this;
		}
		#endregion

	}
	public struct Rect
	{
		internal int left, top, right, bottom;
	}
	
}

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) KAZ Software Limited
Bangladesh Bangladesh
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions