Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Hi, I am trying to override the message that is sent by the forms maximize button and I would like to create my own version of the form maximized state.

My application requires to be in a specific aspect ratio while resizing and I want to allow the user to maximize the form, but only to the given aspect ratio. This will work if I set form maximum size. But I also want it to be positioned in the centre of the screen, so that it is more professional. I have all the calculations for it's sizing I just need to be able to make that sizing happen :p

Here is some code I got from some other forums (converted from C# to VB.Net), but I am not sure how to use it - as I'm quite new to this override business :p:

VB
Imports System.Runtime.InteropServices

<DllImport("user32.dll")> _
Private Shared Function SetWindowLongPtr(hWnd As IntPtr, nIndex As Integer, dwNewLong As Integer) As Integer
End Function

<DllImport("user32.dll", SetLastError:=True)> _
Private Shared Function GetWindowLongPtr(hWnd As IntPtr, nIndex As Integer) As Integer
End Function

<DllImport("user32.dll")> _
Private Shared Function SetWindowPos(hWnd As IntPtr, hWndInsertAfter As IntPtr, X As Integer, Y As Integer, cx As Integer, cy As Integer, _
 uFlags As UInteger) As Boolean
End Function

Protected Overrides Sub WndProc(ByRef m As Message)

    Const WS_MAXIMIZE As Integer = &H1000000
    Const GWL_STYLE As Integer = -16
    Const WM_SYSCOMMAND As Integer = &H112
    Const SC_MAXIMIZE As Integer = &HF030
    Const SC_RESTORE As Integer = &HF120
    Static HWND_TOP As IntPtr = 0
    Const SWP_SHOWWINDOW As Integer = &H40
    Const SWP_NOZORDER As Integer = &H4
    Const SWP_NOMOVE As Integer = &H2
    Const SWP_DRAWFRAME As Integer = &H20

    If m.Msg = WM_SYSCOMMAND Then
        If CInt(m.WParam) = SC_MAXIMIZE Then
            MessageBox.Show("Maximized!!")
            Dim iStyle As Integer = GetWindowLong(Me.Handle, GWL_STYLE)
            SetWindowLong(Me.Handle, GWL_STYLE, iStyle Or WS_MAXIMIZE)
            SetWindowPos(Me.Handle, HWND_TOP, 0, 0, Me.Width, Me.Height, _
                         SWP_DRAWFRAME Or SWP_NOMOVE Or SWP_NOZORDER Or _
                         SWP_SHOWWINDOW)
            Return
        End If

        If CInt(m.WParam) = SC_RESTORE Then
            MessageBox.Show("Restored down!!")
            Dim iStyle As Integer = GetWindowLong(Me.Handle, GWL_STYLE)
            SetWindowLong(Me.Handle, GWL_STYLE, iStyle And Not WS_MAXIMIZE)
            SetWindowPos(Me.Handle, HWND_TOP, 0, 0, Me.Width, Me.Height, _
                         SWP_DRAWFRAME Or SWP_NOMOVE Or SWP_NOZORDER Or _
                         SWP_SHOWWINDOW)
            Return
        End If
    End If

    MyBase.WndProc(m)

End Sub


This code simply sets the forms window state to maximized without changing it's size or location. I simply want it to maximize to the screen's working area while maintaining a given aspect ratio and being centred.

Thanks in advance!
Posted
Updated 5-Aug-12 0:11am
v3

1 solution

I think what you have is far more complex than it needs to be. Forms have a protected property MaximizedBounds, which allows setting of the size and screen position of the maximised form. In use one would simply specify the bounding rectangle of the maximized form, probably in the constructor. Simples!

See http://msdn.microsoft.com/en-us/library/system.windows.forms.form.maximizedbounds.aspx[^]

Alan.
 
Share this answer
 
Comments
Rokas Gustys 5-Aug-12 8:13am    
Wow! Thanks! Didn't knew it could be that simple!!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900