如何用程序关闭一个正在运行的程序!(20分)

  • 主题发起人 主题发起人 angelnice
  • 开始时间 开始时间
A

angelnice

Unregistered / Unconfirmed
GUEST, unregistred user!
详细:<br>单击button1在listbox1显示所有正在运行的程序<br>单击button2关闭当前在listbox1中选中的程序。<br>希望列出源程序清单,谢谢!<br>因为我是菜鸟!
 
// 取得所有窗口名称<br>var<br>&nbsp; &nbsp; i: HWnd;<br>&nbsp; &nbsp; szText: array[0..254] of char;<br>&nbsp; &nbsp; sef:string;<br>begin<br>&nbsp; &nbsp; i := GetWindow(Handle, GW_HWNDFIRST);<br>&nbsp; &nbsp; while i &lt;&gt; 0 do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; if GetWindowText(i, @szText, 255)&gt;0 then<br>&nbsp; &nbsp; &nbsp; &nbsp; listbox1.items.Add(StrPas(@szText));<br>&nbsp; &nbsp; &nbsp; &nbsp; i:=GetWindow(i, GW_HWNDNEXT);<br>&nbsp; &nbsp; end;<br>end;<br><br>// 关闭所选中的程序<br>Var<br>&nbsp; &nbsp; I:Thandle;<br>begin<br>&nbsp; &nbsp; I:=FindWindow(nil,pchar(listbox1.items[listbox1.itemindex]));<br>&nbsp; &nbsp; If I&lt;&gt;0 Then &nbsp;SendMessage(I,WM_SYSCOMMAND,SC_CLOSE,0);<br>end;<br>
 
还有是不是要use哪个单元啊!
 
想知道USE哪个单元很简单,在用到的函数上按F1进入帮助,帮助中靠前有一行Unit,下面就是你要的单元
 
来自:jsxjd, 时间:2002-11-22 12:19:00, ID:1450931<br>-----------------------------------------------------------------<br>获得进程列表,并终止 Excel 进程<br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; FSnapshotHandle:THandle;<br>&nbsp; FProcessEntry32:TProcessEntry32;<br>&nbsp; Ret : BOOL;<br>&nbsp; ProcessID : integer;<br>&nbsp; s:string;<br>begin<br>&nbsp; FSnapshotHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);<br>&nbsp; FProcessEntry32.dwSize:=Sizeof(FProcessEntry32);<br>&nbsp; Ret:=Process32First(FSnapshotHandle,FProcessEntry32);<br>&nbsp; Memo1.clear;<br>&nbsp; while Ret do<br>&nbsp; begin<br>&nbsp; &nbsp; Memo1.lines.add(FProcessEntry32.szExeFile);<br>&nbsp; &nbsp; s:=ExtractFileName(FProcessEntry32.szExeFile);<br>&nbsp; &nbsp; if s='EXCEL.EXE' then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; ProcessID:=FProcessEntry32.th32ProcessID;<br>&nbsp; &nbsp; &nbsp; TerminateProcess(OpenProcess(PROCESS_TERMINATE,false,ProcessID),1);<br>&nbsp; &nbsp; &nbsp; s:='';<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; Ret:=Process32Next(FSnapshotHandle,FProcessEntry32);<br>&nbsp; end;<br>end;
 
TerminateProcess(ProcessHandle);
 
wjh_wy给出的代码引用的单元是:TlHelp32,代劳了,呵呵。
 

Similar threads

S
回复
0
查看
835
SUNSTONE的Delphi笔记
S
S
回复
0
查看
797
SUNSTONE的Delphi笔记
S
后退
顶部