怎么使用ShellExecute(100分)

  • 主题发起人 主题发起人 birch2002
  • 开始时间 开始时间
B

birch2002

Unregistered / Unconfirmed
GUEST, unregistred user!
要不要什么申明啊,我以前是用VC的,不用的,对谢指教啊
 
在uses中增加shellApi
 
<br><br>Q: 如何打开一个应用程序? ShellExecute(this-&gt;m_hWnd,"open","calc.exe","","", SW_SHOW );<br>或 ShellExecute(this-&gt;m_hWnd,"open","notepad.exe",<br>&nbsp; &nbsp; "c://MyLog.log","",SW_SHOW );<br>正如您所看到的,我并没有传递程序的完整路径。<br>Q: 如何打开一个同系统程序相关连的文档? ShellExecute(this-&gt;m_hWnd,"open",<br>&nbsp; &nbsp; "c://abc.txt","","",SW_SHOW );<br>Q: 如何打开一个网页? ShellExecute(this-&gt;m_hWnd,"open",<br>&nbsp; &nbsp; "http://www.google.com","","", SW_SHOW );<br>Q: 如何激活相关程序,发送EMAIL? ShellExecute(this-&gt;m_hWnd,"open",<br>&nbsp; &nbsp; "mailto:nishinapp@yahoo.com","","", SW_SHOW );<br>Q: 如何用系统打印机打印文档? ShellExecute(this-&gt;m_hWnd,"print",<br>&nbsp; &nbsp; "c://abc.txt","","", SW_HIDE);<br>Q: 如何用系统查找功能来查找指定文件? ShellExecute(m_hWnd,"find","d://nish",<br>&nbsp; &nbsp; NULL,NULL,SW_SHOW);<br>Q: 如何启动一个程序,直到它运行结束? SHELLEXECUTEINFO ShExecInfo = {0};<br>ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);<br>ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;<br>ShExecInfo.hwnd = NULL;<br>ShExecInfo.lpVerb = NULL;<br>ShExecInfo.lpFile = "c://MyProgram.exe"; <br>ShExecInfo.lpParameters = ""; <br>ShExecInfo.lpDirectory = NULL;<br>ShExecInfo.nShow = SW_SHOW;<br>ShExecInfo.hInstApp = NULL; <br>ShellExecuteEx(&amp;ShExecInfo);<br>WaitForSingleObject(ShExecInfo.hProcess,INFINITE);<br>或: PROCESS_INFORMATION ProcessInfo; <br>STARTUPINFO StartupInfo; //This is an [in] parameter<br>ZeroMemory(&amp;StartupInfo, sizeof(StartupInfo));<br>StartupInfo.cb = sizeof StartupInfo ; //Only compulsory field<br>if(CreateProcess("c://winnt//notepad.exe", NULL, <br>&nbsp; &nbsp; NULL,NULL,FALSE,0,NULL,<br>&nbsp; &nbsp; NULL,&amp;StartupInfo,&amp;ProcessInfo))<br>{ <br>&nbsp; &nbsp; WaitForSingleObject(ProcessInfo.hProcess,INFINITE);<br>&nbsp; &nbsp; CloseHandle(ProcessInfo.hThread);<br>&nbsp; &nbsp; CloseHandle(ProcessInfo.hProcess);<br>} &nbsp;<br>else<br>{<br>&nbsp; &nbsp; MessageBox("The process could not be started...");<br>}<br><br>Q: 如何显示文件或文件夹的属性? SHELLEXECUTEINFO ShExecInfo ={0};<br>ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);<br>ShExecInfo.fMask = SEE_MASK_INVOKEIDLIST ;<br>ShExecInfo.hwnd = NULL;<br>ShExecInfo.lpVerb = "properties";<br>ShExecInfo.lpFile = "c://"; //can be a file as well<br>ShExecInfo.lpParameters = ""; <br>ShExecInfo.lpDirectory = NULL;<br>ShExecInfo.nShow = SW_SHOW;<br>ShExecInfo.hInstApp = NULL; <br>ShellExecuteEx(&amp;ShExecInfo);<br><br>&nbsp;<br>&nbsp;
 
我这写成了一个函数<br>uses shellapi<br>function ExecuteFile(const FileName, Params, DefaultDir: String;<br>ShowCmd: Integer): THandle;<br>var<br>&nbsp; zFileName, zParams, zDir: array[0..79] of Char;<br>begin<br>&nbsp; Result := ShellExecute(Application.MainForm.Handle, nil,<br>&nbsp; StrPCopy(zFileName, FileName), StrPCopy(zParams, Params),<br>&nbsp; StrPCopy(zDir, DefaultDir), ShowCmd);<br>end;
 
只需要在uses里加上shellapi,其余和VC一样(当然,字符串要改成单引号:)
 
后退
顶部