3

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?

Community
  • 1
  • 1
Victor
  • 13,914
  • 19
  • 78
  • 147

5 Answers5

9

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#

Community
  • 1
  • 1
Adam Plocher
  • 13,994
  • 6
  • 46
  • 79
  • This is not what I want. – Victor Oct 05 '12 at 17:00
  • 2
    @Victor Why not? That accomplishes this exactly.. How is it different than what you want? – Reed Copsey Oct 05 '12 at 17:03
  • I want to allow the user to resize the form JUST HORIZONTALLY – Victor Oct 05 '12 at 17:05
  • 1
    Right, if the Maximum and Minimum Height is set to 200, they can't go above or below 200... But the Minimum Width is 0 and Maximum Width is 10000, they can size it horizontally. – Adam Plocher Oct 05 '12 at 17:08
  • It is true, but I do not want the RESIZE cursor to be displayed, on the TOP, and BOTTOM of my form, this works perfectly, but it is made for VERTICALLY resizing (http://stackoverflow.com/questions/2140882/vertically-only-resizable-windows-form-in-c-sharp) – Victor Oct 05 '12 at 17:11
  • 1
    @Victor, Adam's answer did not refer to the duplicate. If the minimum and maximum height are set to the same value, then the form will not be resizable vertically (as you want). If the minimum and maximum width are set to different values, then the form will be resizable horizontally (again, as you want). Therefore, if you do this, your form will only be resizable horizontally, as you want, not vertically. – rid Oct 05 '12 at 17:14
  • 1
    I just copied the code from that link and modified it to hide the cursors on the top and bottom. Try it out (I didn't test it). Updated my answer with the code. – Adam Plocher Oct 05 '12 at 17:15
  • Thank you very much! Could you provide a link or something to explain me the values in `enum HitTest{ }`??? Thanks in advance! – Victor Oct 05 '12 at 17:18
  • Victor: http://msdn.microsoft.com/en-us/library/windows/desktop/ms645618(v=vs.85).aspx I believe this is what you're looking for. – Adam Plocher Oct 05 '12 at 18:42
2

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).

Marco Mp
  • 422
  • 2
  • 6
1

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

Kyky
  • 63
  • 1
  • 7
0

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.

Gabi Barrientos
  • 1,746
  • 3
  • 23
  • 37
0

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