在程序设计中,经常遇到这种情况:计算机在处理大量数据时,用户等待处理结果,而此时的用户界面往往显得单调乏味。如果有一个能动态显示操作过程的进度条就好了,它既使用户界面具有动感,又增加了程序的趣味性。 笔者用VB 5.0编制应用系统时,分别用VB 5.0的进度条控件和滑动片控件,以及API函数BitBlt()三种方法实现操作过程动态显示。实现效果如图所示。现用两个应用实例说明如下。
★应用实例一:用VB 5.0的进度条控件和滑动片控件分别实现操作过程动态显示。
(1)插入进度条控件和滑动片控件的方法:在VB 5.0主菜单的“工程"项中,选择“部件(0)",在弹出的控件栏中,双击“Microsoft Windows Common Control 5.0"项,按“确定"键返回。那么在窗体工具箱中就有了上述控件。
(2)建立含有如下控件的窗体:
控件 NAME CAPTION INTERVAL MAX FONT FORECOLOR
窗体 FORM1 用VB 5.0实现操作过程动态显示
进度条 PROGRESSBAR1 100
滑动片 SLIDER1 100
计时器 TIMER1 50
标签 LABEL1 宋体(小四)&H00FF0000&(蓝色)
标签 LABEL2 宋体(小四)&H00FF0000&(蓝色)
命令按钮 COMMAND1 退出
(3)输入计时器的程序代码:
Private Sub Timer1_Timer()
Static count As Integer
count = count + 1
Label1.Caption = Str$(count) +“%" '进度条显示百分比
Label2.Caption = Str$(count) + “%" '滑动片显示百分比
If count = 100 Then
count = 0
Else
ProgressBar1.Value = count
Slider1.Value = count
End If
End Sub
(4)输入退出按钮的程序代码:
Private Sub Command1_Click()
End
End Sub
★应用实例二:用VB 5.0的API函数BitBlt()实现操作过程动态显示。
BitBlt()函数格式:
Private Declare Function BitBlt Lib“gdi32" (ByVal hDestDC 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 dwRop As Long) As Long
该函数将点位图从由hSrcDC参数给出的源设备上拷贝到由hDestDC参数给出的目标设备上。xSrc,ySrc指出被移动的点位图在源设备上的原点坐标。X,y,nWidth,nHeight指出目标设备的原点坐标、宽度和高度。dwRop参数定义源位图和目标位图是如何结合的。参数值&HCC0020表示将源位图复制到目标位图上。
(1)建立含有如下控件的窗体:
控件 NAME CAPTION INTERVAL FORECOLOR BACKCOLOR 窗体 FORM1 用BitBlt()函数实现操作过程动态显示
图片框 PICTURE2 &H00FFFFFF&(白色)&H00FF0000&(蓝色)
图片框 PICTURE1 &H00000000&(黑色)&H00FFFFFF&(白色)
计时器 TIMER1 10
命令按钮COMMAND1 退 出
(2)定义窗体级函数和常量:
Option Explicit
Private Declare Function BitBlt Lib “gdi32" (ByVal hDestDC 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 dwRop As Long) As Long
Const srccopy = &HCC0020
(3)输入窗体装入的程序代码:
Private Sub Form_Load()
Picture2.Width = Picture1.Width
Picture2.Height = Picture1.Height
End Sub
(4)输入计时器的程序代码:
Private Sub Timer1_Timer()
Static counts,r As Integer
Static hh As Single
Static percent As String
hh! = counts / 100
percent$ = Format(hh!, “0%")
Picture1.Cls
Picture2.Cls
Picture1.CurrentX = (Picture1.Width - Picture1.TextHeight(percent$)) / 2
Picture2.CurrentX = Picture1.CurrentX
Picture1.CurrentY = (Picture1.Height - Picture1.TextHeight(percent$)) / 2
Picture2.CurrentY = Picture1.CurrentY
Picture1.Print percent$
Picture2.Print percent$
r% = BitBlt(Picture1.hDC, 0, 0, Picture2.Width * hh!, Picture2.Height, Picture2.hDC, 0, 0, srccopy)
counts = counts + 1
If counts = 100 Then
counts = 0
End If
End Sub
(5)输入退出按钮的程序代码:
Private Sub Command1_Click()
End
End Sub
上述程序在同创P/200,中文Windows 95、Windows NT 4.0,Visual Basic 5.0软硬件环境下运行通过。
- 还没有人评论,欢迎说说您的想法!