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);
}
}