给VB控件PictureBox添加滚动条
为了解决上述问题,我们可以在图片框(PictureBox)内部加上水平和垂直滚动条,利用滚动条来显示看不到的图片。具体方法如下:
首先给工程(Project)添加一个OCX控件,单击“工程” 菜单项,在弹出的下拉菜单中点击“组件”,选中其中的“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中,利用水平和垂直滚动条就可以实现图片的滚动。