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:
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!