void ShellExe()
{
shellexecuteinfo shexecinfo = {0};
shexecinfo.cbsize = sizeof(shellexecuteinfo);
shexecinfo.fmask = see_mask_nocloseprocess;
shexecinfo.hwnd = null;
shexecinfo.lpverb = null;
shexecinfo.lpfile = "c:timer.exe ";
shexecinfo.lpparameters = " ";
shexecinfo.lpdirectory = null;
shexecinfo.nshow = sw_show;
shexecinfo.hinstapp = null;
shellexecuteex(&shexecinfo);
sleep(3000); //延时3秒后关闭刚才打开的程序
terminateprocess(shexecinfo.hprocess,infinite);
}
ShellExecute()
函数原型:
HINSTANCE ShellExecute( HWND hwnd, LPCTSTR lpOperation, LPCTSTR lpFile,
LPCTSTR lpParameters, LPCTSTR lpDirectory, INT nShowCmd );
参数说明:
hwnd 窗口的句柄
lpOperation 进行的操作,如 "open ", "print ", "explore "分别对应 "打开 ", "打印 ", "游览 ", 也可以为空( " "),此时表示进行默认的操作。 lpFile 要操作的文件。
lpParameters 如果lpFile指定的是一个可执行文件则表示参数
lpDirectory 操作进行的目录
nShowCmd 程序的运行方式,其取值见上例。
如果这个函数调用成功,将返回实例的句柄,如果不成功,返回值包含错误信息,由于类型比较多,这里就不一一列举了,详见WinApi的帮助。
这样,上面的例子就可以改变为(假设窗口的句柄为Handle)
ShellExecute(NULL, "open ", "notepad ", "c:\\tst.txt ", " ",SW_SHOWNORMAL);
其实不用那么复杂,因为.TXT的关联程序就是记事本,所以只要这样就可以了
ShellExecute(NULL, " ", "c:\\tst.txt ", " ", " ",SW_SHOWNORMAL);
这个函数和WIN9X中的命令行命令 "start "类似,不光可以对文件进行操作,还可以对HTTP、MAILTO等进行操作。这样我们就可以设计有超级链接风格的程序了。
然后放置两个Label,设置其属性为:
Name lbHomepage lbEMail
Caption http://mail.126.com mailto:xxxx@126.com
并把字体改为蓝色有下划线,然后分别在两个Label的Click事件中加入以下代码:
ShellExecute(Me.hWnd, " ",lbHomepage.Caption, " ", " ",SW_SHOWNORMAL);
ShellExecute(Me.hWnd, " ",lbEMail.Caption, " ", " ",SW_SHOWNORMAL);
OK!试试看,如何?够酷的吧!
从上面可看出,ShellExecute的功能要强得多,并且可以完全替代WinExec(),事实上,WinExec()也是为了和以前版本兼容而保留,所以,我们要尽量使用ShellExecute()。其实在WinAPI
中,还有一个函数ShellExecuteEx() ,但其使用复杂,又不支持Windows NT。