help me!怎样在退出程序时关闭外部程序 我在程序中用winexec('c:\windows\calc.exe',sw_show)调用了windows的计算器,在退出程序时我想关闭计算器程序,请问各位高手我应该怎么做?thank you verymuch!!!!!!! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 应该是采用API来关闭它的进程吧!具体用哪个我忘了!:( 杀死一个进程 {For Windows 9x/ME/2000/XP } usesTlhelp32;function KillTask(ExeFileName: string): Integer;constPROCESS_TERMINATE = $0001;varContinueLoop: BOOL;FSnapshotHandle: THandle;FProcessEntry32: TProcessEntry32;beginResult := 0;FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);FProcessEntry32.dwSize := SizeOf(FProcessEntry32);ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);while Integer(ContinueLoop) <> 0 dobeginif ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) =UpperCase(ExeFileName)) or (UpperCase(FProcessEntry32.szExeFile) =UpperCase(ExeFileName))) thenResult := Integer(TerminateProcess(OpenProcess(PROCESS_TERMINATE,BOOL(0),FProcessEntry32.th32ProcessID),0));ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);end;CloseHandle(FSnapshotHandle);end;procedure TForm1.Button1Click(Sender: TObject);beginKillTask('notepad.exe');end;{ For Windows NT/2000/XP }procedure KillProcess(hWindowHandle: HWND);varhprocessID: INTEGER;processHandle: THandle;DWResult: DWORD;beginSendMessageTimeout(hWindowHandle, WM_CLOSE, 0, 0,SMTO_ABORTIFHUNG or SMTO_NORMAL, 5000, DWResult);if isWindow(hWindowHandle) thenbegin// PostMessage(hWindowHandle, WM_QUIT, 0, 0);{ Get the process identifier for the window}GetWindowThreadProcessID(hWindowHandle, @hprocessID);if hprocessID <> 0 thenbegin{ Get the process handle }processHandle := OpenProcess(PROCESS_TERMINATE or PROCESS_QUERY_INFORMATION,False, hprocessID);if processHandle <> 0 thenbegin{ Terminate the process }TerminateProcess(processHandle, 0);CloseHandle(ProcessHandle);end;end;end;end;procedure TForm1.Button2Click(Sender: TObject);beginKillProcess(FindWindow('notepad',nil));end; 不用这么麻烦了,我改用shellexecute(handle,'open','c:\windows\calc.exe',nil,nil,sw_show)然后var hnd:Thandle;用hnd:=findwindow(nil,'计算器');if hnd<>0 thensendmessage(hnd,wm_close,0,0);即可!不过我还是给分!!! webbrowser如何查找同级元素呢? 如何从Delphi的原代码中修改ImageList里的图片? 显示问题 100分:TChart控件如何打印网格线 一台电脑使同时使用两台打印机的问题。 菜鸟的一个问题 如何在代码中创建一个word格式文件?(不是把word调出来) " '"问题 delphi System.ComponentModel.dcu 有没有取浮点数的小数点位数的函数?? 请教:怎样实现查找局域网中所有oracle服务器 怎样获取COOKIE啊
{For Windows 9x/ME/2000/XP }
usesTlhelp32;function KillTask(ExeFileName: string): Integer;constPROCESS_TERMINATE = $0001;varContinueLoop: BOOL;FSnapshotHandle: THandle;FProcessEntry32: TProcessEntry32;beginResult := 0;FSnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);FProcessEntry32.dwSize := SizeOf(FProcessEntry32);ContinueLoop := Process32First(FSnapshotHandle, FProcessEntry32);while Integer(ContinueLoop) <> 0 dobeginif ((UpperCase(ExtractFileName(FProcessEntry32.szExeFile)) =UpperCase(ExeFileName)) or (UpperCase(FProcessEntry32.szExeFile) =UpperCase(ExeFileName))) thenResult := Integer(TerminateProcess(OpenProcess(PROCESS_TERMINATE,BOOL(0),FProcessEntry32.th32ProcessID),0));ContinueLoop := Process32Next(FSnapshotHandle, FProcessEntry32);end;CloseHandle(FSnapshotHandle);end;procedure TForm1.Button1Click(Sender: TObject);beginKillTask('notepad.exe');end;{ For Windows NT/2000/XP }procedure KillProcess(hWindowHandle: HWND);varhprocessID: INTEGER;processHandle: THandle;DWResult: DWORD;beginSendMessageTimeout(hWindowHandle, WM_CLOSE, 0, 0,SMTO_ABORTIFHUNG or SMTO_NORMAL, 5000, DWResult);if isWindow(hWindowHandle) thenbegin// PostMessage(hWindowHandle, WM_QUIT, 0, 0);{ Get the process identifier for the window}GetWindowThreadProcessID(hWindowHandle, @hprocessID);if hprocessID <> 0 thenbegin{ Get the process handle }processHandle := OpenProcess(PROCESS_TERMINATE or PROCESS_QUERY_INFORMATION,False, hprocessID);if processHandle <> 0 thenbegin{ Terminate the process }TerminateProcess(processHandle, 0);CloseHandle(ProcessHandle);end;end;end;end;procedure TForm1.Button2Click(Sender: TObject);beginKillProcess(FindWindow('notepad',nil));end;
然后var hnd:Thandle;
用
hnd:=findwindow(nil,'计算器');
if hnd<>0 then
sendmessage(hnd,wm_close,0,0);
即可!
不过我还是给分!!!