Resizing issue

R

Rina

Hi,

I have a win form in VB.net that I want the user to be
only able to resize it's height (not the width!).
Is there an obvious way to do this?
I have tried to put some values in MinimumSize and
MaximumSize but it does not seem to affect the form.

I have tried to following, it works but it is not
nice visually (it shakes on resize):

Private Sub OnFormResize(ByVal sender As Object, ByVal e
As EventArgs) Handles MyBase.Resize

If Me.Size.Width > Me.MaximumSize.Width Then

Me.Size = New Size(Me.MaximumSize.Width,
Me.Size.Height)

End If

If Me.Size.Width < Me.MinimumSize.Width Then

Me.Size = New Size(Me.MinimumSize.Width,
Me.Size.Height)

End If

End Sub


any help would be great!

thanks,

Rina

..
 
J

Jerry Ham

Rina,

Here's a way to do what you want. In this sample code, I only make it so
that they can't go larger than 200 pixels for the width; you can make it so
that they can't change the width at all if you want:

Jerry

Imports System.Runtime.InteropServices

<StructLayout(LayoutKind.Sequential)> _

Private Class _RECT
Public left As Integer
Public top As Integer
Public right As Integer
Public bottom As Integer
End Class

Const WM_SIZING As Integer = &H214
Const MAX_WIDTH As Integer = 200

Dim bCreated As Boolean = False

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
bCreated = True
End Sub

Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If bCreated = True Then
Select Case m.Msg
Case WM_SIZING
Dim ptr As New IntPtr(m.LParam.ToInt32)
Dim r As New _RECT
Marshal.PtrToStructure(ptr, r)
If r.right - r.left > MAX_WIDTH Then
r.right = r.left + MAX_WIDTH
Marshal.StructureToPtr(r, ptr, True)
End If
End Select
End If

MyBase.WndProc(m)

End Sub



Jerry
 
J

Jerry Ham

I tend to be quite free with un-mananged code as there is way too much you
cannot do directly with .Net in managed code, however I am not actually
using any "un-managed code" in this sample. There are no DLL Import or
Declare Function calls to call directly into the API. All that is being done
is that the rectangle is being intercepted and the value changed. This is
native .Net really. It just needs to use the Marshal class to do it since it
is a pointer. However, a pointer, in and of itself doesn't generally make me
think it is un-managed code (I guess to a purist it might; I don't claim to
know the pefect definition and don't want to argue with anyone on it -
anyone who says "this IS un-managed - OK you are right.).

However, this works and I didn't see another way to do it without getting
the flickering effect that you were seeing before. There may well be a way
to do it completely in "managed" (no marshall class), but I could come up
with this solution off the top of my head...

Jerry
 
J

Jay B. Harlow [MVP - Outlook]

Rina,
You can do this with just setting the MinimumSize & MaximumSize properties
of the Form!

Windows forms already has handlers internal that watch the Resize,
WM_SIZING, and other Win32 events to get this to work.

The trick is to present the correct values for both MinimumSize &
MaximumSize.

For example if I want my Form to always be 600 units wide. I would set, in
the Form.Load event, the constructor after InitializeComponent or even in
the Form Designer:

Me.MinimumSize = New Size(600, 0)
Me.MaximumSize = New Size(600, Integer.MaxValue)

Notice the Width is fixed at 600 for both minimum & maximum.

While the height has a minimum of 0, and a maximum of the maximum of an
integer. The designer will make you enter 2147483647. However! as long as
you pick an arbitrarily large number that is significantly larger than any
screen you can ever run on, you will be fine. ;-)

The above was tested on VB.NET 2003.

I agree with your concern about unmanaged code. I find it better to find the
..NET solution first, instead of jumping to P/Invoke & unmanaged right away.

The framework is very rich, most Win32 features are available, if you know
how to find them & use them. Also considering the .NET ports to Linux & Unix
(www.go-mono.com & others) using P/Invoke too much will limit your app to
Windows!

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top