VBGood网站全文搜索 Google

搜索VBGood全站网页(全文搜索)
首页 - 经验之谈 - 像ACDSee一s样用滑鼠拖曳大于表单的图片
发表评论(0)作者:, 平台:, 阅读:11557, 日期:2000-03-12
像ACDSee一樣用滑鼠拖曳大於表單的圖片

本站之前也提供了一個類似的程式,但是那個程式限制當圖片比表單小時,必須將圖片置於表單左上角,否則程式執行會有問題。以下提供的程式去除了這個限制,同時當圖片比表單小時會將圖片置於表單中央,這樣看起來比較美觀。


Option Explicit

Private gX As Long, gY As Long


Private Sub Form_Load()

Image1 = LoadPicture("c:\winnt\winnt256.bmp") '這個路徑可能要改

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


使用以上程式必須注意:每次載入一個新的圖檔之後,其在表單上的位置不可亂放,最好呼叫 FitForm 副程式,否則程式執行可能會有問題。其實這個程式不限制只能用於 Image 控制項,其他任何支援 Move 方法的控制項都可利用此程式來移動,移動的效果是:當控制項大於表單時,控制項可移動的距離即等於其本身的長或寬;當控制項小於表單時,控制項無法移動,同時控制項會被置於表單中央。