首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > VB Dotnet >

vb.net中PictureBox随鼠标拖动解决办法

2012-09-15 
vb.net中PictureBox随鼠标拖动我想在一个背景图片上添加设备的功能,是通过右键“添加设备”在Form上添加了Pi

vb.net中PictureBox随鼠标拖动
我想在一个背景图片上添加设备的功能, 是通过右键“添加设备”在Form上添加了PictrueBox(设备图片), 但是这个图片是可以随便移动到任意位置的! 并且下次运行程序这些添加的设备要全部都显示出来!

请高人指点我怎么做! 最好是有源码Demo 先谢谢各位了

[解决办法]
Public Class Form2

Dim bPictureBoxDragging As Boolean
Dim oPointClicked As Point
Dim strDown As String
Dim strUp As String
Dim curUp As Cursor
Dim curDown As Cursor




Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub Panel1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseDown
Panel1.Cursor = Cursors.Hand
bPictureBoxDragging = True
oPointClicked = New Point(e.X, e.Y)
End Sub

Private Sub Panel1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseMove
If (bPictureBoxDragging) Then
Dim oMoveToPoint As Point
oMoveToPoint = Me.PointToClient(Panel1.PointToScreen(New Point(e.X, e.Y)))
oMoveToPoint.Offset(oPointClicked.X * -1, oPointClicked.Y * -1)
Panel1.Location = oMoveToPoint
End If
End Sub

Private Sub Panel1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Panel1.MouseUp
bPictureBoxDragging = False
Panel1.Cursor = Cursors.Default
'Panel1.Cursor = curUp
End Sub

Private Sub PictureBox1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
PictureBox1.Cursor = Cursors.Hand
'PictureBox1.Cursor = curDown
bPictureBoxDragging = True
oPointClicked = New Point(e.X, e.Y)
End Sub

Private Sub PictureBox1_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
If (bPictureBoxDragging) Then
Dim oMoveToPoint As Point
oMoveToPoint = Me.PointToClient(PictureBox1.PointToScreen(New Point(e.X, e.Y)))
oMoveToPoint.Offset(oPointClicked.X * -1, oPointClicked.Y * -1)
PictureBox1.Location = oMoveToPoint
End If
End Sub

Private Sub PictureBox1_MouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
bPictureBoxDragging = False
PictureBox1.Cursor = Cursors.Default
'PictureBox1.Cursor = curUp
End Sub
End Class

热点排行