Form Headerfarbe

  • VB.NET

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

    @Rikudo
    1 Sekunde gegooglet...
    social.msdn.microsoft.com/Foru…5-4e2a-995b-bd52a1ff690f/

    Siehe,
    Spoiler anzeigen

    VB.NET-Quellcode

    1. 'A form with custom border and title bar.
    2. 'Some functions, such as resize the window via mouse, are not implemented yet.
    3. Public Class Form1
    4. 'The color and the width of the border.
    5. Private borderColor As Color = Color.GreenYellow
    6. Private borderWidth As Integer = 3
    7. 'The color and region of the header.
    8. Private headerColor As Color = Color.GreenYellow
    9. Private headerRect As Rectangle
    10. 'The region of the client.
    11. Private clientRect As Rectangle
    12. 'The region of the title text.
    13. Private titleRect As Rectangle
    14. 'The region of the minimum button.
    15. Private miniBoxRect As Rectangle
    16. 'The region of the maximum button.
    17. Private maxBoxRect As Rectangle
    18. 'The region of the close button.
    19. Private closeBoxRect As Rectangle
    20. 'The states of the three header buttons.
    21. Private miniState As ButtonState
    22. Private maxState As ButtonState
    23. Private closeState As ButtonState
    24. 'Store the mouse down point to handle moving the form.
    25. Private x As Integer = 0
    26. Private y As Integer = 0
    27. 'The height of the header.
    28. Const HEADER_HEIGHT As Integer = 25
    29. 'The size of the header buttons.
    30. ReadOnly BUTTON_BOX_SIZE As Size = New Size(15, 15)
    31. Private Sub CustomBorderColorForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    32. 'Hide the border and the title bar.
    33. Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
    34. End Sub
    35. Private Sub CustomBorderColorForm_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
    36. 'Draw the header.
    37. Using b As Brush = New SolidBrush(borderColor)
    38. e.Graphics.FillRectangle(b, headerRect)
    39. End Using
    40. 'Draw the title text
    41. Using b As Brush = New SolidBrush(Me.ForeColor)
    42. e.Graphics.DrawString(Me.Text, Me.Font, b, titleRect)
    43. End Using
    44. 'Draw the header buttons.
    45. If Me.MinimizeBox Then
    46. ControlPaint.DrawCaptionButton(e.Graphics, miniBoxRect, CaptionButton.Minimize, miniState)
    47. End If
    48. If Me.MinimizeBox Then
    49. ControlPaint.DrawCaptionButton(e.Graphics, maxBoxRect, CaptionButton.Maximize, maxState)
    50. End If
    51. If Me.MinimizeBox Then
    52. ControlPaint.DrawCaptionButton(e.Graphics, closeBoxRect, CaptionButton.Close, closeState)
    53. End If
    54. 'Draw the border.
    55. ControlPaint.DrawBorder(e.Graphics, clientRect, borderColor, _
    56. borderWidth, ButtonBorderStyle.Solid, borderColor, borderWidth, ButtonBorderStyle.Solid, borderColor, borderWidth, ButtonBorderStyle.Solid, borderColor, borderWidth, ButtonBorderStyle.Solid)
    57. End Sub
    58. 'Handle resize to adjust the region ot border, header and so on.
    59. Private Sub CustomBorderColorForm_Resize(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Resize
    60. headerRect = New Rectangle(Me.ClientRectangle.Location, New Size(Me.ClientRectangle.Width, HEADER_HEIGHT))
    61. clientRect = New Rectangle(New Point(Me.ClientRectangle.Location.X, Me.ClientRectangle.Y + HEADER_HEIGHT), _
    62. CType(New Point(Me.ClientRectangle.Width, Me.ClientRectangle.Height - HEADER_HEIGHT), Drawing.Size))
    63. Dim yOffset = (headerRect.Height + borderWidth - BUTTON_BOX_SIZE.Height) / 2
    64. titleRect = New Rectangle(CInt(yOffset), CInt(yOffset), _
    65. CInt(Me.ClientRectangle.Width - 3 * (BUTTON_BOX_SIZE.Width + 1) - yOffset), _
    66. BUTTON_BOX_SIZE.Height)
    67. miniBoxRect = New Rectangle(Me.ClientRectangle.Width - 3 * (BUTTON_BOX_SIZE.Width + 1), _
    68. CInt(yOffset), BUTTON_BOX_SIZE.Width, BUTTON_BOX_SIZE.Height)
    69. maxBoxRect = New Rectangle(Me.ClientRectangle.Width - 2 * (BUTTON_BOX_SIZE.Width + 1), _
    70. CInt(yOffset), BUTTON_BOX_SIZE.Width, BUTTON_BOX_SIZE.Height)
    71. closeBoxRect = New Rectangle(Me.ClientRectangle.Width - 1 * (BUTTON_BOX_SIZE.Width + 1), _
    72. CInt(yOffset), BUTTON_BOX_SIZE.Width, BUTTON_BOX_SIZE.Height)
    73. Me.Invalidate()
    74. End Sub
    75. Private Sub CustomBorderColorForm_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDown
    76. 'Start to move the form.
    77. If (titleRect.Contains(e.Location)) Then
    78. x = e.X
    79. y = e.Y
    80. End If
    81. 'Check and press the header buttons.
    82. Dim mousePos As Point = Me.PointToClient(Control.MousePosition)
    83. If (miniBoxRect.Contains(mousePos)) Then
    84. miniState = ButtonState.Pushed
    85. ElseIf (maxBoxRect.Contains(mousePos)) Then
    86. maxState = ButtonState.Pushed
    87. ElseIf (closeBoxRect.Contains(mousePos)) Then
    88. closeState = ButtonState.Pushed
    89. End If
    90. End Sub
    91. Private Sub CustomBorderColorForm_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
    92. 'Move and refresh.
    93. If (x <> 0 And y <> 0) Then
    94. Me.Location = New Point(Me.Left + e.X - x, Me.Top + e.Y - y)
    95. Me.Refresh()
    96. End If
    97. End Sub
    98. Private Sub CustomBorderColorForm_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseUp
    99. 'Reset the mouse point.
    100. x = 0
    101. y = 0
    102. 'Check the button states and modify the window state.
    103. If miniState = ButtonState.Pushed Then
    104. Me.WindowState = FormWindowState.Minimized
    105. miniState = ButtonState.Normal
    106. ElseIf maxState = ButtonState.Pushed Then
    107. If Me.WindowState = FormWindowState.Normal Then
    108. Me.WindowState = FormWindowState.Maximized
    109. maxState = ButtonState.Checked
    110. Else
    111. Me.WindowState = FormWindowState.Normal
    112. maxState = ButtonState.Normal
    113. End If
    114. ElseIf closeState = ButtonState.Pushed Then
    115. Me.Close()
    116. End If
    117. End Sub
    118. 'Handle this event to maxmize/normalize the form via double clicking the title bar.
    119. Private Sub CustomBorderColorForm_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseDoubleClick
    120. If (titleRect.Contains(e.Location)) Then
    121. If Me.WindowState = FormWindowState.Normal Then
    122. Me.WindowState = FormWindowState.Maximized
    123. maxState = ButtonState.Checked
    124. Else
    125. Me.WindowState = FormWindowState.Normal
    126. maxState = ButtonState.Normal
    127. End If
    128. End If
    129. End Sub
    130. End Class


    Mfg.eniking1998
    Bilder
    • Anhang.png

      262,91 kB, 1.366×768, 343 mal angesehen
    Die korrekte Lösung würde lauten WndProc zu überschreiben und diese Message abzufangen:
    msdn.microsoft.com/en-us/libra…op/dd145212(v=vs.85).aspx


    Dann über m.HWND ein Graphics Objekt erstellen und damit zeichnen.


    Opensource Audio-Bibliothek auf github: KLICK, im Showroom oder auf NuGet.