Overcome OS Imposed Windows Form Minimum Size Limit

前端 未结 6 706
耶瑟儿~
耶瑟儿~ 2020-11-29 08:57

In an application I am developing, I need to be able to make a windows form smaller than the minimum height limit imposed by the operating system (36 px in Vista). I have t

6条回答
  •  醉梦人生
    2020-11-29 09:25

    I wish I could give more than +1 to Zach for that, it's great and saved my bacon. For future readers, here's the VB translation of Zach's code:

    Imports System.Runtime.InteropServices
    Imports System.Windows.Forms
    Imports System.Drawing
    
    Public Class MyForm
    
        ' Ghastly hack to allow the form to be narrower than the widows-imposed limit (about 132 in WIndows 7)
        ' Thanks to http://stackoverflow.com/questions/992352/overcome-os-imposed-windows-form-minimum-size-limit
    
        Private Const WM_WINDOWPOSCHANGING As Integer = &H46
        Private Const WM_GETMINMAXINFO As Integer = &H24
        Protected Overrides Sub WndProc(ByRef m As Message)
            If m.Msg = WM_WINDOWPOSCHANGING Then
                Dim windowPos As WindowPos = CType(m.GetLParam(GetType(WindowPos)), WindowPos)
    
                ' Make changes to windowPos
    
                ' Then marshal the changes back to the message
                Marshal.StructureToPtr(windowPos, m.LParam, True)
            End If
    
            MyBase.WndProc(m)
    
            ' Make changes to WM_GETMINMAXINFO after it has been handled by the underlying
            ' WndProc, so we only need to repopulate the minimum size constraints
            If m.Msg = WM_GETMINMAXINFO Then
                Dim minMaxInfo As MINMAXINFO = DirectCast(m.GetLParam(GetType(MINMAXINFO)), MINMAXINFO)
                minMaxInfo.ptMinTrackSize.X = Me.MinimumSize.Width
                minMaxInfo.ptMinTrackSize.Y = Me.MinimumSize.Height
                Marshal.StructureToPtr(minMaxInfo, m.LParam, True)
            End If
        End Sub
    
        Private Structure WindowPos
            Public hwnd As IntPtr
            Public hwndInsertAfter As IntPtr
            Public x As Integer
            Public y As Integer
            Public width As Integer
            Public height As Integer
            Public flags As UInteger
        End Structure
         _
        Private Structure MINMAXINFO
            Dim ptReserved As Point
            Dim ptMaxSize As Point
            Dim ptMaxPosition As Point
            Dim ptMinTrackSize As Point
            Dim ptMaxTrackSize As Point
        End Structure
    
        .... rest of the form
    
    End Class
    

提交回复
热议问题