浏览文件夹中的图片(用vb实现)
首先要新建一个工程,在form1中添加dirlistbox控件,drivelistbox控件,filelistbox控件,combobox控件,textbox控件,vscrollbar控件和一个command1控件
然后在代码框中输入以下代码:
option explicit
private declare function findfirstfile lib "kernel32" alias "findfirstfilea" (byval lpfilename as string, lpfindfiledata as win32_find_data) as long
private declare function findclose lib "kernel32" (byval hfindfile as long) as long
private declare function findnextfile lib "kernel32" alias "findnextfilea" (byval hfindfile as long, lpfindfiledata as win32_find_data) as long
private const max_path = 260
private type filetime
dwlowdatetime as long
dwhighdatetime as long
end type
private type win32_find_data
dwfileattributes as long
ftcreationtime as filetime
ftlastaccesstime as filetime
ftlastwritetime as filetime
nfilesizehigh as long
nfilesizelow as long
dwreserved0 as long
dwreserved1 as long
cfilename as string * max_path
calternate as string * 14
end type
dim pic as object
private sub command1_click()
dim obj as object
dim pcname as string
dim i as long, j as long, counter as long
dim wfd as win32_find_data
dim source as string, piname as string
dim hfile as long
dim nfile as long
dim filename() as string
dim x as long, length1 as long, width1 as long
counter = 0
set pic = form1.controls.add("vb.picturebox", "test")
pic.visible = true
if right(dir1.path, 1) <> "\" then
source = dir1.path & "\" & text1.text
else
source = dir1.path & text1.text
end if
hfile = findfirstfile(source, wfd)
if hfile = -1 then
msgbox "没有找到文件"
end if
counter = counter + 1
do
nfile = findnextfile(hfile, wfd)
if nfile <> 0 then
counter = counter + 1
end if
loop until nfile = 0
redim filename(counter) as string
hfile = findfirstfile(source, wfd)
filename(0) = wfd.cfilename
for i = 1 to counter
nfile = findnextfile(hfile, wfd)
filename(i) = wfd.cfilename
next i
if right(dir1.path, 1) <> "\" then
source = dir1.path & "\"
else
source = dir1.path
end if
call findclose(hfile)
for i = 0 to (counter / 4)
for j = 0 to 3
piname = "picture" & j + 1 + i * 4
set obj = form1.controls.add("vb.image", piname)
obj.width = 1500
obj.height = 1500
obj.stretch = true
set obj.container = pic
obj.left = j * obj.width
obj.top = i * obj.height
if (j + 1 + i * 4) > counter then
goto line
end if
obj.picture = loadpicture(source & (filename(j + 1 + i * 4)))
obj.visible = true
next j
next i
line:
width1 = j * obj.width
length1 = i * obj.height
pic.width = 4 * obj.width
pic.height = length1
pic.left = dir1.left + dir1.width
pic.top = 0
width1 = 4 * obj.width
length1 = i * obj.height
vscroll1.min = 0
vscroll1.max = 32767
end sub
private sub dir1_change()
file1.filename = dir1.path
end sub
private sub drive1_change()
dir1.path = drive1.drive
end sub
private sub form_load()
combo1.text = "*.jpg"
combo1.additem "*.bmp"
combo1.additem "*.gif"
vscroll1.largechange = 200
vscroll1.smallchange = 50
text1.text = combo1.text
end sub
private sub vscroll1_change()
pic.top = 0 - vscroll1.value
end sub
该程序在vb6.0+winme环境下调试通过。
欢迎广大读者和本人讨论。我的电子邮件是:ywchen2000@etang.com