以默认程序打开文档 要以与文件后缀名关联的程序打开文档,在windows 9x和windows nt下可以用shellexcute函数方便地实现。这则小技巧展示了会有多方便—你只需要一个声明和一行代码! 开始一个新项目。在form上放一个command button,然后加入以下代码: private declare function shellexecute lib "shell32.dll" alias "shellexecutea" _ (byval hwnd as long, byval lpoperation as string, _ byval lpfile as string, byval lpparameters as string, _ byval lpdirectory as string, byval nshowcmd as long) as long private const sw_normal=1 ’(这些api常量可以用vb常量代替,比如vbnormalfocus) private const sw_maximize=3 private const sw_minimize=6 private const sw_show = 5 private sub command1_click() dim lr as long dim sfile as string dim ifile as integer ’ 创建一个测试用的文本文件 sfile = app.path & "shelltst.txt" on error resume next kill sfile on error goto 0 ifile = freefile open sfile for binary access write as #ifile put #ifile, , "这是一个测试文件,演示shellexecute api函数。" close #ifile ’ 依照文件名打开这个文本。windows将会检查哪个可执行程序与.txt关联 ’ (默认一般是notepad),并运行程序打开文档 lr = shellexecute(me.hwnd, "open", sfile, "", "", vbnormalfocus) if (lr < 0) or (lr > 32) then ’ 成功 else msgbox "无法打开 ’" & sfile & "’", vbinformation end if end sub 当你点击command button,将会在项目所在目录创建一个文本文件,并用默认程序打开(一般是notepad)。 =============================================== 译者附: 本函数还可以用来连接到网页,照下面写就行了: shellexecute 0&, vbnullstring, "http://coolbasic.yeah.net", vbnullstring, vbnullstring, vbnormalfocus 或者这样写来发送email: shellexecute me.hwnd, "open", "mailto:[email protected]", vbnullstring, vbnullstring, sw_show 另外有shellexecute的替代用法,更加简单实用,不用api,一句shell搞定! 连接到网页: shell "rundll32.exe url.dll,fileprotocolhandler http://www.online.sh.cn" 打开文件: shell "rundll32.exe url.dll,fileprotocolhandler " & app.path & "shelltst.txt"
aa=shell ("d:\xmyrs\xmyrs.exe",(参数))
用api使之成为主窗体的子窗体(如果需要的话)
发布时间:2001年3月30日 00:00
以默认程序打开文档
要以与文件后缀名关联的程序打开文档,在windows 9x和windows nt下可以用shellexcute函数方便地实现。这则小技巧展示了会有多方便—你只需要一个声明和一行代码! 开始一个新项目。在form上放一个command button,然后加入以下代码: private declare function shellexecute lib "shell32.dll" alias "shellexecutea" _ (byval hwnd as long, byval lpoperation as string, _ byval lpfile as string, byval lpparameters as string, _ byval lpdirectory as string, byval nshowcmd as long) as long
private const sw_normal=1 ’(这些api常量可以用vb常量代替,比如vbnormalfocus) private const sw_maximize=3 private const sw_minimize=6 private const sw_show = 5
private sub command1_click() dim lr as long dim sfile as string dim ifile as integer
’ 创建一个测试用的文本文件 sfile = app.path & "shelltst.txt" on error resume next kill sfile on error goto 0 ifile = freefile open sfile for binary access write as #ifile put #ifile, , "这是一个测试文件,演示shellexecute api函数。" close #ifile
’ 依照文件名打开这个文本。windows将会检查哪个可执行程序与.txt关联 ’ (默认一般是notepad),并运行程序打开文档 lr = shellexecute(me.hwnd, "open", sfile, "", "", vbnormalfocus) if (lr < 0) or (lr > 32) then ’ 成功 else msgbox "无法打开 ’" & sfile & "’", vbinformation end if end sub
当你点击command button,将会在项目所在目录创建一个文本文件,并用默认程序打开(一般是notepad)。 ===============================================
译者附: 本函数还可以用来连接到网页,照下面写就行了: shellexecute 0&, vbnullstring, "http://coolbasic.yeah.net", vbnullstring, vbnullstring, vbnormalfocus
或者这样写来发送email: shellexecute me.hwnd, "open", "mailto:[email protected]", vbnullstring, vbnullstring, sw_show
另外有shellexecute的替代用法,更加简单实用,不用api,一句shell搞定!
连接到网页: shell "rundll32.exe url.dll,fileprotocolhandler http://www.online.sh.cn" 打开文件: shell "rundll32.exe url.dll,fileprotocolhandler " & app.path & "shelltst.txt"