Windows Forms: FormBorderStyle None - Resize

  • C#
  • .NET (FX) 4.5–4.8

Es gibt 3 Antworten in diesem Thema. Der letzte Beitrag () ist von FormFollowsFunction.

    Windows Forms: FormBorderStyle None - Resize

    Hallo Zusammen,

    ich habe mal eine Frage zu der FormBorderStyle.None Eigenschaft. Wie bekomme ich es hin, dass ich meine Form dennoch mit der Maus resizen kann an den Kanten?

    Oder gibt es eine Alternative den Windows.Forms Border umzufärben?

    LG Marvin
    Du könntest auch eine PictureBox in den Ecken verwenden, hab ich selbst immer in der rechten unnteren Ecke gemacht, aber der Code is mit sicherheit entsprechend anpassbar:

    VB.NET-Quellcode

    1. Dim GripDrag As Boolean
    2. Dim InitialSizeX As Integer
    3. Dim InitialSizeY As Integer
    4. 'Verwendung einer PictureBox als SizeGrip ------------------------------------------------------------------------------------
    5. Private Sub picResize_MouseDown(sender As Object, e As MouseEventArgs) Handles picResize.MouseDown
    6. If e.Button = Windows.Forms.MouseButtons.Left Then 'If the control is being left-clicked
    7. GripDrag = True 'Confirms the grip is ready to be dragged
    8. InitialSizeX = Width 'Sets the initial width
    9. InitialSizeY = Height 'Sets the initial height
    10. End If
    11. End Sub
    12. Private Sub picResize_MouseMove(sender As Object, e As MouseEventArgs) Handles picResize.MouseMove
    13. If GripDrag = True Then
    14. Width = InitialSizeX + (Cursor.Position.X - (Width + Location.X)) 'Increases the width of the form by the amount the grip has been dragged towards the right
    15. Height = InitialSizeY + (Cursor.Position.Y - (Height + Location.Y)) 'Increases the height of the form by the amount the grip has been dragged downward
    16. InitialSizeX = Me.Width 'Resets the value to the form's current width
    17. InitialSizeY = Me.Height 'Resets the value to the form's current height
    18. Refresh()
    19. End If
    20. End Sub
    21. Private Sub picResize_MouseUp(sender As Object, e As MouseEventArgs) Handles picResize.MouseUp
    22. GripDrag = False 'Confirms the grip is no longer being dragged
    23. End Sub
    24. 'Verwendung einer PictureBox als SizeGrip ----------------------------------------------------------------------------------
    If Energy = Low Then
    Drink(aHugeCoffee)
    Else
    Drink(aHugeCoffeeToo)
    End If
    Dieses Forum macht echt keinen Spass, täglich neue ärgerliche Threads !

    Ich habe nur wenige Sekunden gebraucht, um was brauchbares zu finden !
    Und irgentwie sehe ich es nicht so richtig ein, die Ergebnisse meiner Mühen, freihaus zu liefern.

    Ein mal noch :)

    VB.NET-Quellcode

    1. Imports System.Runtime.InteropServices
    2. Public Class FormResizer
    3. Dim TargetForm As Form
    4. Private _BorderWidth As Integer
    5. Private _ResizeDirect As resizeDirectection = resizeDirectection.None
    6. ''' <summary>
    7. ''' Make borderless Form resizeable.
    8. ''' </summary>
    9. ''' <param name="value">As Form</param>
    10. ''' <param name="borderWidth">(Optional) BorderWidth as Integer (Default = 4)</param>
    11. Sub New(ByVal value As Form, Optional borderWidth As Integer = 4)
    12. TargetForm = value
    13. _BorderWidth = borderWidth
    14. AddHandler TargetForm.MouseDown, AddressOf Form_MouseDown
    15. AddHandler TargetForm.MouseMove, AddressOf Form_MouseMove
    16. End Sub
    17. Public Enum resizeDirectection
    18. None = 0
    19. Left = 1
    20. TopLeft = 2
    21. Top = 3
    22. TopRight = 4
    23. Right = 5
    24. BottomRight = 6
    25. Bottom = 7
    26. BottomLeft = 8
    27. End Enum
    28. Public Property resizeDirect() As resizeDirectection
    29. Get
    30. Return _ResizeDirect
    31. End Get
    32. Set(ByVal value As resizeDirectection)
    33. _ResizeDirect = value
    34. Select Case value
    35. Case resizeDirectection.Left
    36. TargetForm.Cursor = Cursors.SizeWE
    37. Case resizeDirectection.Right
    38. TargetForm.Cursor = Cursors.SizeWE
    39. Case resizeDirectection.Top
    40. TargetForm.Cursor = Cursors.SizeNS
    41. Case resizeDirectection.Bottom
    42. TargetForm.Cursor = Cursors.SizeNS
    43. Case resizeDirectection.BottomLeft
    44. TargetForm.Cursor = Cursors.SizeNESW
    45. Case resizeDirectection.TopRight
    46. TargetForm.Cursor = Cursors.SizeNESW
    47. Case resizeDirectection.BottomRight
    48. TargetForm.Cursor = Cursors.SizeNWSE
    49. Case resizeDirectection.TopLeft
    50. TargetForm.Cursor = Cursors.SizeNWSE
    51. Case Else
    52. TargetForm.Cursor = Cursors.Default
    53. End Select
    54. End Set
    55. End Property
    56. <DllImport("user32.dll")>
    57. Public Shared Function ReleaseCapture() As Boolean
    58. End Function
    59. <DllImport("user32.dll")>
    60. Public Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    61. End Function
    62. Private Const WM_NCLBUTTONDOWN As Integer = &HA1
    63. Private Const HTBORDER As Integer = 18
    64. Private Const HTBOTTOM As Integer = 15
    65. Private Const HTBOTTOMLEFT As Integer = 16
    66. Private Const HTBOTTOMRIGHT As Integer = 17
    67. Private Const HTCAPTION As Integer = 2
    68. Private Const HTLEFT As Integer = 10
    69. Private Const HTRIGHT As Integer = 11
    70. Private Const HTTOP As Integer = 12
    71. Private Const HTTOPLEFT As Integer = 13
    72. Private Const HTTOPRIGHT As Integer = 14
    73. Private Sub ResizeForm(ByVal direction As resizeDirectection)
    74. Dim dir As Integer = -1
    75. Select Case direction
    76. Case resizeDirectection.Left
    77. dir = HTLEFT
    78. Case resizeDirectection.TopLeft
    79. dir = HTTOPLEFT
    80. Case resizeDirectection.Top
    81. dir = HTTOP
    82. Case resizeDirectection.TopRight
    83. dir = HTTOPRIGHT
    84. Case resizeDirectection.Right
    85. dir = HTRIGHT
    86. Case resizeDirectection.BottomRight
    87. dir = HTBOTTOMRIGHT
    88. Case resizeDirectection.Bottom
    89. dir = HTBOTTOM
    90. Case resizeDirectection.BottomLeft
    91. dir = HTBOTTOMLEFT
    92. End Select
    93. If dir <> -1 Then
    94. ReleaseCapture()
    95. SendMessage(TargetForm.Handle, WM_NCLBUTTONDOWN, dir, 0)
    96. End If
    97. End Sub
    98. Private Sub Form_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
    99. If e.Button = Windows.Forms.MouseButtons.Left And TargetForm.WindowState <> FormWindowState.Maximized Then
    100. ResizeForm(resizeDirect)
    101. End If
    102. End Sub
    103. Private Sub Form_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
    104. If e.Location.X < _BorderWidth And e.Location.Y < _BorderWidth Then
    105. resizeDirect = resizeDirectection.TopLeft
    106. ElseIf e.Location.X < _BorderWidth And e.Location.Y > TargetForm.Height - _BorderWidth Then
    107. resizeDirect = resizeDirectection.BottomLeft
    108. ElseIf e.Location.X > TargetForm.Width - _BorderWidth And e.Location.Y > TargetForm.Height - _BorderWidth Then
    109. resizeDirect = resizeDirectection.BottomRight
    110. ElseIf e.Location.X > TargetForm.Width - _BorderWidth And e.Location.Y < _BorderWidth Then
    111. resizeDirect = resizeDirectection.TopRight
    112. ElseIf e.Location.X < _BorderWidth Then
    113. resizeDirect = resizeDirectection.Left
    114. ElseIf e.Location.X > TargetForm.Width - _BorderWidth Then
    115. resizeDirect = resizeDirectection.Right
    116. ElseIf e.Location.Y < _BorderWidth Then
    117. resizeDirect = resizeDirectection.Top
    118. ElseIf e.Location.Y > TargetForm.Height - _BorderWidth Then
    119. resizeDirect = resizeDirectection.Bottom
    120. Else resizeDirect = resizeDirectection.None
    121. End If
    122. End Sub
    123. End Class

    VB.NET-Quellcode

    1. Public Class Form1
    2. Dim _FormResizer As New FormResizer(Me, 6)
    3. End Class

    Dieser Beitrag wurde bereits 2 mal editiert, zuletzt von „FormFollowsFunction“ ()