VB编写程序:设计一个程序,当单击窗体时,在窗体上显示任意一幅图片。怎样操作?
首先添加一个窗体,在这个窗体上加一个文本框的控件,在这个文本框的单击事件中写如下的程序:
Dim num, sum, it(1), n, t, h
Private Sub Form_Click()
File1.Refresh
sum = 0
List1.Clear
For i = 0 To File1.ListCount - 1
t = Split(File1.List(i), ".")(UBound(Split(File1.List(i), ".")))
If LCase(t) = LCase("jpg") Or LCase(t) = LCase("bmp") Or LCase(t) = LCase("gif") Then List1.AddItem File1.List(i): sum = sum + 1
Next
h = Rnds(sum)
Print h
Image1.Picture = LoadPicture(List1.List(h))
End Sub
Private Sub Form_Load()
File1.Visible = 0
List1.Visible = 0
num = -1
End Sub
Function Rnds(ByVal Value)
re:
DoEvents
Randomize
n = Fix(Rnd * Value)
If num = 1 Then num = -1
num = num + 1
it(num) = n
If it(0) = it(1) Then GoTo re
Rnds = n
End Function
'添加image1、file1、list1控件各一个