Control verschwindet, sofern die Maus nicht drauf ist

  • VB.NET
  • .NET (FX) 4.5–4.8

Es gibt 2 Antworten in diesem Thema. Der letzte Beitrag () ist von RodFromGermany.

    Control verschwindet, sofern die Maus nicht drauf ist

    Hallo.

    Ich habe ein Problem und zwar verschwindet das Control,
    wenn die Maus nicht in der nähe bzw. auf dem Control ist.

    Code (nicht von mir):



    VB.NET-Quellcode

    1. Public Class Test : Inherits TrackBar
    2. Public Enum Stater As Short
    3. normal = 0
    4. press = 1
    5. hover = 2
    6. End Enum
    7. Public stat As Stater = stater.normal
    8. Public Sub New()
    9. MyBase.New()
    10. MyBase.SetStyle(ControlStyles.DoubleBuffer, True)
    11. MyBase.SetStyle(ControlStyles.SupportsTransparentBackColor, True)
    12. SetStyle(ControlStyles.AllPaintingInWmPaint Or ControlStyles.OptimizedDoubleBuffer Or ControlStyles.UserPaint Or ControlStyles.ResizeRedraw, True)
    13. UpdateStyles()
    14. End Sub
    15. Protected Overrides Sub OnPaintBackground(ByVal pevent As System.Windows.Forms.PaintEventArgs)
    16. MyBase.OnPaintBackground(pevent)
    17. End Sub
    18. Protected Overrides ReadOnly Property CreateParams() As CreateParams
    19. Get
    20. Dim cp As CreateParams = MyBase.CreateParams
    21. cp.ExStyle = cp.ExStyle Or &H20
    22. 'WS_EX_TRANSPARENT
    23. Return cp
    24. End Get
    25. End Property
    26. Protected Overrides Sub OnPaint(ByVal e As System.Windows.Forms.PaintEventArgs)
    27. MyBase.OnPaint(e)
    28. MyBase.OnPaintBackground(e)
    29. With (e.Graphics)
    30. .SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
    31. .PixelOffsetMode = Drawing2D.PixelOffsetMode.HighQuality
    32. .InterpolationMode = Drawing2D.InterpolationMode.HighQualityBilinear
    33. .CompositingQuality = Drawing2D.CompositingQuality.HighQuality
    34. e.Graphics.DrawLine(New Pen(New SolidBrush(NonValueLine), 3), New Point(0 + 12, CInt(Height / 2)), New Point(Width - 12, CInt(Height / 2)))
    35. Dim spektrum As Integer = Maximum - Minimum
    36. spectre = spektrum
    37. Dim pixel As Double = (((Math.Abs(Minimum) + Value) / spektrum) * (Width - 24)) + 12
    38. Dim CurrSi As Integer = CInt(Me.Height / 2)
    39. Dim größe As New Rectangle(CInt(pixel - 12), CurrSi - 12, 24, 24)
    40. Dim größe1 As New Rectangle(CInt(pixel - 5), CurrSi - 5, 10, 10)
    41. where = größe
    42. If stat = Stater.normal Then
    43. e.Graphics.FillEllipse(New SolidBrush(OutherCircleColor), größe)
    44. e.Graphics.FillEllipse(New SolidBrush(LineColor), größe1)
    45. e.Graphics.DrawLine(New Pen(New SolidBrush(LineColor), 3), New Point(0 + 12, CInt(Height / 2)), New Point(CInt(pixel), CInt(Height / 2)))
    46. ElseIf stat = Stater.hover Then
    47. e.Graphics.FillEllipse(New SolidBrush(Dark_Color(OutherCircleColor, 0.75)), größe)
    48. e.Graphics.FillEllipse(New SolidBrush(LineColor), größe1)
    49. e.Graphics.DrawLine(New Pen(New SolidBrush(LineColor), 3), New Point(0 + 12, CInt(Height / 2)), New Point(CInt(pixel), CInt(Height / 2)))
    50. End If
    51. End With
    52. e.Graphics.DrawString(MyBase.Value.ToString, New Font("Segoe UI", CSng(7)), Brushes.Black, New Point(20, 24))
    53. End Sub
    54. Dim where As Rectangle
    55. Dim drin As Boolean
    56. Dim x, y As Integer
    57. Dim spectre As Integer
    58. Private Sub DroidTrackbar_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
    59. If x > where.X AndAlso x < (where.Width + where.X) Then
    60. If y > where.Y AndAlso y < (where.Height + where.Y) Then
    61. drin = True
    62. stat = Stater.normal
    63. End If
    64. End If
    65. End Sub
    66. Private Sub DroidTrackbar_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
    67. Try
    68. x = e.X
    69. y = e.Y
    70. If drin Then
    71. where.X = e.X
    72. Value = CInt((((e.X) / (Width)) * spectre) - Math.Abs(Minimum))
    73. If Value > Maximum Then Value = Maximum
    74. If Value < Minimum Then Value = Minimum
    75. Else
    76. If x > where.X AndAlso x < (where.Width + where.X) Then
    77. If y > where.Y AndAlso y < (where.Height + where.Y) Then
    78. stat = Stater.hover
    79. Else : stat = Stater.normal
    80. End If
    81. Else : stat = Stater.normal
    82. End If
    83. End If
    84. Me.Invalidate()
    85. Catch ex As Exception
    86. End Try
    87. End Sub
    88. Private Function Dark_Color(ByVal Col As Color, ByVal percent As Double) As Color
    89. If (percent = 0.5) Then
    90. Return Col
    91. ElseIf (percent > 0.5) Then
    92. Return Color.FromArgb(Col.A, CInt((255 - Col.R) * ((percent - 0.5) * 2) + Col.R), CInt((255 - Col.G) * ((percent - 0.5) * 2) + Col.G), CInt((255 - Col.B) * ((percent - 0.5) * 2) + Col.B))
    93. Else
    94. Return Color.FromArgb(Col.A, CInt(Col.R * percent * 2), CInt(Col.G * percent * 2), CInt(Col.B * percent * 2))
    95. End If
    96. End Function
    97. Private Sub DroidTrackbar_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseUp
    98. drin = False
    99. End Sub
    100. Dim lin As Color = Color.FromArgb(70, 151, 196)
    101. Public Property LineColor As Color
    102. Get
    103. Return lin
    104. End Get
    105. Set(ByVal value As Color)
    106. lin = value
    107. End Set
    108. End Property
    109. Dim out As Color = Color.FromArgb(119, 206, 234)
    110. Public Property OutherCircleColor As Color
    111. Get
    112. Return out
    113. End Get
    114. Set(ByVal value As Color)
    115. out = value
    116. End Set
    117. End Property
    118. Dim non As Color = Color.FromArgb(229, 229, 229)
    119. Public Property NonValueLine As Color
    120. Get
    121. Return non
    122. End Get
    123. Set(ByVal value As Color)
    124. non = value
    125. End Set
    126. End Property
    127. Private Sub DroidTrackbar_Scroll(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Scroll
    128. Invalidate()
    129. End Sub
    130. End Class


    Kann mir jemand sagen, woran das liegt?
    @Daniel Baumert Jou, nicht nachvollziehbar.
    Erstell ein neues Projekt, kopier diesen Deinen Code in eine neue Klasse, kompiliere und zieh ein Control in Deine Form.
    Oder
    Welche Properties gegenüber Standard hast Du verändert?
    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!