View Single Post
  #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);
			}
		}
Reply With Quote