关键字:vb 预览
前一阵有人在vb专家门诊中提出一个问题,如何在vb中实现打开图像文件的预览,虽然给出了300分的高分,回答着却寥寥无几。我在参照了delphi的源代码后在vb中实现了其部分图像预览功能,在中文windows98 se下测试通过。
从msdn中可以知道调用文件打开通用对话框需调用api 函数getopenfilename,原形如下:
bool getopenfilename( lpopenfilename lpofn );// lpofn 为初始化数据结构的地址
其参数lpofn指向类型为openfilename变量的地址,windows已经为我们实现自定义的文件打开对话框留了接口。为了实现这个自定义的对话框,重点设置openfilename中的如下几个参数:
| flags | ofn_enablehook 使由lpfnhook成员指定的钩子函数有效 |
| ofn_enabletemplate 表示由lptemplatename指定一个对话框模板资源,这个资源存在于由hinstance指定的模块中 | |
| ofn_explorer 如果指定了上述两个标志则必须指定这标志 | |
| lpfnhook | 指向钩子函数的地址 |
| lptemplatename | 对话框模板资源的字符串名,而不是id |
就是说你要在你的程序中包含一个对话框模板,windows将以这个模板为基础显示通用对话框。从很多资料上都可以知道如果要在自定义的对话框中显示通用对话框,在您的对话框资源中必须包含一个static,它的id是stc32,十进制值为1119,这个static也就是显示原来通用对话框的地方。当然他对对话框也有一些要求,这里就不废话了,自己看msdn吧。
我们在一个可以制作对话框资源的环境(我用的是vc)中制作出这个对话框,并将它存为res文件。从这里开始我们进入vb ide中,把这个资源文件加入到你的vb应用程序中,假设它的名字是“dlgopentemp”。
接下来我们在vb工程中加入一个form,假设它的名字是frmpreview,将它的borderstyle 设定为none。并将一个picturebox或者image加入到窗体中,名字就叫做picture1吧,我们就将在它里边显示预览的内容。下边呢也就是最关键的一步,编写hook。
在工程中加入一个module,名字无所谓啦。假设我们的钩子叫wndproc,定义如下:
public function wndproc(byval hdlg as long, byval uimsg as long, byval wparam as long, byval lparam as long) as long
on error goto lblexit
select case uimsg
case wm_notify
copymemory nmheader, byval lparam, len(nmheader)
select case nmheader.code
case cdn_initdone
getwindowrect getdlgitem(hdlg, 1119), staticrect
mapwindowpoints 0, hdlg, staticrect, 2
setparent frmpreview.hwnd, hdlg
frmpreview.visible = true
frmpreview.move (staticrect.right - staticrect.left + 10) * screen.twipsperpixelx, 5 * screen.twipsperpixelx
frmpreview.refresh
wndproc = 0
case cdn_selchange
frmpreview.loadpic getfilesname(hdlg)
wndproc = 0
end select
case wm_destroy
frmpreview.visible = false
setparent frmpreview.hwnd, 0
unload frmpreview
case else
end select
exit function
lblexit:
end function
在frmpreview 中增加一个方法loadpic,此方法将用户选中的可识别的图像文件显示在frmpreview中的picture1中,如何显示看您自己了。在得到cdn_selchange时,调用getfilesname得到此时用户选中的文件,代码如下:
private function getfilesname(hwindow as long) as string
dim hparent as long
dim lretvalue as long
dim thefilename(1024) as byte
for i = 0 to ubound(thefilename)
thefilename(i) = 0
next
hparent = getparent(hwindow)
lretvalue = sendmessage(hparent, cdm_getfilepath, 1024, thefilename(0))
getfilesname = strconv(thefilename, vbunicode)
end function
调用getopenfilename api的方法如下:
public function showopenfiledlg(hparent as long) as long
on error goto lblexit
static strfilter as string
strfilter = "all pictures" & chr(0) & "*.bmp;*.dib;*.jpg;*.gif;*.wmf;*.emf;*.ico;*.cur" & chr(0) & _
"bitmap (*.bmp;*.dib)" & chr(0) & "*.bmp;*.dib" & chr(0) & _
"jpeg (*.jpg)" & chr(0) & "*.jpg" & chr(0) & _
"gif (*.gif)" & chr(0) & "*.gif" & chr(0) & _
"metafile (*.wmf;*.emf)" & chr(0) & "*.wmf;*.emf" & chr(0) & _
"icons (*.ico;*.cur)" & chr(0) & "*.ico;*.cur" & chr(0) & _
"all files (*.*)" & chr(0) & "*.*" & chr(0) & chr(0)
frmpreview.hide
pointer = getprocaddr(addressof wndproc)
dim myid as long
with openfile
.lstructsize = len(openfile)
.hwndowner = hparent
.hinstance = app.hinstance
.lpstrfilter = strfilter
.lpstrcustomfilter = ""
.nmaxcustfilter = 0
.nfilterindex = 0
.lpstrfile = filesname
.nmaxfile = 1023
.lpstrfiletitle = ""
.nmaxfiletitle = 0
.lpstrinitialdir = ""
.lpstrtitle = ""
.flags = ofn_enablehook + ofn_hidereadonly + ofn_enabletemplate + ofn_explorer
.nfileoffset = 0
.nfileextension = 0
.lpstrdefext = ""
.lcustdata = 0
.lpfnhook = pointer
.lptemplatename = "dlgopentemp"
end with
showopenfiledlg = getopenfilename(openfile)
lblexit:
end function
文件就放在filesname里,它必须预先分配好空间。soryy,又说废话了。
这些代码没有经过严格的测试,只是实现了简单的功能,肯定存在很多bug,如果哪位兄弟姐妹发现了或者给debug了,别忘了给我妹一份。我的email地址是wjxcn@263.net。