如何关闭任务栏中的所有程序?(100分)

  • 主题发起人 主题发起人 LEESEASKY
  • 开始时间 开始时间
L

LEESEASKY

Unregistered / Unconfirmed
GUEST, unregistred user!
在一篇文章里看到,用FindWindow,SendMessage(HWnd, WM_CLOSE, 0, 0)<br>可以关闭一个应用程序。<br>所以想怎么才能得到任务栏中的全部程序句柄,并把他们都关掉呢?<br>用下面一段代码,可以得到系统所有程序的信息,但怎么才能知道这些程序是在<br>任务栏中的呢?<br>var <br>&nbsp; hCurrentWindow: HWnd; <br>&nbsp; szText: array[0..254] of char; <br>begin <br>&nbsp; hCurrentWindow := GetWindow(Handle, GW_HWNDFIRST); <br>&nbsp; while hCurrentWindow &lt;&gt; 0 do <br>&nbsp; begin <br>&nbsp; &nbsp; if GetWindowText(hCurrentWindow, @szText, 255) &gt; 0 then <br>&nbsp; &nbsp; &nbsp; Memo1.Lines.Add(StrPas(@szText)); <br>&nbsp; &nbsp; &nbsp; hCurrentWindow := GetWindow(hCurrentWindow, GW_HWNDNEXT); <br>&nbsp; &nbsp; &nbsp;end; <br>end; <br><br>
 
一个想法:<br>&nbsp;任务栏也是一个窗口它有一个handle,在其上显示的程序标题按钮应当也是一个控件,是不是<br>通过这种方法来得到它们的handle,然后再关闭.
 
给你一个控件的代码<br>可以列出所有的进程列表<br>也可以只列出任务栏上的程序列表<br><br>/<br>// &nbsp;Methods:<br>// &nbsp;procedure Execute;<br>// &nbsp;function GetWinHandle (Idx : Integer): HWND;<br>// &nbsp;function GetWinParent (Idx : Integer): HWND;<br>// &nbsp;function GetThreadID &nbsp;(Idx : Integer): HTask;<br>// &nbsp;function GetIconHandle(Idx : Integer): HIcon;<br>// &nbsp;function GetProcessID (Idx : Integer): DWORD;<br>// &nbsp;function GetWinStyle &nbsp;(Idx : Integer): LongInt;<br>// &nbsp;function GetWinExStyl (Idx : Integer): LongInt;<br>// &nbsp;function GetWinExePath(Idx : Integer): String;<br>// &nbsp;function GetWnClasName(Idx : Integer): String;<br>// &nbsp;function GetParentName(Idx : Integer): String;<br>//<br>// &nbsp;e.g.: whdl := WinList1.GetWinHandle(ListBox1.ItemIndex);<br>//<br>// &nbsp;Properties:<br>// &nbsp;Property WhatDoFind : TFind_ID;<br>// &nbsp;Property WindowList : TStringList;<br>// &nbsp;Property EmptyNames : Boolean;<br>// &nbsp;Property TaskBarSty : Boolean;<br>// &nbsp;Property TimerIntval: Integer;<br>// &nbsp;Property EnableTimer: Boolean;<br>// &nbsp;Property OnChanged &nbsp;: TNotifyEvent;<br>//<br>//<br><br><br>unit FindWin;<br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>&nbsp; ExtCtrls, tlhelp32, shellApi;<br><br>const<br>&nbsp;cAbout = 'TFindWin4.0(32): &amp;copy; 1996-1998 by Dr.Plass';<br>type<br>&nbsp; TFind_ID = (id_All,id_VisOnly,id_InvisOnly);<br><br>&nbsp; TWinInfo = class(TObject)<br>&nbsp; private<br>&nbsp; public<br>&nbsp; &nbsp;hpar,<br>&nbsp; &nbsp;whdl &nbsp; &nbsp;: HWND;<br>&nbsp; &nbsp;thdl &nbsp; &nbsp;: HTask;<br>&nbsp;Parent,<br>&nbsp; &nbsp;clas,<br>&nbsp;ExePath : String;<br>&nbsp; &nbsp;hIco,<br>&nbsp; &nbsp;phdl &nbsp; &nbsp;: DWORD;<br>&nbsp; &nbsp;wsty,<br>&nbsp; &nbsp;wXty &nbsp; &nbsp;: LongInt;<br>&nbsp; end;<br><br>&nbsp; TWinList = class(TComponent)<br>&nbsp; private<br>&nbsp; &nbsp;FFindID &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : TFind_ID;<br>&nbsp; &nbsp;fWindowsList &nbsp; &nbsp; &nbsp;: TStringList;<br>// WinObj &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: TWinInfo;<br>&nbsp; &nbsp;fAcceptEmptyNames,<br>&nbsp; &nbsp;fTimerOn,<br>&nbsp; &nbsp;fTaskBarStyle &nbsp; &nbsp; : Boolean;<br>&nbsp; &nbsp;fOnChanged &nbsp; &nbsp; &nbsp; &nbsp;: TNotifyEvent;<br>&nbsp; &nbsp;timer1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: TTimer;<br>&nbsp; &nbsp;fTimeVal &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: Integer;<br>&nbsp; &nbsp;fAbout &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: String;<br>&nbsp; protected<br>&nbsp; &nbsp;Procedure GetWindowsInfo(WinHandle : HWND);<br>&nbsp; &nbsp;procedure SomeChanged;<br>&nbsp; &nbsp;procedure FuAbout(value :String);<br>&nbsp; &nbsp;procedure SetTimer(value:Boolean);<br>&nbsp; &nbsp;procedure SetTimeVal(value:Integer);<br>&nbsp; &nbsp;procedure Timer1Timer(Sender:TObject);<br>&nbsp; public<br>&nbsp; &nbsp;Constructor Create(AOwner : TComponent); override;<br>&nbsp; &nbsp;Destructor Destroy; override;<br>&nbsp; &nbsp;Procedure Execute; virtual;<br>&nbsp; &nbsp;function GetWinHandle(Idx : Integer): HWND;<br>&nbsp; &nbsp;function GetWinParent(Idx : Integer): HWND;<br>&nbsp; &nbsp;function GetThreadID (Idx : Integer): HTask;<br>&nbsp; &nbsp;function GetIconHandle(Idx : Integer): HIcon;<br>&nbsp; &nbsp;function GetProcessID(Idx : Integer): DWORD;<br>&nbsp; &nbsp;function GetWinStyle (Idx : Integer): LongInt;<br>&nbsp; &nbsp;function GetWinExStyl(Idx : Integer): LongInt;<br>&nbsp; &nbsp;function GetWinExePath(Idx : Integer): String;<br>&nbsp; &nbsp;function GetWnClasName(Idx : Integer): String;<br>&nbsp; &nbsp;function GetParentName(Idx : Integer): String;<br>&nbsp; published<br>&nbsp; &nbsp;Property About &nbsp; &nbsp; &nbsp;: String read FAbout write FuAbout;<br>&nbsp; &nbsp;Property WhatDoFind : TFind_ID read FFindID write FFindID default id_visonly;<br>&nbsp; &nbsp;Property WindowList : TStringList read fWindowsList write fWindowsList;<br>&nbsp; &nbsp;Property EmptyNames : Boolean read fAcceptEmptyNames write fAcceptEmptyNames default False;<br>&nbsp; &nbsp;Property TaskBarSty : Boolean read fTaskBarStyle write fTaskBarStyle default True;<br>&nbsp; &nbsp;Property TimerIntval: Integer read fTimeVal write SetTimeVal default 500;<br>&nbsp; &nbsp;Property EnableTimer: Boolean read fTimerOn write SetTimer default False;<br>&nbsp; &nbsp;Property OnChanged &nbsp;: TNotifyEvent read FOnChanged write FOnChanged;<br>&nbsp; end;<br><br>function &nbsp;GetStyle(hdl : HWnd):Boolean;<br>procedure Register;<br>implementation<br>var<br>&nbsp;WCheck,<br>&nbsp;nWCheck : LongInt;<br>&nbsp;ac,ftb,<br>&nbsp;changed : Boolean;<br>&nbsp;fid &nbsp; &nbsp; : TFind_ID;<br>&nbsp;Nums &nbsp; &nbsp;: Integer;<br>&nbsp;aw &nbsp; &nbsp; &nbsp;: array[0..255] of HWnd;<br><br><br>function EnumWindowsProc(hwndNext : HWnd; var lpara : LongInt):Bool;stdcall;<br>var<br>&nbsp;P &nbsp; &nbsp; &nbsp;: array[0..255] of Char;<br>&nbsp;DoIt &nbsp; : Boolean;<br>&nbsp;hwPop &nbsp;: HWnd;<br>begin<br>&nbsp;Result := True;<br><br>&nbsp;if hwndNext &lt;&gt; 0 then begin<br>&nbsp; P[0] := #0;<br>&nbsp; DoIt := False;<br>&nbsp; GetWindowText(hwndNext, P, 255);<br>&nbsp; if (ac or (P[0] &lt;&gt; #0)) then<br><br>&nbsp; &nbsp;case fid of<br>&nbsp; &nbsp; id_all &nbsp; &nbsp; &nbsp;: &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DoIt := True;<br>&nbsp; &nbsp; id_visonly &nbsp;: if (IsWindowVisible(hwndNext)) &nbsp; &nbsp; then DoIt := True;<br>&nbsp; &nbsp; id_invisonly: if NOT (IsWindowVisible(hwndNext)) then DoIt := True;<br>&nbsp; &nbsp;end;<br><br>&nbsp; &nbsp;if (DoIt)then begin<br>&nbsp; &nbsp; if NOT(ftb) then begin<br> aw[Nums]:= hwndNext;<br>&nbsp; &nbsp; &nbsp;inc (Nums);<br>&nbsp; &nbsp; &nbsp;nWCheck := nWCheck + hwndNext;<br>&nbsp; &nbsp; &nbsp;exit;<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; if (GetWindow(hwndNext, GW_OWNER) = 0) then<br> if GetStyle(hwndNext) then begin<br>&nbsp; &nbsp; &nbsp; aw[Nums]:= hwndNext;<br>&nbsp; &nbsp; &nbsp; inc (Nums);<br>&nbsp; &nbsp; &nbsp; nWCheck := nWCheck + hwndNext;<br>&nbsp; &nbsp; &nbsp;end<br>&nbsp; &nbsp; &nbsp;else if isWindow(GetLastActivePopup(hwndNext))then begin<br>&nbsp; &nbsp; &nbsp; &nbsp;hwPop := GetLastActivePopup(hwndNext);<br>&nbsp; &nbsp; &nbsp; &nbsp;if GetStyle(hwPop) then begin<br>&nbsp; &nbsp; &nbsp; &nbsp; aw[Nums]:= hwndNext;<br>&nbsp; &nbsp; &nbsp; &nbsp; inc (Nums);<br>&nbsp; &nbsp; &nbsp; &nbsp; nWCheck := nWCheck + hwndNext;<br>&nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp;end;<br>&nbsp; end else Result := False;<br>end;<br><br>Constructor TWinList.Create(AOwner : TComponent);<br>Begin<br>&nbsp;inherited Create(AOwner);<br>&nbsp;fWindowsList &nbsp; &nbsp; &nbsp;:= TStringList.Create;<br>&nbsp;timer1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;:= TTimer.Create(self);<br>&nbsp;timer1.OnTimer &nbsp; &nbsp;:= Timer1Timer;<br>&nbsp;timer1.Enabled &nbsp; &nbsp;:= False;<br>&nbsp;fAcceptEmptyNames := False;<br>&nbsp;fTimeVal &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;:= 500;<br>&nbsp;fTimerOn &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;:= False;<br>&nbsp;fFindId &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; := id_visonly;<br>&nbsp;fTaskBarStyle &nbsp; &nbsp; := True;<br>&nbsp;WCheck &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;:= 0;<br>&nbsp;fAbout &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;:= '';<br>&nbsp;changed &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; := True;<br>End;<br><br>Destructor TWinList.Destroy;<br>var<br> i : Integer;<br>Begin<br>&nbsp;for i := 0 to fWindowsList.Count-1 do &nbsp; // that's new because of the delphi bug<br> fWindowsList.Objects.Free;<br>&nbsp;fWindowsList.Clear;<br>&nbsp;fWindowsList.Free;<br>// WinObj.Free;<br>// WinObj := NIL;<br>&nbsp;timer1.Enabled := False;<br>&nbsp;timer1.Free;<br>&nbsp;inherited Destroy;<br>End;<br><br>Procedure TWinList.GetWindowsInfo(WinHandle : HWND);<br>var<br>&nbsp;P &nbsp; &nbsp; &nbsp;: array[0..256] Of Char;<br>&nbsp;dwActiveProcessId : DWORD;<br>&nbsp;snap &nbsp; : THandle;<br>&nbsp;pe32 &nbsp; : TPROCESSENTRY32;<br>&nbsp;tw &nbsp; &nbsp; : Word;<br>&nbsp;ts &nbsp; &nbsp; : array[0..MAX_PATH] of char;<br>&nbsp;WinObj : TWinInfo; //that`s new because of the delphi bug<br>Begin<br>&nbsp;P[0] := #0; snap := 0;<br>&nbsp;WinObj &nbsp; &nbsp; &nbsp;:= TWinInfo.Create;<br>&nbsp;WinObj.whdl := WinHandle;<br>&nbsp;WinObj.thdl := GetWindowThreadProcessId(WinHandle,NIL);<br>&nbsp;GetWindowThreadProcessId(WinHandle, @dwActiveProcessId );<br>&nbsp;WinObj.phdl := dwActiveProcessId;<br>&nbsp;WinObj.wsty := GetWindowLong(WinHandle,GWL_STYLE);<br>&nbsp;WinObj.wXty := GetWindowLong(WinHandle,GWL_EXSTYLE);<br>&nbsp;WinObj.hPar := GetWindowLong(WinHandle,GWL_HWNDPARENT);<br>&nbsp;GetClassName(WinHandle, P, 100);<br>&nbsp;WinObj.clas := String(P);<br><br><br>&nbsp;if WinObj.hPar = 0 then P := 'Desktop'<br>&nbsp;else &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GetWindowText(WinObj.hPar, P, 255);<br>&nbsp;if P='' then P := 'NoName';<br>&nbsp;WinObj.Parent := String(P);<br><br>&nbsp;GetWindowText(WinHandle, P, 255);<br>&nbsp;try<br> snap := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,<br> dwActiveProcessId);<br> if snap &lt;&gt; 0 then begin<br> pe32.dwSize := SizeOf(TPROCESSENTRY32);<br> if Process32First(snap, pe32) then begin<br> if pe32.th32ProcessID = dwActiveProcessId then begin<br> WinObj.ExePath := String(pe32.szExeFile);<br><br> // and so on ProcessEntry<br> end else begin<br> while Process32Next(snap, pe32) do begin<br> if pe32.th32ProcessID = dwActiveProcessId then begin<br> WinObj.ExePath := String(pe32.szExeFile);<br> // and so on ProcessEntry<br> break;<br> end;<br> end;<br> end;<br> tw &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;:= 0;<br> StrCopy(ts,pe32.szExeFile);<br> WinObj.hIco := ExtractIcon(hInstance,pe32.szExeFile,0);<br> if WinObj.hIco=0 then<br> Winobj.hIco &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;:= ExtractAssociatedIcon(hInstance,ts,tw);<br> end;<br> end;<br>&nbsp;finally<br> CloseHandle(snap);<br>&nbsp;end;<br>&nbsp;fWindowsList.AddObject(P, WinObj);<br><br>End;<br><br>Procedure TWinList.Execute;<br>var<br>&nbsp;i : Integer;<br>Begin<br>&nbsp;nWCheck := 0;<br>&nbsp;timer1.Enabled := False;<br>&nbsp;ac &nbsp; := fAcceptEmptyNames;<br>&nbsp;fid &nbsp;:= FFindID;<br>&nbsp;ftb &nbsp;:= fTaskBarStyle;<br>&nbsp;Nums := 0;<br><br>&nbsp;EnumWindows(@EnumWindowsProc,0);<br><br>&nbsp;if WCheck &lt;&gt; nWCheck then begin<br> WCheck &nbsp;:= nWCheck;<br> for i := 0 to fWindowsList.Count-1 do &nbsp;//that`s new because of the delphi bug<br> fWindowsList.Objects.Free;<br> fWindowsList.Clear;<br> for i := 0 to Nums - 1 do<br> GetWindowsInfo(aw);<br> changed := True;<br>&nbsp;end else changed := False;<br>&nbsp;if fTimerOn then timer1.Enabled := True;<br>End;<br><br>procedure TWinList.SomeChanged;<br>begin<br>&nbsp;if Assigned(FOnChanged) then FOnChanged(Self);<br>end;<br><br>procedure TWinList.Timer1Timer(Sender:TObject);<br>begin<br>&nbsp;if fTimeVal &lt; 1 then timer1.Enabled := False;<br>&nbsp;Execute;<br>&nbsp;Application.ProcessMessages;<br>&nbsp;if changed then SomeChanged;<br>end;<br><br>procedure TWinList.FuAbout(value:String);<br>begin<br>&nbsp;if value &lt;&gt; fAbout then begin<br> fAbout := cAbout;<br> MessageDlg('Komponente: TFindWin4.0 [32] &nbsp; &nbsp; &nbsp; &nbsp; '+ #10#13+ #10#13+<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '&amp;copy; 1996-1998 by Dr.Plass &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '+ #10#13+<br> &nbsp; &nbsp;'Peter.Plass@fh-zwickau.de &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; '+ #10#13+<br> &nbsp; &nbsp;'http://www.fh-zwickau.de/~pp/tm.htm ',<br> &nbsp; &nbsp;mtInformation, [mbOk],0);<br>&nbsp; end;<br>end;<br><br>procedure TWinList.SetTimer(value:Boolean);<br>begin<br>&nbsp;if value &lt;&gt; fTimerOn then begin<br>&nbsp; fTimerOn &nbsp; &nbsp; &nbsp; := value;<br>&nbsp; timer1.Enabled := fTimerOn;<br>&nbsp;end;<br>end;<br><br>procedure TWinList.SetTimeVal(value: Integer);<br>begin<br>&nbsp;if value &lt; 1 then &nbsp;EnableTimer:= False;<br>&nbsp;if value &lt;&gt; fTimeVal then begin<br>&nbsp; fTimeVal &nbsp; &nbsp; &nbsp; &nbsp;:= value;<br>&nbsp; timer1.Interval := fTimeVal;<br>&nbsp;end;<br>end;<br><br>function TWinList.GetWinParent(Idx : Integer) : HWND;<br>begin<br>&nbsp; Result := TWinInfo(fWindowsList.Objects[Idx]).hpar;<br>end;<br>function TWinList.GetWinHandle(Idx : Integer) : HWND;<br>begin<br>&nbsp; Result := TWinInfo(fWindowsList.Objects[Idx]).whdl;<br>end;<br>function TWinList.GetThreadID(Idx : Integer) : HTask;<br>begin<br>&nbsp; Result := TWinInfo(fWindowsList.Objects[Idx]).thdl;<br>end;<br>function TWinList.GetIconHandle(Idx : Integer) : HIcon;<br>begin<br>&nbsp; Result := TWinInfo(fWindowsList.Objects[Idx]).hIco;<br>end;<br>function TWinList.GetProcessID(Idx : Integer) : DWORD;<br>begin<br>&nbsp; Result := TWinInfo(fWindowsList.Objects[Idx]).phdl;<br>end;<br>function TWinList.GetWinStyle(Idx : Integer) : LongInt;<br>begin<br>&nbsp; Result := TWinInfo(fWindowsList.Objects[Idx]).wsty;<br>end;<br>function TWinList.GetWinExStyl(Idx : Integer) : LongInt;<br>begin<br>&nbsp; Result := TWinInfo(fWindowsList.Objects[Idx]).wXty;<br>end;<br>function TWinList.GetWinExePath(Idx : Integer): String;<br>begin<br>&nbsp; Result := TWinInfo(fWindowsList.Objects[Idx]).ExePath;<br>end;<br>function TWinList.GetWnClasName(Idx : Integer): String;<br>begin<br>&nbsp;Result := TWinInfo(fWindowsList.Objects[Idx]).clas;<br>end;<br>function TWinList.GetParentName(Idx : Integer): String;<br>begin<br>&nbsp;Result := TWinInfo(fWindowsList.Objects[Idx]).Parent;<br>end;<br><br>function GetStyle(hdl : HWnd):Boolean;<br>var<br>&nbsp;style : LongInt;<br>BEGIN<br>&nbsp;Result := False;<br>&nbsp;style &nbsp;:= GetWindowLong(hdl,GWL_STYLE);<br>&nbsp;if(( &nbsp;style AND WS_POPUP) &nbsp; &nbsp;= WS_POPUP) then &nbsp;begin<br>&nbsp; if(((style AND WS_GROUP) &nbsp; &nbsp;= WS_GROUP) &nbsp; &nbsp; &nbsp; AND NOT<br>&nbsp; &nbsp; &nbsp;((style AND WS_DISABLED) = WS_DISABLED) &nbsp; &nbsp;AND<br>&nbsp; &nbsp; &nbsp;((style AND WS_SYSMENU) &nbsp;= WS_SYSMENU)) &nbsp; &nbsp;then<br>&nbsp; &nbsp; &nbsp;Result := True;<br>&nbsp;end else Result := True;<br>END;<br><br>procedure Register;<br>begin<br>&nbsp; RegisterComponents('Pl-Freeware', [TWinList]);<br>end;<br><br>end.<br><br>留下MAIL<br>我可以发给你例子程序
 
e-mail : leeseas_cn@sina.com<br><br>今天在调试的时候又发现用sendmessage可以关闭QQ等一些程序,但无法关闭<br>我的电脑和IE,后来改用postmessage就都关掉了,这两个函数有什么不同呢?<br>哦,对了,有一个怎么也关不掉 : MSN Messenger
 
sendmessage将消息传送给指定窗口并调用其wndproc来处理,直到处理完成才返回<br>postmessage是将消息放到消息队列中后立即返回,而不管是否处理完成.
 
谢谢两位参与讨论我的问题!
 
后退
顶部