关于VB中Shell及ShellExecute的总结

2018年7月1日10:27:48 发表评论 1,254 views

shell 函数只可以执行.exe .com .bat 的可执行文件。

ShellExecute 是个api函数,可以执行与Windows系统相关联的文件。

ShellExecute声明: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

比如:

shell "c:\1.doc" 应该报错的,因为shell只能执行.exe .com .bat

正确的应为:ShellExecute Me.hwnd, "open", "C:\1.doc", "", "", 0

打开某个网址:ShellExecute Me.hwnd, "open", http://www.sina.com, "", "", 5

打开Excel:ShellExecute hwnd, "open", "C:\1.xls", vbNull, vbNull, SW_SHOWNORMAL

打开文件夹

shell "explorer 文件夹"1

运用命令指示符:shell "cmd /c start D:\music"  '打开d盘下music的文件夹

打开网址:

Shell "C:\Program Files\TheWorld 2.0\TheWorld.exe http://www.baidu.com", 1 '用别的浏览器打开网址

Shell "explorer.exe http://www.baidu.com", 1 '用IE浏览器打开网址

调用系统计算器Shell "calc.exe", 3

调用记事本Shell "NotePad ", vbNormalFocus

(打开某个记事本:Shell "NOTEPAD.EXE " & 文档路径, vbNormalFocus

打开任务管理器:Shell "c:\windows\system32\taskmgr.exe", 1

打开资源管理器:Shell "c:\windows\explorer.exe " & path, 1

 

注销用户    Shell "shutdown -l"      

关闭系统    Shell "shutdown -S -t 0"

重启        Shell "shutdown -R -t 0"

0是代表响应的时间 

 

结束explorer.exe进程

Shell "taskkill /f /im explorer.exe ", vbHide

(结束其它进程也是这样,如:shell "taskkill /f /im ttplayer.exe")

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: