2024年5月11日发(作者:)

二是利用VB的picturebox和滚动条控件编程实现

PICTUREBOX控件能支持许多种图像格式,但没有缩放功能并且不自动添加滚

动条,所以必须自行编程实现。

应用程序使用两个图片框,一个作为包容器,可称之为父图片框,另一个作

为子图片框显示图形,配合滚动条控件在父图片框内移动子图片框就可实现画面

的滚动。

VB中提供了paintpicture方法,可以在窗体、图片框上的任何地方绘制图

形,语法为 icture picture, x1, y1, width1, height1, x2,

y2, width2, height2, opcode,其中x1,y1为指定在 object 上绘制 picture

的目标坐标;width1,height1 指定 picture 的目标宽度和高度;x2,y2指定

picture 内剪贴区的坐标 width2,height2 指定 picture 内剪贴区的源宽度和高

度,当我们重新设定width1,height1时,就可以在子图片框内实现平滑的缩放图

片。虽然Paintpicture可以代替bitblt windows API函数,但没有API的执行

速度快,如程序对速度的要求比较高,可用API函数代替paintpicture方法 。

在窗体中先画出picture1作为父图片框,然后在其中中画出picture2,在窗

体中再画出滚动条,picture2的autosize属性为TRUE,borderstyle属性为0,

autoredraw属性为true,滚动条的largechange和smallchange属性设为合适大

小,窗体既设置完毕。

以下为可以直接使用的完整程序,可以用光标键移动画面,也可以拖放方式

移动画面;用加减号缩放画面。

Public c1, c2, c3, c4 As Integer

Public tf, d1, d2, d3, d4, blf As Integer

Public yk, yg As Long

Private Sub Form_Load()

c1 =

c2 =

blf = 100

e = LoadPicture("a:")

yk =

yg =

End Sub

Private Sub Form_Resize()

If Width > 1500 And Height > 1170 Then

c3 = - c1

c4 = - c2

, , + c3, + c4

c1 =

c2 =

Call p

End If

us

End Sub

'以拖放方式滚动画面

Private Sub Picture2_DragDrop(Source As Control, x As Single, y As Single)

If > Then '通过计算鼠标移动位置,调用vscroll_change事件

cccc = + (d2 - y) '移动画面

If cccc <= 0 Then

cccc = 0

End If

If cccc >= Then

cccc =

End If

= cccc

End If

If > Then

cccc1 = + (d1 - x)

If cccc1 <= 0 Then

cccc1 = 0

End If

If cccc1 >= Then

cccc1 =

End If

= cccc1

End If

End Sub

'设置功能键,光标键移动画面,加减号缩放画面

Private Sub Picture2_KeyDown(KeyCode As Integer, Shift As Integer)

Select Case KeyCode

Case vbKeyLeft

= IIf( - hange < 0, 0, -

hange)

Case vbKeyRight

If > Then

= IIf( + hange > , ,

+ hange)

End If

Case vbKeyUp

= IIf( - hange < 0, 0, -

hange)

Case vbKeyDown

If > Then

= IIf( + hange > , ,

+ hange)

End If

Case vbKeyAdd

If blf < 150 Then