ShellExecute函数:
在ShellAPI单元中,ShellExecute函数的定义为:
function ShellExecute(hWnd: HWND; Operation, FileName, Parameters,
Directory: PChar; ShowCmd: Integer): HINST; stdcall;
作用:打开或打印一个指定的文件;
参数说明:
hWnd hwnd, //指向父窗口的句柄;
Operation: PChar, //指向一个null结尾的串以指明要执行的操作;
可以是"open","print","explore",NULL;
FileName: PChar, //指向文件名或文件夹名串;
Parameters: PChar, //指向一个null结尾的串以指明可执行文件的参数;
如果FileName参数为文档,此参数应为NULL;
Directory: PChar, //指向一个null结尾的串以指明默认目录;
ShowCmd: Integer //文件在打开时是否显示;
如果FileName参数为文档,此参数应为0;
ShellExecute的功能是运行一个外部程序(或者是打开一个已注册的文件、打开一个目录、打印一个文件等等),并对外部程序有一定的控制。
有几个API函数都可以实现这些功能,但是在大多数情况下ShellExecute是更多的被使用的,同时它并不是太复杂。下面举例说明它的用法。
1、打开一个应用程序
ShellExecute(Handle, ''open'', PChar(''D:/application.exe''), nil, nil, SW_SHOW);
2、打开记事本,同时打开一个txt文件
(注:系统能自动记事本应用程序的路径,不必使用绝对路径)
ShellExecute(Handle, ''open'', PChar(''notepad''), PChar(''D:/readme.txt''), nil, SW_SHOW);
3、打印一个文档
ShellExecute(Handle, ''print'', PChar(''D:/MyDoc.doc''), nil, nil, SW_SHOW);
注意:可能会看到word暂时被打开,但它会自行关闭。
4、打开一个HTML页面
ShellExecute(Handle, ''open'', PChar(''http://www.youkuaiyun.com''), nil, nil, SW_SHOW);
5、通过一个已经注册的文件类型来打开应用程序
ShellExecute(Handle, ''open'', PChar(''D:/readme.txt''), nil, nil, SW_SHOW);
6、用windows Explorer 打开一个目录
ShellExecute(Handle, ''explore'', PChar(''c:/windows)'', nil, nil, SW_SHOW);
7、运行一个DOS命令并立即返回
ShellExecute(Handle, ''open'', PChar(''command.com''), PChar(''/c copy test1.txt test2.txt''), nil, SW_SHOW);
8、运行一个DOS命令并保持DOS窗口存在
ShellExecute(Handle, ''open'', PChar(''command.com''), PChar(''/k dir''), nil, SW_SHOW);
9、激活相关程序,发送EMAIL
ShellExecute(this->m_hWnd,"open",
"yuhen1118@126.com","","", SW_SHOW );
10、用系统查找功能来查找指定文件
ShellExecute(m_hWnd,"find","d://nish", NULL,NULL,SW_SHOW);
11、启动一个程序,直到它运行结束
SHELLEXECUTEINFO ShExecInfo = {0};
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
ShExecInfo.hwnd = NULL;
ShExecInfo.lpVerb = NULL;
ShExecInfo.lpFile = "c://MyProgram.exe";
ShExecInfo.lpParameters = "";
ShExecInfo.lpDirectory = NULL;
ShExecInfo.nShow = SW_SHOW;
ShExecInfo.hInstApp = NULL;
ShellExecuteEx(&ShExecInfo);
WaitForSingleObject(ShExecInfo.hProcess,INFINITE);
或:
PROCESS_INFORMATION ProcessInfo;
STARTUPINFO StartupInfo; //This is an [in] parameter
ZeroMemory(&StartupInfo, sizeof(StartupInfo));
StartupInfo.cb = sizeof StartupInfo ; //Only compulsory field
if(CreateProcess("c://winnt//notepad.exe", NULL,
NULL,NULL,FALSE,0,NULL,
NULL,&StartupInfo,&ProcessInfo))
{
WaitForSingleObject(ProcessInfo.hProcess,INFINITE);
CloseHandle(ProcessInfo.hThread);
CloseHandle(ProcessInfo.hProcess);
}
else
{
MessageBox("The process could not be started...");
}
12、显示文件或文件夹的属性
SHELLEXECUTEINFO ShExecInfo ={0};
ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
ShExecInfo.fMask = SEE_MASK_INVOKEIDLIST ;
ShExecInfo.hwnd = NULL;
ShExecInfo.lpVerb = "properties";
ShExecInfo.lpFile = "c://"; //can be a file as well
ShExecInfo.lpParameters = "";
ShExecInfo.lpDirectory = NULL;
ShExecInfo.nShow = SW_SHOW;
ShExecInfo.hInstApp = NULL;
ShellExecuteEx(&ShExecInfo);