Possible Duplicate:
Vertically (only) resizable windows form in C#
I have a case where I need to allow the user to resize the form just horizontally. The maximum width of the form is x
. How can I do that?
Possible Duplicate:
Vertically (only) resizable windows form in C#
I have a case where I need to allow the user to resize the form just horizontally. The maximum width of the form is x
. How can I do that?
Set your MaximumSize and MinimumSize to the same Height, but variable widths.
To make it so the resize cursor doesn't appear on the top or bottom:
protected override void WndProc(ref Message m) {
base.WndProc(ref m);
switch (m.Msg) {
case 0x84: //WM_NCHITTEST
var result = (HitTest)m.Result.ToInt32();
if (result == HitTest.Top || result == HitTest.Bottom)
m.Result = new IntPtr((int)HitTest.Caption);
if (result == HitTest.TopLeft || result == HitTest.BottomLeft)
m.Result = new IntPtr((int)HitTest.Left);
if (result == HitTest.TopRight || result == HitTest.BottomRight)
m.Result = new IntPtr((int)HitTest.Right);
break;
}
}
enum HitTest {
Caption = 2,
Transparent = -1,
Nowhere = 0,
Client = 1,
Left = 10,
Right = 11,
Top = 12,
TopLeft = 13,
TopRight = 14,
Bottom = 15,
BottomLeft = 16,
BottomRight = 17,
Border = 18
}
Code copied and modified from: Vertically (only) resizable windows form in C#
Form has the MaximumSize and MinimumSize properties.
Set them the same as Size, except for the Width of MaximumSize of course.
It might also be a good idea to disable the MaximizeBox as it won't really make much sense (it would just place the window at the top-left corner of the current monitor).
You can create an static variable that will hold the mex value that it can has.
In the form resize event you can check if the value is more than your static value and change it to that value:
int maxValue = 100;
private void MainForm_ResizeEnd(object sender, EventArgs e)
{
if(this.Size.Width > maxValue)
this.Size.Width = maxValue;
}
Or you can set the Max value in the properties: MaximunSize
set the minimum and maximum height of the form to the initial height of the form. that should prevent the form to be resized vertically and allow the user to only resize the form horizontally.
If you want to set bounds for horizontal resizing, do the same for the minimum or maximum width of the form.
In the form SizeChanged event, you could do something like this
private void Form1_ResizeEnd(object sender, EventArgs e) {
//this does not prevent a resize to full screen
int i = this.Size.Height;
//force width to 300
this.Size = new Size(300, i);
return;
}