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

怎么优化自由移动Picturebox中的Image

2012-01-28 
如何优化自由移动Picturebox中的Image?我在FORM中放置了一个Picturebox,在这个Picturebox中放置了一个Imag

如何优化自由移动Picturebox中的Image?
我在FORM中放置了一个Picturebox,在这个Picturebox中放置了一个Image,我想实现:
在程序执行时,能用鼠标拖着Image自由移动Image的位置,我的代码如下:
'---------------------------------
  Dim dragx As Single
  Dim dragy As Single
  Private Sub Picture1_DragDrop(Source As Control, X As Single, Y As Single)
  Image1.Picture = Source
  Image1.Move (X - dragx), (Y - dragy)
  End Sub
  Private Sub Image1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  dragx = X
  dragy = Y
  Image1.Drag BEGIN_DRAG
  End Sub
'---------------------------------
程序能执行,但是移动Image非常不方便,有时要拖鼠标半天才移动一点,有时干脆不动,我该如何解决?

[解决办法]
'这是拖动pictureBox的例子。在picturebox中拖动image的原理相同。更好的办法是发送消息。
Option Explicit
Dim bDown As Boolean
Dim dX As Single, dY As Single
Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
bDown = True
dX = X
dY = Y
End Sub

Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If bDown = True Then Picture1.Move Picture1.Left + X - dX, Picture1.Top + Y - dY
End Sub

Private Sub Picture1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
bDown = False
End Sub

热点排行