自己写的一个小代码,列出所有窗口HANDLE<br><br>unit Unit1;<br><br>interface<br><br>uses<br> Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br> StdCtrls,PSAPI;<br>const<br> CM_MAIN =WM_USER+$2010;<br>type<br> TForm1 = class(TForm)<br> Memo1: TMemo;<br> Button1: TButton;<br> procedure Button1Click(Sender: TObject);<br> private<br> { Private declarations }<br> public<br> { Public declarations }<br><br> Procedure ListAllActiveWnd();<br><br> end;<br><br>var<br> Form1: TForm1;<br> <br> <br>implementation<br><br>{$R *.DFM}<br><br>Procedure TForm1.ListAllActiveWnd();<br><br> type<br> PStringList = ^TStringList;<br><br> Function EnumWndProc(hWin: HWND; Param: LPARAM): BOOL; stdcall;<br> var<br> szBuf: array[0..100] of Char;<br><br> begin<br> // 取得所有可見的視窗<br> //if (IsWindow(hWin) and IsWindowVisible(hWin)) then<br> if (IsWindow(hWin) ) then<br> begin<br> // 只取最上層可見視窗<br> if (Windows.GetParent(hWin) = 0) then<br> begin<br> FillChar(szBuf, SizeOf(szBuf), 0);<br> GetWindowText(hWin, szBuf, SizeOf(szBuf));<br> if string(szbuf)='mainform' then<br> begin<br> PostMessage(hWin, CM_MAIN, 0, 2);<br> end<br> else<br> begin<br><br> end;<br> if (szBuf <> '') then<br> PStringList(Param)^.AddObject(szBuf, TObject(hWin));<br> end;<br> end;<br><br> Result := TRUE;<br> end;<br><br>var<br> i: Integer;<br> slWnd: TStringList;<br> dwProcess1, dwProcess2: DWORD;<br> cnt
WORD;<br> szModName:array [0..255]of char;<br> hProc:THandle;<br> hMod: HMODULE;<br>begin<br> slWnd := TStringList.Create;<br> // 用 EnumWindows 列舉出所有可能的視窗, 並記在TStringList中<br> EnumWindows(@EnumWndProc, LPARAM(@slWnd));<br><br> Memo1.Lines.Clear;<br><br> // 比對所有可能的視窗, 去掉屬於同一個Process的視窗<br> // 最後一個一定是Program Manager, 所以loop只到slWnd.Count - 2, 直接去掉<br> // Program Manager<br> for i := 0 to slWnd.Count-2 do<br> begin<br> GetWindowThreadProcessId(HWND(slWnd.Objects
), @dwProcess1);<br> GetWindowThreadProcessId(HWND(slWnd.Objects[i + 1]), @dwProcess2);<br> if dwProcess1 <> dwProcess2 then<br> begin<br> //Memo1.Lines.Add(slWnd.Strings);<br> // 以下為取得執行檔名稱的過程<br> hProc:= OpenProcess(PROCESS_QUERY_INFORMATION or PROCESS_VM_READ, false, dwProcess1);<br> if (hProc)<>0 then<br> begin<br> FillChar(szModName, SizeOf(szModName), 0);<br> EnumProcessModules(hProc, @hMod, SizeOf(hMod), cnt);<br> if GetModuleFileNameEx(hProc, hMod, szModName,sizeof(szModName))<>0 then<br> begin<br> Memo1.Lines.Add(slWnd.Strings+'--->'+szModName);<br> end;<br> CloseHandle(hProc);<br> end;<br> end;<br> end;<br><br> slWnd.Free;<br>end;<br><br><br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br> ListAllActiveWnd;<br>end;<br><br>end.