Form zeichnet UserControl nicht

  • VB.NET

Es gibt 10 Antworten in diesem Thema. Der letzte Beitrag () ist von sirblacksoul.

    Form zeichnet UserControl nicht

    Guten Tag,
    ich möchte ein UserControl schreiben das ich auch zur Laufzeit resizen kann, das funktioniert soweit auch, nur das einzigste Problem ist, das er jetzt nicht mehr meine Form zeichnet, könntet ihr mir vielleicht verraten warum?





    VB.NET-Quellcode

    1. Public Class UserControl1
    2. Private cl_cmsLabel As New ContextMenuStrip
    3. Private WithEvents cl_Control As Control
    4. Private WithEvents cl_OwnerForm As Form
    5. Private cl_nGRS As Integer = 6
    6. Private cl_bInResizeMode As Boolean
    7. Private cl_bMoveControl As Boolean
    8. Private cl_pLastMousePos As Point
    9. Private cl_nMinWidth As Integer = 20
    10. Private cl_nMinHeigth As Integer = 20
    11. Private cl_bActive As Boolean
    12. Private cl_cGrabRects() As Control = { _
    13. New Control, _
    14. New Control, _
    15. New Control, _
    16. New Control, _
    17. New Control, _
    18. New Control, _
    19. New Control, _
    20. New Control}
    21. Public Sub Deactivate()
    22. RemoveHandler cl_OwnerForm.Paint, AddressOf ControlPaint
    23. RemoveHandler cl_Control.MouseMove, AddressOf ControlMouseMove
    24. cl_Control.Cursor = Cursors.Default
    25. For nCount As Integer = 0 To cl_cGrabRects.Length - 1
    26. RemoveHandler cl_cGrabRects(nCount).Paint, AddressOf GrabRectPaint
    27. RemoveHandler cl_cGrabRects(nCount).MouseClick, AddressOf GrabRectMouseClick
    28. RemoveHandler cl_cGrabRects(nCount).MouseMove, AddressOf GrabRectMouseMove
    29. RemoveHandler cl_cGrabRects(nCount).MouseDown, AddressOf GrabRectMouseDown
    30. RemoveHandler cl_cGrabRects(nCount).MouseUp, AddressOf GrabRectMouseUp
    31. RemoveHandler cl_cGrabRects(nCount).Move, AddressOf GrabRectMove
    32. cl_cGrabRects(nCount).Visible = False
    33. Next
    34. cl_bActive = False
    35. cl_OwnerForm.Refresh()
    36. End Sub
    37. Sub New(ByVal frmOwnerForm As Form)
    38. cl_Control = Me
    39. cl_OwnerForm = frmOwnerForm
    40. End Sub
    41. Public Sub Activate()
    42. AddHandler cl_OwnerForm.Paint, AddressOf ControlPaint
    43. AddHandler cl_Control.MouseMove, AddressOf ControlMouseMove
    44. cl_Control.Cursor = Cursors.SizeAll
    45. For nCount As Integer = 0 To cl_cGrabRects.Length - 1
    46. AddHandler cl_cGrabRects(nCount).Paint, AddressOf GrabRectPaint
    47. AddHandler cl_cGrabRects(nCount).MouseClick, AddressOf GrabRectMouseClick
    48. AddHandler cl_cGrabRects(nCount).MouseMove, AddressOf GrabRectMouseMove
    49. AddHandler cl_cGrabRects(nCount).MouseDown, AddressOf GrabRectMouseDown
    50. AddHandler cl_cGrabRects(nCount).MouseUp, AddressOf GrabRectMouseUp
    51. AddHandler cl_cGrabRects(nCount).Move, AddressOf GrabRectMove
    52. cl_cGrabRects(nCount).Visible = True
    53. Next
    54. cl_bActive = True
    55. cl_OwnerForm.Refresh()
    56. End Sub
    57. Private Sub ControlMouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles Me.MouseDown
    58. If e.Button = Windows.Forms.MouseButtons.Left Then
    59. cl_bMoveControl = True
    60. cl_pLastMousePos = New Point(e.X, e.Y)
    61. For nCount As Integer = 0 To cl_cGrabRects.Length - 1
    62. If cl_bActive = True Then cl_cGrabRects(nCount).Visible = False
    63. Next
    64. cl_OwnerForm.Refresh()
    65. ElseIf e.Button = Windows.Forms.MouseButtons.Right Then
    66. AddHandler cl_cmsLabel.Opening, AddressOf cmsLabel_Opening
    67. ContextMenuStrip = cl_cmsLabel
    68. End If
    69. End Sub
    70. Private Sub ControlMouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) 'Handles Me.MouseMove
    71. If cl_bMoveControl = True Then
    72. cl_Control.Left = cl_Control.Left + e.X - cl_pLastMousePos.X
    73. cl_Control.Top = cl_Control.Top + e.Y - cl_pLastMousePos.Y
    74. cl_OwnerForm.Refresh()
    75. End If
    76. End Sub
    77. Private Sub ControlMouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles Me.MouseUp
    78. cl_bMoveControl = False
    79. For nCount As Integer = 0 To cl_cGrabRects.Length - 1
    80. If cl_bActive = True Then cl_cGrabRects(nCount).Visible = True
    81. Next
    82. cl_OwnerForm.Refresh()
    83. End Sub
    84. Private Sub cmsLabel_Opening(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
    85. cl_cmsLabel.Items.Clear()
    86. If cl_bActive = False Then
    87. cl_cmsLabel.Items.Add("Activate Control", Nothing, New System.EventHandler(AddressOf SelectedChildMenu_OnClick))
    88. Else
    89. cl_cmsLabel.Items.Add("Deactivate Control", Nothing, New System.EventHandler(AddressOf SelectedChildMenu_OnClick))
    90. End If
    91. RemoveHandler cl_cmsLabel.Opening, AddressOf cmsLabel_Opening
    92. e.Cancel = False
    93. End Sub
    94. Private Sub SelectedChildMenu_OnClick(ByVal sender As Object, ByVal e As System.EventArgs)
    95. If sender.ToString = "Activate Control" Then
    96. Activate()
    97. ElseIf sender.ToString = "Deactivate Control" Then
    98. Deactivate()
    99. End If
    100. End Sub
    101. Public Sub ControlPaint(ByVal sender As Object, ByVal e As PaintEventArgs)
    102. If cl_bInResizeMode = True Or cl_bMoveControl = True Then Exit Sub
    103. With e.Graphics
    104. 'Rahmen
    105. .DrawRectangle(Pens.Gray, New Rectangle( _
    106. cl_Control.Location.X - (cl_nGRS / 2), _
    107. cl_Control.Location.Y - (cl_nGRS / 2), _
    108. cl_Control.Width + cl_nGRS, _
    109. cl_Control.Height + cl_nGRS))
    110. 'Positionen für die Rects in der Reihenfolge:
    111. 'TopLeft, TopMiddle, TopRight, MiddleLeft, MiddleRight, BottomLeft,BottomMiddle,BottomRight
    112. Dim GrabRectPos() As Point = { _
    113. New Point(cl_Control.Location.X - cl_nGRS, cl_Control.Location.Y - cl_nGRS), _
    114. New Point(cl_Control.Location.X + (cl_Control.Width / 2) - (cl_nGRS / 2), cl_Control.Location.Y - cl_nGRS), _
    115. New Point(cl_Control.Location.X + cl_Control.Width, cl_Control.Location.Y - cl_nGRS), _
    116. New Point(cl_Control.Location.X - cl_nGRS, cl_Control.Location.Y + (cl_Control.Height / 2) - (cl_nGRS / 2)), _
    117. New Point(cl_Control.Location.X + (cl_Control.Width), cl_Control.Location.Y + (cl_Control.Height / 2) - (cl_nGRS / 2)), _
    118. New Point(cl_Control.Location.X - cl_nGRS, cl_Control.Location.Y + (cl_Control.Height)), _
    119. New Point(cl_Control.Location.X + (cl_Control.Width / 2) - (cl_nGRS / 2), cl_Control.Location.Y + (cl_Control.Height)), _
    120. New Point(cl_Control.Location.X + cl_Control.Width, cl_Control.Location.Y + (cl_Control.Height))}
    121. For nCount As Integer = 0 To cl_cGrabRects.Length - 1
    122. cl_cGrabRects(nCount).Location = GrabRectPos(nCount)
    123. cl_cGrabRects(nCount).Size = New Size(cl_nGRS, cl_nGRS)
    124. cl_cGrabRects(nCount).BackColor = Color.LightGray
    125. cl_cGrabRects(nCount).Parent = cl_OwnerForm
    126. Next
    127. End With
    128. End Sub
    129. Private Sub GrabRectPaint(ByVal sender As Object, ByVal e As PaintEventArgs)
    130. e.Graphics.DrawRectangle(Pens.Black, New Rectangle(0, 0, cl_nGRS - 1, cl_nGRS - 1))
    131. End Sub
    132. Private Sub GrabRectMove(ByVal sender As Object, ByVal e As System.EventArgs)
    133. Dim cCurrControl As Control = CType(sender, Control)
    134. Dim nIndex As Integer = GetCurrIndex(sender)
    135. End Sub
    136. Private Sub GrabRectMouseClick(ByVal sender As Object, ByVal e As MouseEventArgs)
    137. Dim cCurrControl As Control = CType(sender, Control)
    138. Dim nIndex As Integer = GetCurrIndex(sender)
    139. End Sub
    140. Private Sub GrabRectMouseMove(ByVal sender As Object, ByVal e As MouseEventArgs)
    141. Dim cCurrControl As Control = CType(sender, Control)
    142. Dim nIndex As Integer = GetCurrIndex(sender)
    143. Dim MousePos As Point
    144. Static cnRight As Integer
    145. Static cnBottom As Integer
    146. Select Case nIndex
    147. Case 0 : cCurrControl.Cursor = Cursors.SizeNWSE
    148. Case 1 : cCurrControl.Cursor = Cursors.SizeNS
    149. Case 2 : cCurrControl.Cursor = Cursors.SizeNESW
    150. Case 3 : cCurrControl.Cursor = Cursors.SizeWE
    151. Case 4 : cCurrControl.Cursor = Cursors.SizeWE
    152. Case 5 : cCurrControl.Cursor = Cursors.SizeNESW
    153. Case 6 : cCurrControl.Cursor = Cursors.SizeNS
    154. Case 7 : cCurrControl.Cursor = Cursors.SizeNWSE
    155. End Select
    156. If cl_bInResizeMode = True Then
    157. MousePos = cl_OwnerForm.PointToClient(Windows.Forms.Cursor.Position)
    158. cCurrControl.Left = cl_OwnerForm.PointToClient(Windows.Forms.Cursor.Position).X 'cCurrControl.Left + e.X
    159. cCurrControl.Top = cl_OwnerForm.PointToClient(Windows.Forms.Cursor.Position).Y 'cCurrControl.Top + e.Y
    160. Dim nTmpHeigth As Integer
    161. Dim nTmpWidth As Integer
    162. Dim nTmpDelta As Integer
    163. Select Case nIndex
    164. Case 0 'TopLeft GrabButton
    165. nTmpDelta = cl_Control.Top - cCurrControl.Top
    166. nTmpHeigth = cl_Control.Height + nTmpDelta
    167. nTmpDelta = cl_Control.Left - cCurrControl.Left
    168. nTmpWidth = cl_Control.Width + nTmpDelta
    169. If nTmpHeigth < cl_nMinHeigth Then Exit Select
    170. If nTmpWidth < cl_nMinWidth Then Exit Select
    171. cl_Control.Size = New Size(nTmpWidth, nTmpHeigth)
    172. cl_Control.Top = cCurrControl.Top
    173. cl_Control.Left = cCurrControl.Left
    174. Case 1 'TopMiddle GrabButton
    175. nTmpDelta = cl_Control.Top - cCurrControl.Top
    176. nTmpHeigth = cl_Control.Height + nTmpDelta
    177. If nTmpHeigth < cl_nMinHeigth Then Exit Select
    178. cl_Control.Height = nTmpHeigth
    179. cl_Control.Top = cCurrControl.Top
    180. Case 2 'TopRight GrabButton
    181. nTmpDelta = cl_Control.Top - cCurrControl.Top
    182. nTmpHeigth = cl_Control.Height + nTmpDelta
    183. nTmpWidth = cCurrControl.Left - cl_Control.Left
    184. If nTmpHeigth < cl_nMinHeigth Then Exit Select
    185. If nTmpWidth < cl_nMinWidth Then nTmpWidth = cl_nMinWidth
    186. cl_Control.Size = New Size(nTmpWidth, nTmpHeigth)
    187. cl_Control.Top = cCurrControl.Top
    188. Case 3 'MiddleLeft GrabButton
    189. nTmpDelta = cl_Control.Left - cCurrControl.Left
    190. nTmpWidth = cl_Control.Width + nTmpDelta
    191. If nTmpWidth < cl_nMinWidth Then Exit Select
    192. cl_Control.Width = nTmpWidth
    193. cl_Control.Left = cCurrControl.Left
    194. Case 4 'MiddleRight GrabButton
    195. nTmpWidth = cl_cGrabRects(4).Left - cl_Control.Left
    196. If nTmpWidth < cl_nMinWidth Then nTmpWidth = cl_nMinWidth
    197. cl_Control.Width = nTmpWidth
    198. Case 5 'BottomLeft GrabButton
    199. nTmpHeigth = cCurrControl.Top - cl_Control.Top
    200. nTmpDelta = cl_Control.Left - cCurrControl.Left
    201. nTmpWidth = cl_Control.Width + nTmpDelta
    202. If nTmpHeigth < cl_nMinHeigth Then nTmpHeigth = cl_nMinHeigth
    203. If nTmpWidth < cl_nMinWidth Then Exit Select
    204. cl_Control.Size = New Size(nTmpWidth, nTmpHeigth)
    205. cl_Control.Left = cCurrControl.Left
    206. Case 6 'BottomMiddle GrabButton
    207. nTmpHeigth = cCurrControl.Top - cl_Control.Top
    208. If nTmpHeigth < cl_nMinHeigth Then nTmpHeigth = cl_nMinHeigth
    209. cl_Control.Height = nTmpHeigth
    210. Case 7 'BottomcnRight GrabButton
    211. nTmpHeigth = cCurrControl.Top - cl_Control.Top
    212. nTmpWidth = cCurrControl.Left - cl_Control.Left
    213. If nTmpHeigth < cl_nMinHeigth Then nTmpHeigth = cl_nMinHeigth
    214. If nTmpWidth < cl_nMinWidth Then nTmpWidth = cl_nMinWidth
    215. cl_Control.Size = New Size(nTmpWidth, nTmpHeigth)
    216. End Select
    217. cl_OwnerForm.Refresh()
    218. Else
    219. cnRight = cl_Control.Right
    220. cnBottom = cl_Control.Bottom
    221. End If
    222. End Sub
    223. Private Sub GrabRectMouseDown(ByVal sender As Object, ByVal e As MouseEventArgs)
    224. cl_bInResizeMode = True
    225. For nCount As Integer = 0 To cl_cGrabRects.Length - 1
    226. cl_cGrabRects(nCount).Visible = False
    227. Next
    228. cl_OwnerForm.Refresh()
    229. End Sub
    230. Private Sub GrabRectMouseUp(ByVal sender As Object, ByVal e As MouseEventArgs)
    231. cl_bInResizeMode = False
    232. For nCount As Integer = 0 To cl_cGrabRects.Length - 1
    233. cl_cGrabRects(nCount).Visible = True
    234. Next
    235. cl_OwnerForm.Refresh()
    236. End Sub
    237. Private Function GetCurrIndex(ByVal sender As Object) As Integer
    238. Dim CurrControl As Control = CType(sender, Control)
    239. For nCount As Integer = 0 To cl_cGrabRects.Length
    240. If cl_cGrabRects(nCount) Is CurrControl Then
    241. Return nCount
    242. End If
    243. Next
    244. End Function
    245. End Class
    Nimm lieber im Sub New:

    VB.NET-Quellcode

    1. SetStyle(RedrawOnResizing, True)

    bin mir nicht sicher ob es RedrawOnResizing war ... Intellisense sei dank ;) kannst du das auch selbst rausfinden
    wenigstens weißt du was gemeint war ;)

    ich habe zwar schon öfters ein Usercontrol gemacht, aber so ein Fehler ist mir bis jetzt noch nicht gekommen.
    Ich hab mal versucht, Dein Control nachzuvollziehen, da gab es lediglich Irritationen wegen

    VB.NET-Quellcode

    1. Option Strict On

    Außerdem war der Mouse.Move-Handler geklemmt.
    Kannst Du mal bitte genau beschreiben, was nicht funktioniert?
    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!
    Naja, ich habe ein Usercontrol auf dem ein paar shapes sind. Ich möchte das es möglich ist dieses Usercontrol während der Laufzeit zu resizen, also die Größe zu ändern so wie es in einigen Programmen möglich ist, mit den 8 Kästchen an dennen ich es größer kleiner ziehen kann. Dabei habe ich ein Menü das aufspringt wenn ich einen Rechtsklick auf das Control mache und das Resizing aktivieren kann. Das funktioniert auch alles soweit wenn ich auf meine !Form! rechtsklicke kommt das Menü und ich kann es aktivieren, es zeichnet mein Rectangle und die 8 ecken zum vergrößern, was auch funktioniert. Nur fehlen jegliche Shapes meines Usercontrols. Sprick ich sehe nichts außer dem Rectangle.
    Verstanden.
    Ich musste auch die BackColor verändern, um Dein Rechteck zu sehen.
    Ich werd mal nachsehen.
    -----
    So.
    Er tut eigentlich alles, was er soll.
    Er malt die kleinen Vierecke zum Ziehen und ändert den Mauszeiger beim Anfassen.
    Mehr Aktionen habe ich nicht gefunden.
    Aber mit

    VB.NET-Quellcode

    1. Private Sub MyUserControl_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    2. e.Graphics.DrawRectangle(Pens.Gray, New Rectangle(0, 0, Me.Width - 1, Me.Height - 1))
    3. e.Graphics.DrawEllipse(Pens.Red, New Rectangle(0, 0, Me.Width - 1, Me.Height - 1))
    4. End Sub

    macht er etwas. :D

    Schreib doch mal, was Du glaubst, was es tun sollte.
    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!

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

    Ähm wenn du dir den Code genauer anschaust siehst du aber das er weitaus mehr aktionen macht. z.B. wenn du eine Ecke auswählst die vergößerung und verkleinerung. Wenn du in Visual studio mal ein Label auf dein Form ziehst, sind dort auch die Ecken zum vergrößern und wenn du es ziehst kannst du es positionierung. Ich möchte genau das realisieren, jedoch zur Laufzeit

    RodFromGermany schrieb:

    Er malt die kleinen Vierecke zum Ziehen und ändert den Mauszeiger beim Anfassen.

    Schon klar. Tut er.
    Was sollte er denn noch tun, was ich ggf. nicht gesehen habe?
    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!
    Er ändert nicht nur den Mauszeiger sondern das ganze Control. Zumindest dessen größe. Jedoch zeigt er das Control selbst nicht an, er registriert nur das es existiert.

    // habe nach sehr langem hin und her es geschafft auf die reihe zu bekommen, nachdem ich eine neue form erstellt habe, das usercontrol neu erstellt hatte und dem shape noch einen hintergrund zugewiesen hatte funktionierte alles so wie es sollte.

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „sirblacksoul“ ()