VB2010平面移動
VisualBasic2010においてMouseDown、DragDropを用いてPanelを移動させるコードを書きました。
Panel4へPanel1,Panel2,Panel3を移動させるというものです。
例)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Shown
Panel4.AllowDrop = True
End Sub
Private Sub Panel1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseDown
Panel1.DoDragDrop(Panel1, DragDropEffects.Move)
End Sub
Private Sub Panel2_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel2.MouseDown
Panel2.DoDragDrop(Panel2, DragDropEffects.Move)
End Sub
Private Sub Panel3_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel3.MouseDown
Panel3.DoDragDrop(Panel3, DragDropEffects.Move)
End Sub
Private Sub Panel4_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Panel4.DragDrop
Dim srsPnl As Panel = e.Data.GetData(GetType(Panel))
Dim dstPnl As New Panel
dstPnl.Size = srsPnl.Size
dstPnl.Location = Panel4.PointToClient(CursorPosition) 'New Point(e.X, e.Y)
dstPnl.BackColor = srsPnl.BackColor
AddHandler dstPnl.MouseDown, AddressOf dstPnl_MouseDown
AddHandler dstPnl.MouseMove, AddressOf dstPnl_MouseMove
Panel4.Controls.Add(dstPnl)
End Sub
Private Sub Panel4_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles Panel4.DragEnter
If e.Data.GetDataPresent(GetType(Panel)) Then
e.Effect = DragDropEffects.Move
End If
End Sub
Private previousPos As Point
Private Sub dstPnl_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseDown
previousPos = CursorPosition()
End Sub
Private Sub dstPnl_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
If e.Button = Windows.Forms.MouseButtons.Left Then
Dim nowPos As Point = CursorPosition()
DirectCast(sender, Panel).Left += nowPos.X - previousPos.X
DirectCast(sender, Panel).Top += nowPos.Y - previousPos.Y
Console.WriteLine(nowPos.X & "-" & previousPos.X)
previousPos = nowPos
End If
End Sub
Function CursorPosition() As Point
Return New Point(CInt(Cursor.Position.X / 10) * 10, CInt(Cursor.Position.Y / 10) * 10)
End Function
このようにした場合、初めに移動させたものが最前にきてしまいます。(画像参照)
私はPanel1(黒)を最前に持っていきたいと考えています。
もしお時間等ありましたら、お力添えをいただけると嬉しく思います。
どうかよろしくお願いします。
お礼
ありがとうございました。