procedure TfrmCreateAndRestore.btnOpenCalcClick(Sender: TObject); {打开计算器,同时获得窗口句柄} var StartInfo: TStartupInfo; CallBackProc: EnumThreadWndProc; ExitCode: Cardinal; begin if CalledWinHandle <> 0 then begin if GetExitCodeProcess(ProcessInfo.hProcess, ExitCode) then begin if ExitCode = 0 then begin ShowMessage('Calc have exited'); //测试用的 CalledWinHandle := 0; end; end else begin ShowMessage('Can''t retrive Calc window'); //测试用的 CalledWinHandle := 0; end; end; if CalledWinHandle = 0 then begin //创建进程,打开计算器 FillChar(StartInfo, SizeOf(StartInfo), #0); with StartInfo do begin cb := SizeOf(StartInfo); dwFlags := StartF_UseShowWindow; wShowWindow := SW_NORMAL; end; if CreateProcess(nil, PChar('Calc'), nil, nil, True, CREATE_DEFAULT_ERROR_MODE, nil, nil, StartInfo, ProcessInfo) then begin //获得窗口句柄 CallBackProc := @Enum; while CalledWinHandle = 0 do //如果不做这个While,就会要打开五六个计算器才能找到,why? EnumThreadWindows(ProcessInfo.dwThreadId, @CallBackProc, 0); end else begin ShowMessage('Create fail'); //测试用的 Exit; end; end; //显示计算器 if IsWindowVisible(CalledWinHandle) then begin SetForeGroundWindow(CalledWinHandle); ShowWindow(CalledWinHandle, SW_RESTORE); end else ShowWindow(CalledWinHandle, SW_SHOW); end;
shellexecute(application.Handle ,'open','www.csdn.net,nil,nil,sw_normal);
winexec('command.com /c copy *.* c:\',SW_Normal);
winexec('start abc.txt');
ShellExecute或ShellExecuteEx//启动文件关联程序
function executefile(const filename,params,defaultDir:string;showCmd:integer):THandle;
ExecuteFile('C:\abc\a.txt','x.abc','c:\abc\',0);
ExecuteFile('http://tingweb.yeah.net','','',0);
ExecuteFile('mailto:[email protected]','','',0);
{打开计算器,同时获得窗口句柄}
var
StartInfo: TStartupInfo;
CallBackProc: EnumThreadWndProc;
ExitCode: Cardinal;
begin
if CalledWinHandle <> 0 then
begin
if GetExitCodeProcess(ProcessInfo.hProcess, ExitCode) then
begin
if ExitCode = 0 then
begin
ShowMessage('Calc have exited'); //测试用的
CalledWinHandle := 0;
end;
end
else begin
ShowMessage('Can''t retrive Calc window'); //测试用的
CalledWinHandle := 0;
end;
end;
if CalledWinHandle = 0 then
begin
//创建进程,打开计算器
FillChar(StartInfo, SizeOf(StartInfo), #0);
with StartInfo do
begin
cb := SizeOf(StartInfo);
dwFlags := StartF_UseShowWindow;
wShowWindow := SW_NORMAL;
end;
if CreateProcess(nil, PChar('Calc'), nil, nil, True,
CREATE_DEFAULT_ERROR_MODE, nil, nil, StartInfo, ProcessInfo) then
begin
//获得窗口句柄
CallBackProc := @Enum;
while CalledWinHandle = 0 do
//如果不做这个While,就会要打开五六个计算器才能找到,why?
EnumThreadWindows(ProcessInfo.dwThreadId, @CallBackProc, 0);
end
else begin
ShowMessage('Create fail'); //测试用的
Exit;
end;
end;
//显示计算器
if IsWindowVisible(CalledWinHandle) then
begin
SetForeGroundWindow(CalledWinHandle);
ShowWindow(CalledWinHandle, SW_RESTORE);
end
else
ShowWindow(CalledWinHandle, SW_SHOW);
end;