怎样关闭托盘程序!!!!(高手请进)(100分)

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

a_mao_gong

Unregistered / Unconfirmed
GUEST, unregistred user!
各位高手,兄弟我现在遇到一个难题,请援手。<br>我在程序运行的时候打开了另一个程序(托盘程序),然后想在程序关闭的时候把托盘程序也一起关闭,但没有找到好的方法。不知道有谁用过TerminateProcess()来关闭托盘程序,或者有更好的方法。
 
API SendMessage()
 
//安装图标<br>procedure TForm1.InstallIcon;<br>begin<br>&nbsp; IconData.cbSize:=SizeOf(IconData);<br>&nbsp; IconData.Wnd:=Handle;<br>&nbsp; IconData.uID:=ICON_ID;<br>&nbsp; IconData.uFlags:=NIF_ICON or NIF_MESSAGE or NIF_TIP;<br>&nbsp; IconData.uCallBackMessage:=ICONEVENT;<br>&nbsp; IconData.hIcon:=Form1.Icon.Handle;;<br>&nbsp; IconData.szTip:='广告窗口杀手';<br>&nbsp; Shell_NotifyIcon(NIM_ADD, @IconData );<br>end;<br>//卸载图标<br>procedure TForm1.UnInstallIcon;<br>begin<br>&nbsp; Shell_NotifyIcon(NIM_DELETE, @IconData );<br>end;
 
托盘程序的进程句柄能获得就好办了。
 
uses ShellAPI;<br><br>var<br>&nbsp; hw : Thandle;<br>&nbsp; procid : DWORD;<br>&nbsp; threadid : DWORD;<br>&nbsp; nid: TNotifyIconData;<br>begin<br>&nbsp; hw := findwindow(pChar(Edit1.text),nil);<br>&nbsp; if hw &lt;&gt; 0 then<br>&nbsp; begin<br>// &nbsp; &nbsp;showmessage('found');<br>&nbsp; &nbsp; threadid := getwindowthreadprocessid(hw,@procid);<br>&nbsp; &nbsp; procid := OpenProcess(PROCESS_TERMINATE,false,procid);<br>&nbsp; &nbsp; //postmessage(hw,WM_QUIT,0,0); &nbsp;<br>&nbsp; &nbsp; nid.cbSize := sizeof(nid);<br>&nbsp; &nbsp; nid.uID := 1;//内部标识,与加入小图标时的数一致,不然需鼠标移过去才消失。<br>&nbsp; &nbsp; nid.Wnd := hw; //主窗口句柄<br>&nbsp; &nbsp; Shell_NotifyIcon(NIM_DELETE, @nid); //去掉小图标<br>&nbsp; &nbsp; TerminateProcess(procid,0);<br>&nbsp; end;<br>end;<br><br>有的也可以这样关闭:<br>var<br>&nbsp; hw : Thandle;<br>begin<br>&nbsp; hw := findwindow(pChar(Edit1.text),nil);<br>&nbsp; if hw &lt;&gt; 0 then<br>&nbsp; begin<br>&nbsp; &nbsp; postmessage(hw,WM_quit,0,0);<br>&nbsp; end;<br>end;
 
后退
顶部