How to resize a form which has FormBorderStyle.None

  • Thread starter Thread starter Heinz
  • Start date Start date
H

Heinz

Hi,
I have a form which has FormBorderStyle.None .There is only a 2 labels(1
main label , the other a small one at left down site of the form to resize)
on this Form.
I want to resize the form but dont know which events I need.

Have first try to add a MouseDown event on label2 to get the coordinate of
the mouse and store it in variables mousx , mousy .
Than I have add a MouseMove Event on label2
and calculate the difference of the current mouseposition and the ones
stored in mousx, mousy in it.
Than resize the form with the difference.
But its somehow false. When click left button on label2 and move the mouse
and stop it. The form still resize. it only stopes when I get the
mouseposition to the coordinates stored in mousx and mousy.

Have I even take the right way to resize a form with FormBorderStyle.None or
is there any simple way?
thx
 
Yes, you need a resize container of some sort. You need to trap MouseDown,
MouseMove, and MouseUp events. You'll also need to set Capture to the resize
container (in your case the label) so that you don't lose events. Losing events
is the primary reason that most resizers fail. We just added a custom resize
control to the Terrarium application, but I don't have the source code handy for
the good version, only the slower/more flaky version. But here goes (note
you'll need to extrapolate the variables that I'm using in this case).

private void backgroundPanel_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs me)
{
this.backgroundPanel.Capture = true;
this.bDragging = true;
this.mouseDown = new Point( me.X,me.Y );
}

private void backgroundPanel_MouseUp(object sender,
System.Windows.Forms.MouseEventArgs e)
{
this.backgroundPanel.Capture = false;
bDragging = false;
}

private void backgroundPanel_MouseMove(object sender,
System.Windows.Forms.MouseEventArgs me)
{
Point p = new Point(me.X, me.Y);
Rectangle r = this.ClientRectangle;

if (bDragging)
{
if ( this.parentForm != null )
{
this.Cursor = Cursors.Hand;
int width = this.parentForm.Size.Width;
int height = this.parentForm.Size.Height;
width += (me.X - mouseDown.X);
height += (me.Y - mouseDown.Y);
this.parentForm.Size = new Size( width, height );
this.parentForm.Invalidate();
}
}
}
 
take the current mouse position from the mouse move event.
in mouse down event just set a boolean flag. in the mouse move check if this
boolean flag is set. If it is set then only resize the window else return.
 
Thank you very much for your help and sharing the code.
Think this will bring me one step forwards.
I had nearly the same code but I dont use Capture. Dont even know that
"Capture" exits.
Will try it thx again.
 
Back
Top