TSB wird bei Maximierung Schwarz - DockProblem mit TSB

  • VB.NET

Es gibt 8 Antworten in diesem Thema. Der letzte Beitrag () ist von mbfan.

    TSB wird bei Maximierung Schwarz - DockProblem mit TSB

    Hallo,
    ich habe ein Problem mit meinem aktuellen Programmversuch.
    Ich habe mit dem TSB-Code (weiß nicht mehr von wo genau in diesem Forum hier) ein ToolStrip in die Fensterleiste gemacht. Ich habe nun zwei Probleme:
    1. Wenn ich die Form maximiere, wird die Fenserleiste oben schwarz, wie es in Vista normalerweise ist.
    Kann man das unter Win7 ändern?
    2. Ich habe in der Form ein TableLayout drin. Wenn ich das nicht auf Dock = None stelle, kann ich das Fenster an den Seiten nicht mehr bewegen, da die Ränder vom TL überlagert werden.
    Kann man das ändern?
    Spoiler anzeigen
    Das Ergebnis

    VB.NET-Quellcode

    1. Public Class Hauptform
    2. Private Sub Hauptform_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    3. End Sub
    4. #Region " TSB-Code "
    5. Private dwmMargins As Dwm.MARGINS
    6. Private _marginOk As Boolean
    7. Private _aeroEnabled As Boolean
    8. Public Sub New()
    9. If Environment.OSVersion.Version.Major >= 6 Then
    10. Dim enabled As Integer = 0
    11. Dim response As Integer = Dwm.DwmIsCompositionEnabled(enabled)
    12. _aeroEnabled = enabled = 1
    13. End If
    14. If _aeroEnabled = True Then
    15. SetStyle(ControlStyles.ResizeRedraw, True)
    16. InitializeComponent()
    17. ToolStrip1.Renderer = New NCARenderer()
    18. DoubleBuffered = True
    19. End If
    20. End Sub
    21. Public Shared Function LoWord(ByVal dwValue As Integer) As Integer
    22. Return dwValue And &HFFFF
    23. End Function
    24. Public Shared Function HiWord(ByVal dwValue As Integer) As Integer
    25. Return (dwValue >> 16) And &HFFFF
    26. End Function
    27. Private Sub Form1_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
    28. If _aeroEnabled = True Then
    29. ToolStrip1.MaximumSize = New Size(Width - 100 - ToolStrip1.Left, 0)
    30. End If
    31. End Sub
    32. Private Sub Form1_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
    33. If _aeroEnabled = True Then
    34. If dwmMargins.cyTopHeight < ToolStrip1.Bottom Then
    35. dwmMargins.cyTopHeight = ToolStrip1.Bottom
    36. End If
    37. Dwm.DwmExtendFrameIntoClientArea(Me.Handle, dwmMargins)
    38. 'ToolStripButton2.Visible = True
    39. Else
    40. 'ToolStripButton2.Visible = False
    41. End If
    42. End Sub
    43. Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    44. If _aeroEnabled Then
    45. e.Graphics.Clear(Color.Transparent)
    46. e.Graphics.FillRectangle(SystemBrushes.ButtonFace, Rectangle.FromLTRB(dwmMargins.cxLeftWidth - 0, dwmMargins.cyTopHeight - 0, Width - dwmMargins.cxRightWidth - 0, Height - dwmMargins.cyBottomHeight - 0))
    47. Else
    48. e.Graphics.Clear(Color.FromArgb(&HC2, &HD9, &HF7))
    49. End If
    50. End Sub
    51. Protected Overrides Sub WndProc(ByRef m As Message)
    52. Dim WM_NCCALCSIZE As Integer = &H83
    53. Dim WM_NCHITTEST As Integer = &H84
    54. Dim result As IntPtr
    55. Dim dwmHandled As Integer = Dwm.DwmDefWindowProc(m.HWnd, m.Msg, m.WParam, m.LParam, result)
    56. If dwmHandled = 1 Then
    57. m.Result = result
    58. Return
    59. End If
    60. If m.Msg = WM_NCCALCSIZE AndAlso CInt(m.WParam) = 1 Then
    61. Dim nccsp As SIZE_PARAMS = CType(System.Runtime.InteropServices.Marshal.PtrToStructure(m.LParam, GetType(SIZE_PARAMS)), SIZE_PARAMS)
    62. nccsp.rect0.Top += 0
    63. nccsp.rect0.Bottom += 0
    64. nccsp.rect0.Left += 0
    65. nccsp.rect0.Right += 0
    66. If (Not _marginOk) Then
    67. dwmMargins.cyTopHeight = nccsp.rect2.Top - nccsp.rect1.Top
    68. dwmMargins.cxLeftWidth = nccsp.rect2.Left - nccsp.rect1.Left
    69. dwmMargins.cyBottomHeight = nccsp.rect1.Bottom - nccsp.rect2.Bottom
    70. dwmMargins.cxRightWidth = nccsp.rect1.Right - nccsp.rect2.Right
    71. _marginOk = True
    72. End If
    73. System.Runtime.InteropServices.Marshal.StructureToPtr(nccsp, m.LParam, False)
    74. m.Result = IntPtr.Zero
    75. ElseIf m.Msg = WM_NCHITTEST AndAlso CInt(m.Result) = 0 Then
    76. m.Result = HitTestNCA(m.HWnd, m.WParam, m.LParam)
    77. Else
    78. MyBase.WndProc(m)
    79. End If
    80. End Sub
    81. Private Function HitTestNCA(ByVal hwnd As IntPtr, ByVal wparam As IntPtr, ByVal lparam As IntPtr) As IntPtr
    82. Dim HTCLIENT As Integer = 1
    83. Dim HTCAPTION As Integer = 2
    84. Dim HTLEFT As Integer = 10
    85. Dim HTRIGHT As Integer = 11
    86. Dim HTTOP As Integer = 12
    87. Dim HTTOPLEFT As Integer = 13
    88. Dim HTTOPRIGHT As Integer = 14
    89. Dim HTBOTTOM As Integer = 15
    90. Dim HTBOTTOMLEFT As Integer = 16
    91. Dim HTBOTTOMRIGHT As Integer = 17
    92. Dim p As New Point(LoWord(CInt(lparam)), HiWord(CInt(lparam)))
    93. Dim topleft As Rectangle = RectangleToScreen(New Rectangle(0, 0, dwmMargins.cxLeftWidth, dwmMargins.cxLeftWidth))
    94. If topleft.Contains(p) Then
    95. Return New IntPtr(HTTOPLEFT)
    96. End If
    97. Dim topright As Rectangle = RectangleToScreen(New Rectangle(Width - dwmMargins.cxRightWidth, 0, dwmMargins.cxRightWidth, dwmMargins.cxRightWidth))
    98. If topright.Contains(p) Then
    99. Return New IntPtr(HTTOPRIGHT)
    100. End If
    101. Dim botleft As Rectangle = RectangleToScreen(New Rectangle(0, Height - dwmMargins.cyBottomHeight, dwmMargins.cxLeftWidth, dwmMargins.cyBottomHeight))
    102. If botleft.Contains(p) Then
    103. Return New IntPtr(HTBOTTOMLEFT)
    104. End If
    105. Dim botright As Rectangle = RectangleToScreen(New Rectangle(Width - dwmMargins.cxRightWidth, Height - dwmMargins.cyBottomHeight, dwmMargins.cxRightWidth, dwmMargins.cyBottomHeight))
    106. If botright.Contains(p) Then
    107. Return New IntPtr(HTBOTTOMRIGHT)
    108. End If
    109. Dim top As Rectangle = RectangleToScreen(New Rectangle(0, 0, Width, dwmMargins.cxLeftWidth))
    110. If top.Contains(p) Then
    111. Return New IntPtr(HTTOP)
    112. End If
    113. Dim cap As Rectangle = RectangleToScreen(New Rectangle(0, dwmMargins.cxLeftWidth, Width, dwmMargins.cyTopHeight - dwmMargins.cxLeftWidth))
    114. If cap.Contains(p) Then
    115. Return New IntPtr(HTCAPTION)
    116. End If
    117. Dim left As Rectangle = RectangleToScreen(New Rectangle(0, 0, dwmMargins.cxLeftWidth, Height))
    118. If left.Contains(p) Then
    119. Return New IntPtr(HTLEFT)
    120. End If
    121. Dim right As Rectangle = RectangleToScreen(New Rectangle(Width - dwmMargins.cxRightWidth, 0, dwmMargins.cxRightWidth, Height))
    122. If right.Contains(p) Then
    123. Return New IntPtr(HTRIGHT)
    124. End If
    125. Dim bottom As Rectangle = RectangleToScreen(New Rectangle(0, Height - dwmMargins.cyBottomHeight, Width, dwmMargins.cyBottomHeight))
    126. If bottom.Contains(p) Then
    127. Return New IntPtr(HTBOTTOM)
    128. End If
    129. Return New IntPtr(HTCLIENT)
    130. End Function
    131. #End Region
    132. End Class
    Mein TSB-Code in der Hauptform

    VB.NET-Quellcode

    1. Option Explicit On
    2. Option Strict On
    3. Imports System.Runtime.InteropServices
    4. Imports System.Drawing.Drawing2D
    5. #Region "DWM"
    6. Public Class Dwm
    7. _
    8. Public Shared Function DwmDefWindowProc(ByVal hwnd As IntPtr, ByVal msg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr, ByRef result As IntPtr) As Integer
    9. End Function
    10. _
    11. Public Shared Function DwmIsCompositionEnabled(ByRef pfEnabled As Integer) As Integer
    12. End Function
    13. _
    14. Public Shared Function DwmExtendFrameIntoClientArea(ByVal hdc As IntPtr, ByRef marInset As MARGINS) As Integer
    15. End Function
    16. _
    17. Public Structure MARGINS
    18. Public cxLeftWidth As Integer
    19. Public cxRightWidth As Integer
    20. Public cyTopHeight As Integer
    21. Public cyBottomHeight As Integer
    22. Public Sub New(ByVal Left As Integer, ByVal Right As Integer, ByVal Top As Integer, ByVal Bottom As Integer)
    23. Me.cxLeftWidth = Left
    24. Me.cxRightWidth = Right
    25. Me.cyTopHeight = Top
    26. Me.cyBottomHeight = Bottom
    27. End Sub
    28. End Structure
    29. End Class
    30. #End Region
    31. #Region "NCARenderer"
    32. Public Class NCARenderer
    33. Inherits Office2007Renderer.Office2007Renderer
    34. Protected Overrides Sub OnRenderToolStripBackground(ByVal e As ToolStripRenderEventArgs)
    35. If e.ToolStrip.IsDropDown Then
    36. MyBase.OnRenderToolStripBackground(e)
    37. Else
    38. e.Graphics.Clear(Color.Transparent)
    39. End If
    40. End Sub
    41. Protected Overrides Sub OnRenderToolStripBorder(ByVal e As ToolStripRenderEventArgs)
    42. If e.ToolStrip.IsDropDown Then
    43. MyBase.OnRenderToolStripBorder(e)
    44. End If
    45. End Sub
    46. Protected Overrides Sub OnRenderItemText(ByVal e As ToolStripItemTextRenderEventArgs)
    47. If e.ToolStrip.IsDropDown Then
    48. MyBase.OnRenderItemText(e)
    49. Else
    50. Dim path As New GraphicsPath()
    51. path.AddString(e.Text, e.TextFont.FontFamily, CInt(Fix(e.TextFont.Style)), e.TextFont.Size + 2, e.TextRectangle.Location, New StringFormat())
    52. e.Graphics.SmoothingMode = SmoothingMode.HighQuality
    53. e.Graphics.FillPath(Brushes.Black, path)
    54. path.Dispose()
    55. End If
    56. End Sub
    57. Protected Overrides Sub OnRenderOverflowButtonBackground(ByVal e As ToolStripItemRenderEventArgs)
    58. If e.Item.Selected Then
    59. e.Graphics.Clear(Color.FromArgb(20, Color.Navy))
    60. End If
    61. Dim r As Rectangle = Rectangle.Empty
    62. If e.Item.RightToLeft = RightToLeft.Yes Then
    63. r = New Rectangle(0, e.Item.Height - 8, 9, 5)
    64. Else
    65. r = New Rectangle(e.Item.Width - 12, e.Item.Height - 16, 9, 5)
    66. End If
    67. MyBase.DrawArrow(New ToolStripArrowRenderEventArgs(e.Graphics, e.Item, r, SystemColors.ControlText, ArrowDirection.Down))
    68. e.Graphics.DrawLine(SystemPens.ControlText, CInt(Fix(r.Right - 7)), CInt(Fix(r.Y - 2)), CInt(Fix(r.Right - 3)), CInt(Fix(r.Y - 2)))
    69. End Sub
    70. End Class
    71. #End Region
    72. #Region "Api"
    73. _
    74. Public Structure RECT
    75. Public Left As Integer
    76. Public Top As Integer
    77. Public Right As Integer
    78. Public Bottom As Integer
    79. End Structure
    80. _
    81. Public Structure SIZE_PARAMS
    82. Public rect0, rect1, rect2 As RECT
    83. Public lppos As IntPtr
    84. End Structure
    85. #End Region
    Mein Code in der TSB.vb-Datei

    Wenn noch weitere Informationen benötigt werden, bitte einfach schreiben.
    mbfan
    You should live for that what you belive. - Drag-Drop Beschreibung
    Gut, dass da Option Strict On steht.
    Kannst Du den Code bitte so schreiben, dass beim Einfügen keine Fehler kommen?
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    Ich verstehe dein Anliegen leider nicht ganz.
    Beim Einfügen wohin?
    mbfan
    You should live for that what you belive. - Drag-Drop Beschreibung
    Du suchst eine Fehler und postest Code.
    Ich möchte Dir helfen und kopiere den Code in ein leeres Programm, um Deinen Effekt nachzuvollziehen.
    Dabei treten elementare Fehler auf, die Du hineingeschrieben hast und die ich nicht bereit bin, rauszumachen.
    Füge also Deinen Code in eine leere Programmhülse ein und Du wirst merken.
    z.B.
    Funktion ohne Rückgabewert.
    "_" -Zeilen im Code
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    Add:
    Ich habe das Thema nun wieder gefunden, es war doch nicht hier.
    Ich habe folgendes verwendet:
    vbarchiv.net/tipps/tipp_1994-t…e-in-der-titelleiste.html
    mbfan
    You should live for that what you belive. - Drag-Drop Beschreibung
    Hättest Du selbst finden können:

    VB.NET-Quellcode

    1. Protected Overrides Sub OnRenderToolStripBackground(ByVal e As ToolStripRenderEventArgs)
    2. If e.ToolStrip.IsDropDown Then
    3. MyBase.OnRenderToolStripBackground(e)
    4. Else
    5. 'e.Graphics.Clear(Color.Transparent)
    6. e.Graphics.Clear(Color.Violet) ' schreib hier eine Farbe rein, die Dir gefällt.
    7. End If
    8. End Sub
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    Danke, jedoch hatte ich das eigentlich nicht gesucht.
    Wenn ich die Form Maximiere, wird die Titelleiste schwarz, nicht nur der Button. Wenn ich jedoch das Programm minimiere und dann wieder maximiere, ist dies nicht der Fall. Deswegen habe ich jetzt das Resize-Event dahingehend erweitert:

    VB.NET-Quellcode

    1. Dim maxi As Boolean = False
    2. ' Reaktion auf Form-Ereignisse
    3. Private Sub Form1_Resize(ByVal sender As Object, _
    4. ByVal e As System.EventArgs) Handles Me.Resize
    5. ToolStrip1.MaximumSize = New Size(Width - 100 - ToolStrip1.Left, 0)
    6. If WindowState = FormWindowState.Maximized Then
    7. If maxi = False Then
    8. maxi = True
    9. Me.WindowState = FormWindowState.Minimized
    10. Me.WindowState = FormWindowState.Maximized
    11. maxi = False
    12. End If
    13. End If
    14. End Sub

    Mit dem Dock-Problem könnte ich mir jetzt einen Grund herleiten: Wenn das Control TableLayoutPanel gedock wird, wird es aufgrund der Erweiterung des Client-Bereiches in den NonClient-Bereich ebenfalls über die eigentlichen Formränder erweitern werden. Ich hoffe, dass ich mit dieser Information das Problem lösen kann.
    mbfan
    You should live for that what you belive. - Drag-Drop Beschreibung
    Wie hast Du es hinbekommen, dass die üblichen Fenster-Boxen angezeigt werden (Minimize, Maximize, Close)?
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    Die waren nach dem Mini-Maximieren bei mir wieder da, ohne dass ich etwas gemacht habe.
    Das Dock-Problem habe ich nun ebenfalls "gelöst": Ich lasse Doch=None, aber ankere das TLP an allen vier Seiten. Das TLP muss beim Anfang eine Position von 10;30 haben und 10 Pixel vom rechten, linken und unteren Formende entfernt sein. Ins FormResize-Event muss dann nur noch diese kleine Erweiterung rein:

    VB.NET-Quellcode

    1. Dim maxi As Boolean = False
    2. ' Reaktion auf Form-Ereignisse
    3. Private Sub Form1_Resize(ByVal sender As Object, _
    4. ByVal e As System.EventArgs) Handles Me.Resize
    5. ToolStrip1.MaximumSize = New Size(Width - 100 - ToolStrip1.Left, 0)
    6. If WindowState = FormWindowState.Maximized Then
    7. If maxi = False Then
    8. maxi = True
    9. Me.WindowState = FormWindowState.Minimized
    10. Me.WindowState = FormWindowState.Maximized
    11. TableLayoutPanel1.Location = New Drawing.Point(14, 37)
    12. maxi = False
    13. End If
    14. Else
    15. TableLayoutPanel1.Location = New Drawing.Point(10, 30)
    16. End If
    17. End Sub

    mbfan
    You should live for that what you belive. - Drag-Drop Beschreibung