IT Community - Software Programming, Web Development and Technical Support

Windows Forms Mouse Handling

This is a discussion on Windows Forms Mouse Handling within the C# Programming forums, part of the Software Development category; Hi All, Could u tell me Rubber Band Effect in a Form?...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > C# Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 09-20-2007, 07:45 AM
Sundaram Sundaram is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Location: chennai
Posts: 117
Sundaram is on a distinguished road
Send a message via MSN to Sundaram Send a message via Yahoo to Sundaram
Default Windows Forms Mouse Handling

Hi All,
Could u tell me Rubber Band Effect in a Form?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 09-25-2007, 08:36 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

Here is code how to implement a rubber band effect in Windows forms.

Code:
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);
			}
		}
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 09-25-2007, 08:46 AM
Sundaram Sundaram is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Location: chennai
Posts: 117
Sundaram is on a distinguished road
Send a message via MSN to Sundaram Send a message via Yahoo to Sundaram
Default Re: Windows Forms Mouse Handling

Hi Ramesh,
Your code is very useful,
Can u give full code this one...

thanx advance..
sundaram
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #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();
		}
	}
}
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Windows Forms Application Interview Tips and Tricks santhakumar Interview Questions & Answers and Tips 497 06-03-2008 08:50 PM
Windows Forms Patterns Mramesh C# Programming 0 09-25-2007 09:13 AM
Windows Forms from MFC Sundaram C# Programming 5 09-25-2007 09:09 AM
Pratical issues in adding controls to Windows Forms in C#.Net 2005 Sathish Kumar C# Programming 21 09-22-2007 05:11 AM
Practical issues when we develop code in Windows Forms in C#.Net 2005 Sathish Kumar C# Programming 10 09-20-2007 07:17 AM


All times are GMT -7. The time now is 02:17 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0