Top-level window size

前端 未结 1 1948
误落风尘
误落风尘 2021-01-25 07:37
  1. Why is the minimum size of a top-level window constrained to 2x2 pixels?
  2. Is it possible to override this constraint so that I
相关标签:
1条回答
  • 2021-01-25 08:29

    The minimum size of a window is normally restricted to the smallest size that still allows the system menu and the caption buttons to be accessible by the user. Even for borderless windows, not exactly appropriate. You can make it smaller by explicitly setting the Size property after the window is created, normally the Load event handler is your first opportunity. Or OnHandleCreated().

    But yeah, Windows still restricts it to 2x2, the "why" is elusive. Surely what you really want to know is how to work around it. You do so by intercepting the WM_GETMINMAXINFO message and lowering the minimum track size. A sample form class that demonstrates the approach:

    Imports System.Runtime.InteropServices
    
    Public Class Form1
        Protected Overrides Sub WndProc(ByRef m As Message)
            MyBase.WndProc(m)
            If m.Msg = WM_GETMINMAXINFO Then
                Dim mmi = DirectCast(Marshal.PtrToStructure(m.LParam, GetType(MINMAXINFO)), MINMAXINFO)
                mmi.ptMinTrackSize = New Point(0, 0)
                Marshal.StructureToPtr(mmi, m.LParam, False)
            End If
        End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Me.Size = New Size(0, 0)
            MsgBox(Me.Size.ToString())
        End Sub
    
        Private Const WM_GETMINMAXINFO As Integer = &h24
        Private Structure MINMAXINFO
            Public ptReserved As Point
            Public ptMaxSize As Point
            Public ptMaxPosition As Point
            Public ptMinTrackSize As Point
            Public ptMaxTrackSize As Point
        End Structure
    End Class
    

    Tested on Windows 8.1. I can't promise that it will work on all Windows versions. It should.

    0 讨论(0)
提交回复
热议问题