- Why is the minimum size of a top-level window constrained to 2x2 pixels?
- Is it possible to override this constraint so that I can set the width/height to < 2.
If you run the sample application and change the size of the window to let's say 1,1
you'll see that the size is constrained to 2,2
.
Sample application
Public Class Form1
Public Sub New()
Me.InitializeComponent()
Me.wnd = New Window With {.BackColor = Color.Red, .Bounds = New Rectangle(100, 100, 200, 200)}
Me.grid = New PropertyGrid With {.Dock = DockStyle.Fill, .SelectedObject = Me.wnd, .TabIndex = 1}
Me.btn = New Button With {.Text = "Set {width=1, height=1} using SetWindowPos.", .Dock = DockStyle.Top, .Height = 30, .TabIndex = 0}
Me.Controls.AddRange({Me.grid, Me.btn})
End Sub
Private Sub HandleDisposed(sender As Object, e As EventArgs) Handles Me.Disposed
If (Not Me.grid Is Nothing) Then Me.grid.Dispose()
If (Not Me.wnd Is Nothing) Then Me.wnd.Dispose()
If (Not Me.btn Is Nothing) Then Me.btn.Dispose()
End Sub
Private Sub HandleButtonClick(sender As Object, e As EventArgs) Handles btn.Click
Window.SetWindowPos(New HandleRef(Me.wnd, Me.wnd.Handle), Nothing, 0, 0, 1, 1, (Window.SWP_DRAWFRAME Or Window.SWP_NOMOVE Or Window.SWP_NOZORDER Or Window.SWP_NOOWNERZORDER))
Me.grid.Refresh()
End Sub
Private WithEvents wnd As Window
Private WithEvents grid As PropertyGrid
Private WithEvents btn As Button
Public Class Window
Inherits Control
Public Sub New()
Me.SetTopLevel(True)
End Sub
Protected Overrides Sub OnHandleCreated(e As System.EventArgs)
Try
If (IntPtr.Size = 4) Then
Dim style As Int32 = (GetWindowLong32(New HandleRef(Me, Me.Handle), GWL_STYLE) And Not (WS_CAPTION Or WS_THICKFRAME Or WS_MINIMIZE Or WS_MAXIMIZE Or WS_SYSMENU))
Dim exstyle As Int32 = (GetWindowLong32(New HandleRef(Me, Me.Handle), GWL_EXSTYLE) And Not (WS_EX_DLGMODALFRAME Or WS_EX_CLIENTEDGE Or WS_EX_STATICEDGE))
SetWindowLong32(New HandleRef(Me, Me.Handle), GWL_STYLE, New HandleRef(Nothing, New IntPtr(style)))
SetWindowLong32(New HandleRef(Me, Me.Handle), GWL_EXSTYLE, New HandleRef(Nothing, New IntPtr(exstyle)))
Else
Dim style As Int64 = (GetWindowLong64(New HandleRef(Me, Me.Handle), GWL_STYLE) And Not CLng(WS_CAPTION Or WS_THICKFRAME Or WS_MINIMIZE Or WS_MAXIMIZE Or WS_SYSMENU))
Dim exstyle As Int64 = (GetWindowLong64(New HandleRef(Me, Me.Handle), GWL_EXSTYLE) And Not CLng(WS_EX_DLGMODALFRAME Or WS_EX_CLIENTEDGE Or WS_EX_STATICEDGE))
SetWindowLong64(New HandleRef(Me, Me.Handle), GWL_STYLE, New HandleRef(Nothing, New IntPtr(style)))
SetWindowLong64(New HandleRef(Me, Me.Handle), GWL_EXSTYLE, New HandleRef(Nothing, New IntPtr(exstyle)))
End If
SetWindowPos(New HandleRef(Me, Me.Handle), New HandleRef(Nothing, New IntPtr(HWND_TOPMOST)), 0, 0, 0, 0, (SWP_DRAWFRAME Or SWP_NOMOVE Or SWP_NOSIZE))
Catch ex As Exception
Throw ex
Finally
MyBase.OnHandleCreated(e)
End Try
End Sub
<DllImport("user32.dll", EntryPoint:="GetWindowLong", CharSet:=CharSet.Auto)> _
Friend Shared Function GetWindowLong32(ByVal hWnd As HandleRef, ByVal nIndex As Integer) As Int32
End Function
<DllImport("user32.dll", EntryPoint:="GetWindowLongPtr", CharSet:=CharSet.Auto)> _
Friend Shared Function GetWindowLong64(ByVal hWnd As HandleRef, ByVal nIndex As Integer) As Int64
End Function
<DllImport("user32.dll", EntryPoint:="SetWindowLong", CharSet:=CharSet.Auto)> _
Friend Shared Function SetWindowLong32(ByVal hWnd As HandleRef, ByVal nIndex As Integer, ByVal dwNewLong As HandleRef) As Int32
End Function
<DllImport("user32.dll", EntryPoint:="SetWindowLongPtr", CharSet:=CharSet.Auto)> _
Friend Shared Function SetWindowLong64(ByVal hWnd As HandleRef, ByVal nIndex As Integer, ByVal dwNewLong As HandleRef) As Int64
End Function
<DllImport("user32.dll", CharSet:=CharSet.Auto, ExactSpelling:=True)> _
Friend Shared Function SetWindowPos(ByVal hWnd As HandleRef, ByVal hWndInsertAfter As HandleRef, ByVal x As Integer, ByVal y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal flags As Integer) As Boolean
End Function
Friend Const GWL_EXSTYLE As Integer = -20
Friend Const GWL_STYLE As Integer = -16
Friend Const HWND_TOPMOST As Integer = -1
Friend Const SWP_DRAWFRAME As Integer = &H20
Friend Const SWP_NOMOVE As Integer = 2
Friend Const SWP_NOSIZE As Integer = 1
Friend Const SWP_NOZORDER As Integer = 4
Friend Const SWP_NOOWNERZORDER As Integer = &H200
Friend Const WS_CAPTION As Integer = &HC00000
Friend Const WS_THICKFRAME As Integer = &H40000
Friend Const WS_MAXIMIZE As Integer = &H1000000
Friend Const WS_MINIMIZE As Integer = &H20000000
Friend Const WS_SYSMENU As Integer = &H80000
Friend Const WS_EX_DLGMODALFRAME As Integer = 1
Friend Const WS_EX_CLIENTEDGE As Integer = &H200
Friend Const WS_EX_STATICEDGE As Integer = &H20000
End Class
End Class