注册 登录
编程论坛 VB6论坛

vb picturebox 放在frame控件中 如何获取picturebox的坐标 X

wolf死神 发布于 2013-05-12 10:29, 3350 次点击
就是吧picturebox 放在frame 里面, mousedown 不能获取X Y的值,求大神指教
5 回复
#2
bczgvip2013-05-12 22:20
谁说不行的。
#3
zhengang10262013-05-13 08:25
问题是你想获得什么座标?如果是图片框在Frame1中的座标,它的座标就应在Frame1事件里获得,如:
Private Sub Frame1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
 If Button > 0 Then
      X1 = X: Y1 = Y
 endif
End Sub
如果是图片框内的鼠标座标,就应在pictureBox事件中获得,如:
Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
 If Button > 0 Then
      X1 = X: Y1 = Y
 End If
End Sub
#4
wolf死神2013-05-16 21:26
楼上, 我我在frame1 控件中加了一个picturebox控件, 我目的是可以再frame1控件中可以随意拖动picturebox,不改变picturebox的height 和width 只是改变picturebox在frame1中的左右位置, 如何实现拖动?
#5
lowxiong2013-05-16 23:37
新建一工程,窗口里放一个frame和一个picturebox,系统默认名称,将picture1.enabled=false,拷贝下列代码,会发现可以方便移动picture1(如果设置picture1.enabled=true,直接使用picture1的mousemove事件移动,则图片框会颤抖,移动效果不理想)。
'**************************************拷贝下列代码,直接运行即可**********************************************
Dim oldX As Single, oldY As Single
Private Sub Frame1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  If Button = 1 And X > Picture1.Left And X < Picture1.Left + Picture1.Width And Y > Picture1.Top And Y < Picture1.Top + Picture1.Height Then
    '如果鼠标左键按下并且鼠标处于picture1坐标范围则记录初始坐标
    oldX = X
    oldY = Y
  Else
    oldX = -1
    oldY = -9999
  End If
End Sub

Private Sub Frame1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  If oldX = -1 And oldY = -9999 Then Exit Sub
  If Button = 1 Then
    Picture1.Left = Picture1.Left + X - oldX
    Picture1.Top = Picture1.Top + Y - oldY
    oldX = X
    oldY = Y
  End If
End Sub
#6
lowxiong2013-05-17 07:37
换了一种方式,发现并不抖动,代码简单很多,不需要设置picture1.enbled=false。
'**************************************拷贝下列代码,直接运行即可**********************************************
Dim oldX As Integer, oldY As Integer
Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  If Button = 1 Then
    oldX = X
    oldY = Y
  End If
End Sub

Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
  If Button = 1 Then
    Picture1.Left = Picture1.Left + X - oldX
    Picture1.Top = Picture1.Top + Y - oldY
  End If
End Sub


[ 本帖最后由 lowxiong 于 2013-5-17 07:42 编辑 ]
1