用paintpicture的方法加载图片后,为什么就没办法用下面的方法对图片进行拖动呢?
Private Sub Picture1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
gX = x
gY = y
End SubPrivate Sub Picture1_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 Picture1
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 Picture1_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
gX = x
gY = y
End SubPrivate Sub Picture1_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 Picture1
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那该怎么办呢?
Public Declare Function StretchBlt Lib "gdi32" (ByVal hDC As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal nSrcWidth As Long, ByVal nSrcHeight As Long, ByVal dwRop As Long) As Long
Public Declare Function GetObject Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long
Public Declare Function SelectObject Lib "gdi32" (ByVal hDC As Long, ByVal hObject As Long) As Long
Public Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hDC As Long) As Long
Public Declare Function CreateCompatibleBitmap Lib "gdi32" (ByVal hDC As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
Public Declare Function DeleteDC Lib "gdi32" (ByVal hDC As Long) As Long
Public Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Public Type BITMAP '14 bytes
bmType As Long
bmWidth As Long
bmHeight As Long
bmWidthBytes As Long
bmPlanes As Integer
bmBitsPixel As Integer
bmBits As Long
End Type'程序中:
Dim xnow As Long
Dim ynow As Long
Dim xtemp As Long
Dim ytemp As Long
Dim xchange As Long
Dim ychange As Long
Dim w As Long
Dim h As Long
Dim hBitmap As Long
Dim hmemDC As Long
Private Sub Form_Load()
Picture1.AutoRedraw = True
Me.ScaleMode = vbPixels
Picture1.ScaleMode = vbPixels
Dim bm As BITMAP
Dim pic As Picture
Set pic = LoadPicture("c:\abc.bmp")
GetObject pic.Handle, LenB(bm), bm
w = bm.bmWidth
h = bm.bmHeight
hmemDC = CreateCompatibleDC(Picture1.hDC)
SelectObject hmemDC, pic.Handle
StretchBlt Picture1.hDC, 0, 0, Picture1.Width, Picture1.Height, hmemDC, 0, 0, Picture1.Width, Picture1.Height, vbSrcCopy
End SubPrivate Sub Picture1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 Then
xnow = X
ynow = Y
xchange = 0
ychange = 0
Picture1.AutoRedraw = False
End If
End SubPrivate Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 Then
xchange = xtemp + xnow - X
ychange = ytemp + ynow - Y
If xchange > w - Picture1.Width Then xchange = w - Picture1.Width
If ychange > h - Picture1.Height Then ychange = h - Picture1.Height
If xchange < 0 Then xchange = 0
If ychange < 0 Then ychange = 0
If xchange > w Or ychange > h Or xchange < 0 Or ychange < 0 Then Exit Sub
StretchBlt Picture1.hDC, 0, 0, Picture1.Width, Picture1.Height, hmemDC, xchange, ychange, Picture1.Width, Picture1.Height, vbSrcCopy
End If
End Sub
Private Sub Picture1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
Picture1.AutoRedraw = True
xtemp = xchange
ytemp = ychange
End Sub'上面的代码里使用StretchBlt而不用bitblt,是因为考虑到你说还要进行缩放等操作
'不过建议楼主还是先搞清楚基本的图形语句和api再想其他事吧
是调试时候加上去的,没用,去掉。
如果我想让图片居中并根据所要缩放的倍数fold进行改变,
StretchBlt Picture1.hDC, Picture1.Width / 2 - pic.Width * fold / 2, Picture1.Height / 2 - pic.Height * fold / 2, pic.Width * fold, pic.Height * fold, hmemDC, 0, 0, Picture1.Width, Picture1.Height, vbSrcCopy
这样画不居中,而且会先出现个很大的马赛克,鼠标点一下大小又恢复没缩放的大小