View Single Post
  #4 (permalink)  
Old 09-25-2007, 08:51 AM
Mramesh Mramesh is offline
D-Web Sr.Programmer
 
Join Date: Sep 2007
Location: Chennai
Posts: 106
Mramesh is on a distinguished road
Send a message via MSN to Mramesh
Default Re: Windows Forms Mouse Handling

This is an example on how to implement a rubber band effect in Windows forms.


Code:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Text;
using System.Windows.Forms;
using System.Data;

namespace ResizableForm
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		
		private System.Windows.Forms.PictureBox pictureBox1;
		private System.ComponentModel.Container components = null;
		static int frmLastWidth=0;
		static int frmLastHeight=0;
		static int frmWidth;
		static int frmHeight;
		static bool frmIsResizing=false;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Button button1;
		System.Drawing.Rectangle frmRectangle = new System.Drawing.Rectangle();

		public Form1()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();
			
			// Add picture box handler junk
			pictureBox1.MouseMove += new MouseEventHandler(pictureBox1_MouseMove);
			pictureBox1.MouseDown += new MouseEventHandler(pictureBox1_MouseDown);
			pictureBox1.MouseUp += new MouseEventHandler(pictureBox1_MouseUp);
		}
		private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
		{
			frmRectangle.Location = new System.Drawing.Point(this.Left,this.Top);
			frmRectangle.Size = new System.Drawing.Size(frmWidth,frmHeight);
			ControlPaint.DrawReversibleFrame(frmRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Thick);
		}
		private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
		{
			frmIsResizing = false;
			frmRectangle.Location = new System.Drawing.Point(this.Left,this.Top);
			frmRectangle.Size = new System.Drawing.Size(frmWidth,frmHeight);
			ControlPaint.DrawReversibleFrame(frmRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Thick);
			this.Width = frmWidth;
			this.Height = frmHeight;
		}
		private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
		{
			if (e.Button==MouseButtons.Left)
			{
				
				//this.ResizeRedraw = false;
				
				int sizeageX = (MousePosition.X-this.Location.X);
				int sizeageY = (MousePosition.Y-this.Location.Y);

				// Use this to restrict Width
				if (sizeageX < 120)
					sizeageX = 120;

				// Use this to restrict Height
				if (sizeageY < 81)
					sizeageY = 81;

				frmWidth = sizeageX;
				frmHeight = sizeageY;

				if (frmLastWidth == 0)
					frmLastWidth = frmWidth;
				if (frmLastHeight==0)
					frmLastHeight = frmHeight;
				if (frmIsResizing)
				{
					frmRectangle.Location = new System.Drawing.Point(this.Left,this.Top);
					frmRectangle.Size = new System.Drawing.Size(frmLastWidth,frmLastHeight);
				}

				frmIsResizing = true;

				ControlPaint.DrawReversibleFrame(frmRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Thick);
				frmLastWidth = frmWidth;
				frmLastHeight = frmHeight;

				frmRectangle.Location = new System.Drawing.Point(this.Left,this.Top);
				frmRectangle.Size = new System.Drawing.Size(frmWidth,frmHeight);

				ControlPaint.DrawReversibleFrame(frmRectangle, Color.Empty, System.Windows.Forms.FrameStyle.Thick);
			}
		}


		#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.label1 = new System.Windows.Forms.Label();
			this.button1 = new System.Windows.Forms.Button();
			this.pictureBox1 = new System.Windows.Forms.PictureBox();
			this.SuspendLayout();
			// 
			// label1
			// 
			this.label1.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right);
			this.label1.Location = new System.Drawing.Point(32, 184);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(80, 16);
			this.label1.TabIndex = 1;
			this.label1.Text = "Grab this ->>";
			// 
			// button1
			// 
			this.button1.Anchor = (System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right);
			this.button1.Location = new System.Drawing.Point(96, 8);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(48, 24);
			this.button1.TabIndex = 2;
			this.button1.Text = "Close";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// pictureBox1
			// 
			this.pictureBox1.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right);
			this.pictureBox1.BackColor = System.Drawing.Color.Blue;
			this.pictureBox1.Cursor = System.Windows.Forms.Cursors.SizeNWSE;
			this.pictureBox1.Location = new System.Drawing.Point(120, 176);
			this.pictureBox1.Name = "pictureBox1";
			this.pictureBox1.Size = new System.Drawing.Size(32, 32);
			this.pictureBox1.TabIndex = 0;
			this.pictureBox1.TabStop = false;
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.ClientSize = new System.Drawing.Size(152, 208);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.button1,
																		  this.label1,
																		  this.pictureBox1});
			this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
			this.Name = "Form1";
			this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
			this.Text = "Form1";
			this.ResumeLayout(false);

		}
		#endregion
		/// <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 Form1());
		}

		private void button1_Click(object sender, System.EventArgs e)
		{
			Application.Exit();
		}
	}
}
Reply With Quote