分类: C/C++
2010-10-11 17:38:14
Q: 如何打开一个应用程序?
ShellExecute(this->m_hWnd,"open","calc.exe","","", SW_SHOW );或 ShellExecute(this->m_hWnd,"open","notepad.exe","c:\\MyLog.log","",SW_SHOW );正如您所看到的,我并没有传递程序的完整路径。
Q: 如何打开一个同系统程序相关连的文档?ShellExecute(this->m_hWnd,"open","c:\\abc.txt","","",SW_SHOW );
Q: 如何打开一个网页?
ShellExecute(this->m_hWnd,"open","","","", SW_SHOW );
Q: 如何激活相关程序,发送EMAIL?
ShellExecute(this->m_hWnd,"open","mailto:nishinapp@yahoo.com","","", SW_SHOW );
Q: 如何用系统打印机打印文档?
ShellExecute(this->m_hWnd,"print","c:\\abc.txt","","", SW_HIDE);
Q: 如何用系统查找功能来查找指定文件?
ShellExecute(m_hWnd,"find","d:\\nish",NULL,NULL,SW_SHOW);
Q: 如何启动一个程序,直到它运行结束?
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...");
}
Q: 如何显示文件或文件夹的属性?
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);
ShellExecute的功能是运行一个外部程序(或者是打开一个已注册的文件、打开一个目录、打印一个文件等等),并对外部程序有一定的控制。 有几个API函数都可以实现这些功能,但是在大多数情况下ShellExecute是更多的被使用的,同时它并不是太复杂。下面举例说明它的用法。 开始一个新的应用程序 ShellExecute(Handle, ''open'', PChar(''c:\test\app.exe''), nil, nil, SW_SHOW); 打开记事本,并打开一个文件(系统能识别记事本应用程序的路径,因此我们不必使用绝对路径) ShellExecute(Handle, ''open'', PChar(''notepad''), PChar(''c:\test\readme.txt''), nil, SW_SHOW); 打印一个文档 ShellExecute(Handle, ''print'', PChar(''c:\test\test.doc''), nil, nil, SW_SHOW); 注意:可能你会看到word暂时的被打开,但它会自动关闭。 打开一个HTML页面 ShellExecute(Handle, ''open'', PChar(), nil, nil, SW_SHOW); 你能通过一个已经注册的文件类型来打开应用程序 ShellExecute(Handle, ''open'', PChar(''c:\test\readme.txt''), nil, nil, SW_SHOW); 用windows Explorer 打开一个目录 ShellExecute(Handle, ''explore'', PChar(''c:\windows)'', nil, nil, SW_SHOW); 运行一个DOS命令并立即返回 ShellExecute(Handle, ''open'', PChar(''command.com''), PChar(''/c copy file1.txt file2.txt''), nil, SW_SHOW); 运行一个DOS命令并保持DOS窗口存在 ShellExecute(Handle, ''open'', PChar(''command.com''), PChar(''/k dir''), nil, SW_SHOW); |