| Forum: VB.NET |
Thema:
Re: Verschieben von Usercontrols |
Von:
Klaas Wedemeyer (
17.06.2004 17:20) |
Hi Stephan,
ich würde das mit Drag & Drop machen. Hie ein bischen Code aus meiner Anwendung (das meiste steht so in der Hilfe drin):
' wenn man die Maustaste druckt, dann den Punkt merken
' mCtrl ist das Control, welches verschoben werden kann
' Wenn Du mehrere hast und Du die Controls sovieso ableitest, würde ich den Code in die Controls schreiben
' Sonst mußt Du die Events der Form abfangen und dann das Control finden, über dem die Maus ist. Ich glaube da gibt es eine Funktion für
Private dragBoxFromMouseDown As Rectangle
Private Sub mCtrl_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles mCtrl.ControlMouseDown
' Remember the point where the mouse down occurred. The DragSize indicates
' the size that the mouse can move before a drag event should be started.
Dim dragSize As Size = SystemInformation.DragSize
' Create a rectangle using the DragSize, with the mouse position being
' at the center of the rectangle.
dragBoxFromMouseDown = New Rectangle(New Point(e.X - (dragSize.Width / 2), _
e.Y - (dragSize.Height / 2)), dragSize)
End Sub
' Wenn man sie wieder losläßt, dann den Punkt vergessen
Private Sub mCtrl_MouseUp(ByVal sender As Object, ByVal e As MouseEventArgs) Handles mCtrl.ControlMouseUp
' Reset the drag rectangle when the mouse button is raised.
dragBoxFromMouseDown = Rectangle.Empty
End Sub
' Wenn sich die Maus bewegt, dann Drag und Drop auslösen:
Private Sub mCtrl_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles mCtrl.ControlMouseMove
If ((e.Button And MouseButtons.Left) = MouseButtons.Left) Then
' If the mouse moves outside the rectangle, start the drag.
If (Rectangle.op_Inequality(dragBoxFromMouseDown, Rectangle.Empty) And _
Not dragBoxFromMouseDown.Contains(e.X, e.Y)) Then
' The screenOffset is used to account for any desktop bands
' that may be at the top or left side of the screen when
' determining when to cancel the drag drop operation.
screenOffset = SystemInformation.WorkingArea.Location
' Proceed with the drag and drop, passing in the list item.
Dim dropEffect As DragDropEffects = mCtrl.Ctrl.DoDragDrop(mCtrl, DragDropEffects.Move)
End If
End If
End Sub
' Darf man hier was fallen lassen
Private Sub CockpitPanel_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles MyBase.DragOver
If e.Data.GetDataPresent(GetType(DragDrobArgs)) Then
If (e.AllowedEffect And DragDropEffects.Move) = DragDropEffects.Move Then
Dim Ctrl As Control = e.Data.GetData(GetType(Control))
' Entweder Du läßt folgende Zeile weg, oder Du sorgst dafür, dass das Control auch empfänger von Drag und Drop sein kann.
' Auf jeden Fall schieb dieser Befehl das Control unter die Maus und macht so das Control zum Ziel des Drop
ctrl.Location=GetPoint(e.X, e.Y)
e.Effect = DragDropEffects.Move
Else
e.Effect = DragDropEffects.None
End If
Else
e.Effect = DragDropEffects.None
End If
End Sub
' Und nun das ganze beenden
Private Sub DragDrop(ByVal sender As Object, ByVal e As DragEventArgs) Handles MyBase.DragDrop
If e.Data.GetDataPresent(GetType(DragDrobArgs)) Then
If (e.AllowedEffect And DragDropEffects.Move) = DragDropEffects.Move Then
ctrl.Location=GetPoint(e.X, e.Y)
End If
End If
End Sub
So und das wars. Ich hoffe, ich habe nicht so viel von meinem Code mitkopiert und Du kommst mit dem kram klar.
Viel Glück, Klaas
| Betreff |
Von |
Datum |
|
|
|
|
Stephan
|
22.06.2004 21:03 |
|
|
|
Antworten
Vorsicht bei der Eingabe: Die Zeichen ' oder -- sind nicht erlaubt!