VB 控件中图片移动的问题
问题是这样的。在VB中有一个控件A(控件A可是Image也可以是Picture也可以是别的,只要可以实现我的目的就行)一个图片B.jpg(大于1024×768)我建立一个...
问题是这样的。在VB中
有一个控件A(控件A 可是Image 也可以是Picture 也可以是别的,只要可以实现我的目的就行)
一个图片B.jpg(大于1024×768)
我建立一个窗体,然后在窗体中添加一个控件A,在A中加载一张图片B.jpg
这样加载进去的图片在控件A中只能显示一部分
然后我需要实现鼠标直接可以拖拽图片,看到图片的其它部分
如果直接拖拽不行,那加一横一竖2个滚动条来移动图片也可以
也许我的问题没有说的很清楚
其实我只想知道,当一个图片大于他所在的控件的时候,怎么才可以图片的0.0坐标不在他所在控件的0.0坐标上
打个比方,怎么才可以把一张图片加载进Picture后,让它水平垂直居中 展开
有一个控件A(控件A 可是Image 也可以是Picture 也可以是别的,只要可以实现我的目的就行)
一个图片B.jpg(大于1024×768)
我建立一个窗体,然后在窗体中添加一个控件A,在A中加载一张图片B.jpg
这样加载进去的图片在控件A中只能显示一部分
然后我需要实现鼠标直接可以拖拽图片,看到图片的其它部分
如果直接拖拽不行,那加一横一竖2个滚动条来移动图片也可以
也许我的问题没有说的很清楚
其实我只想知道,当一个图片大于他所在的控件的时候,怎么才可以图片的0.0坐标不在他所在控件的0.0坐标上
打个比方,怎么才可以把一张图片加载进Picture后,让它水平垂直居中 展开
2个回答
展开全部
Option Explicit
Private gX As Long, gY As Long
Private Sub Form_Load()
Image1 = LoadPicture("d:\002.jpg") '这个路径可能要改
'FitForm Image1
End Sub
Private Sub Form_Resize()
Call CenterImage(Image1)
End Sub
Private Sub Image1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
gX = X
gY = Y
End Sub
Private Sub Image1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Not Button = vbLeftButton Then Exit Sub
Dim dx As Long, dy As Long, ax As Long, ay As Long, t As Long, l As Long, tt As Long, ll As Long
With Image1
dy = Y - gY
dx = X - gX
ll = .Left
tt = .Top
l = Abs(ll)
t = Abs(tt)
ax = (.Width - l - ScaleWidth)
ay = (.Height - t - ScaleHeight)
If ll > 0 Then
dx = 0
Else
If dx < 0 Then
If Abs(dx) > ax Then dx = -ax
Else
If dx > l Then dx = l
End If
End If
If tt > 0 Then
dy = 0
Else
If dy < 0 Then
If Abs(dy) > ay Then dy = -ay
Else
If dy > t Then dy = t
End If
End If
.Move ll + dx, tt + dy
End With
End Sub
Private Sub CenterImage(dest As Image)
dest.Move (ScaleWidth - dest.Width) / 2, (ScaleHeight - dest.Height) / 2
End Sub
Private Sub FitForm(dest As Image)
Width = dest.Width + Width - ScaleWidth
Height = dest.Height + Height - ScaleHeight
Move (Screen.Width - Width) / 2, (Screen.Height - Height) / 2
End Sub
不是我编的...找到的...
Private gX As Long, gY As Long
Private Sub Form_Load()
Image1 = LoadPicture("d:\002.jpg") '这个路径可能要改
'FitForm Image1
End Sub
Private Sub Form_Resize()
Call CenterImage(Image1)
End Sub
Private Sub Image1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
gX = X
gY = Y
End Sub
Private Sub Image1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Not Button = vbLeftButton Then Exit Sub
Dim dx As Long, dy As Long, ax As Long, ay As Long, t As Long, l As Long, tt As Long, ll As Long
With Image1
dy = Y - gY
dx = X - gX
ll = .Left
tt = .Top
l = Abs(ll)
t = Abs(tt)
ax = (.Width - l - ScaleWidth)
ay = (.Height - t - ScaleHeight)
If ll > 0 Then
dx = 0
Else
If dx < 0 Then
If Abs(dx) > ax Then dx = -ax
Else
If dx > l Then dx = l
End If
End If
If tt > 0 Then
dy = 0
Else
If dy < 0 Then
If Abs(dy) > ay Then dy = -ay
Else
If dy > t Then dy = t
End If
End If
.Move ll + dx, tt + dy
End With
End Sub
Private Sub CenterImage(dest As Image)
dest.Move (ScaleWidth - dest.Width) / 2, (ScaleHeight - dest.Height) / 2
End Sub
Private Sub FitForm(dest As Image)
Width = dest.Width + Width - ScaleWidth
Height = dest.Height + Height - ScaleHeight
Move (Screen.Width - Width) / 2, (Screen.Height - Height) / 2
End Sub
不是我编的...找到的...
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询