首先給工程(Project)添加一個OCX控件,單擊選單上的工程(Project)選單項,在彈出的下拉選單中點擊組件(Components),選中其中的"Microsoft Common Dialog Control 5.0",確定完成加載工作;然后畫一個PictureBox,采用VB提供的默認名字Picture1, 再在Picture1上面畫一個PictureBox,默認名字為Picture2,注意別忘了設置:Picture2.AutoSize=TRUE;接著,加上水平和垂直滾動條,默認名字分別為:HScroll1,VScroll1;以后加載圖形到Picture2上,就可以了;最后,在窗體中引入其它控件:一個按鈕(Command),默認名為Command1和一個"Microsoft Common Dialog Control",默認名為CommonDialog1。具體VB代碼如下:
Private Sub Form-Load()
Picture2.Left = 0
Picture2.Top = 0
Picture2.Width = Picture1.Width
Picture2.Height = Picture1.Height
VScroll1.Min = 0
HScroll1.Min = 0
HScroll1.Min = 0
VScroll1.Max = Picture2.Height - Picture1.Height
HScroll1.Max = Picture2.Width - Picture1.Width
If HScroll1.Max 〈 0 Then HScroll1.Enabled = False
If VScroll1.Max 〈 0 Then VScroll1.Enabled = False
End Sub
Private Sub Command-Click()
On Error GoTo ErrExit
CommonDialog1.Filter =
"Bitmap file(*.bmp)|*.bmp|All File(*.*)|*.*"
CommonDialog1.FilterIndex = 1
CommonDialog1.ShowOpen
Picture2.Picture = LoadPicture(CommonDialog1.filename)
VScroll1.Min = 0
HScroll1.Min = 0
VScroll1.Max = Picture2.Height - Picture1.Height
HScroll1.Max = Picture2.Width - Picture1.Width
If HScroll1.Max 〈 0 Then HScroll1.Enabled = False
If VScroll1.Max 〈 0 Then VScroll1.Enabled = False
ErrExit:
End Sub
Private Sub HScroll1-Change()
Picture2.Left = -HScroll1.Value
End Sub
Private Sub VScroll1-Change()
Picture2.Top = -VScroll1.Value
End Sub
該程序通過點擊Command1按鈕,在彈出的對話框中選擇一個圖形文件加載到Picture2中,利用水平和垂直滾動條就可以實現圖片的滾動。
延伸閱讀
文章來源于領測軟件測試網 http://www.kjueaiud.com/