Public Type typMousePos
x As Single
y As Single
End Type
form1:picview在picbox里
Option Explicit
Private mP As typMousePos
Private Sub Form_Activate()
picView.BackColor = picBox.BackColor
If (picView.Width > picBox.Width) Or (picView.Height > picBox.Height) Then
picView.MousePointer = 5
Else
picView.MousePointer = 0
End If
End Sub
Private Sub Form_Deactivate()
Unload Me
End Sub
Private Sub Form_Load()
picView.Picture = LoadPicture("f:\picture\1.jpg")
picView.Left = 0
picView.Top = 0
picView.AutoSize = True
picView.Appearance = 0
End Sub
Private Sub picView_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
If Button = 1 Then
mP.x = x
mP.y = y
End If
End Sub
Private Sub picView_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
Dim tmpX As Single, tmpY As Single
If Button = 1 Then
tmpX = picView.Left + x - mP.x
tmpY = picView.Top + y - mP.y
If tmpX > 0 Then tmpX = 0
If tmpX + picView.Width < picBox.Width Then tmpX = picBox.Width - picView.Width
If tmpY > 0 Then tmpY = 0
If tmpY + picView.Height < picBox.Height Then tmpY = picBox.Height - picView.Height
picView.Left = tmpX
picView.Top = tmpY
End If
End Sub
这是别人的代码,很不错
StretchBlt 其定义如下:
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
'以下在.Bas
Option Explicit
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
Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
Const SRCCOPY = &HCC0020
Public Sub DrawBitMap(Dst As Object, ByVal xRate As Double, _
ByVal yRate As Double, ByVal FileName As String)
Dim dstWidth As Long, dstHeight As Long
Dim srcWidth As Long, srcHeight As Long
Dim x As Long, y As Long
Dim pic As StdPicture
Dim hDc5 As Long, i As Long
Set pic = LoadPicture(FileName) '读取图形档
hDc5 = CreateCompatibleDC(0) '建立Memory DC
i = SelectObject(hDc5, pic.Handle) '在该memoryDC上放上bitmap图
dstHeight = CLng(srcHeight * yRate)
If dstHeight < 0 Then
y = -1 * dstHeight
Else
y = 0
End If
dstWidth = CLng(srcWidth * xRate)
If dstWidth < 0 Then
x = -1 * dstWidth
Else
x = 0
End If
Call StretchBlt(Dst.hdc, x, y, dstWidth, dstHeight, hDc5, 0, 0, srcWidth, srcHeight, SRCCOPY)
Call DeleteDC(hDc5)
End Sub
Public Sub DrawPicture(Dst As Object, ByVal xRate As Double, _
ByVal yRate As Double, ByVal FileName As String)
Dim dstWidth As Long, dstHeight As Long
Dim srcWidth As Long, srcHeight As Long
Dim x As Long, y As Long
Dim pic As StdPicture
Dim i As Long
dstHeight = CLng(srcHeight * yRate)
If dstHeight < 0 Then
y = -1 * dstHeight
Else
y = 0
End If
dstWidth = CLng(srcWidth * xRate)
If dstWidth < 0 Then
x = -1 * dstWidth
Else
x = 0
End If
Dst.ScaleMode = 3
Dst.PaintPicture pic, x, y, dstWidth, dstHeight, 0, 0, srcWidth, srcHeight
End Sub
'以下在Form需两个command button一个PictureBox
Private Sub Command1_Click()
Call DrawBitMap(Me, 1.5, -1.5, "c:\windows\circles.bmp") '放大1.5倍并上下翻转
End Sub
Private Sub Command2_Click()
Call DrawBitMap(Picture1, 1.5, -1.5, "c:\windows\client.ico") '放大1.5倍并上下翻转
End Sub
Private Sub picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 1 Then
picture1.Drag vbBeginDrag
End If
End Sub
Private Sub picture2_DragDrop(Source As Control, X As Single, Y As Single)
'注意把Source.FileName,即picture1的图片的文件名 传递进来
picture2.Picture = LoadPicture(Source.FileName)
picture2.PaintPicture picture2.Picture, 0, 0, picture2.ScaleWidth, picture2.ScaleHeight
End Sub
Private Sub picture2_DragOver(Source As Control, X As Single, Y As Single, State As Integer)
Select Case State
Case vbEnter
' 装载图标。
Source.DragIcon = ?'这里装入拖放时显示的图标
Case vbLeave
Source.DragIcon = LoadPicture() ' 卸载图标.
End Select