如何列举所有打开的窗口,将标题和句柄显示在一个列表中!(100分)

  • 主题发起人 主题发起人 乖乖兔
  • 开始时间 开始时间

乖乖兔

Unregistered / Unconfirmed
GUEST, unregistred user!
如题。{在线等待}
 
procedure TForm1.Button3Click(Sender: TObject);<br>var<br>&nbsp; hCurrentWindow:HWnd;<br>&nbsp; szText:array[0..254]of char;<br>begin<br>&nbsp; listbox2.clear;<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; ListBox2.Items.Add(Strpas(@szText));<br>&nbsp; &nbsp; &nbsp; hCurrentWindow:=GetWindow(hCurrentWindow,GW_HWNDNEXT);<br>&nbsp; end;<br>end;<br>
 
用EnumWindow()函数,它要求一个回调函数,并将每个窗口的句柄传递给回调函数,这样在回调函数中你就可以取得所有窗口的相关信息
 
请哪位给出 EnumWindow() 和 EnumWindowProc() 这两个函数应用的详细代码!<br><br>
 
直接用全文检索搜EnumWindowProc就行.<br>我给你搜了一个:<br>http://www.delphibbs.com/delphibbs/dispq.asp?lid=468437
 
var<br>&nbsp; WindowName,<br>&nbsp; WinHandle:String;<br><br>function EnumWindowsProc(Hw: HWnd; AMainForm: TForm1): Boolean; stdcall;<br>var<br>&nbsp; WinName: array[0..144] of Char;<br>begin<br>&nbsp; Result := True;<br>&nbsp; GetWindowText(Hw, WinName, 144); <br>&nbsp; WinHandle := IntToStr(GetActiveWindow);<br>&nbsp; WindowName := StrPas(WinName); <br>&nbsp; Form1.ListBox.Items.Add(.....);<br>end;<br><br>procedure TForm1.btnGetWinInfoClick(Sender: TObject);<br>begin<br>&nbsp; EnumWindows(@EnumWindowsProc, 0);<br>end;
 
function EnumChildProc(AHWnd: HWnd; lParam: lParam): Boolean; stdcall;<br>var<br>&nbsp; aryWndCaption: array[0..255] of Char;<br>&nbsp; aryWndClassName: array[0..255] of Char;<br>&nbsp; sClassName: string;<br>begin<br>&nbsp; FillChar(aryWndCaption, 256, #0);<br>&nbsp; FillChar(aryWndClassName, 256, #0);<br>&nbsp; GetWindowText(AHWnd, aryWndCaption, 255);<br>&nbsp; GetClassName(AHWnd, aryWndClassName, 255);<br>&nbsp; sClassName := StrPas(aryWndClassName);<br><br>&nbsp; if AnsiCompareText(sClassName, 'SysListView32') = 0 then<br>&nbsp; begin<br>&nbsp; &nbsp; frmMain.Label1.Caption := IntToHex(AHWnd, 8);<br>&nbsp; &nbsp; //frmMain.Label2.Caption := StrPas(aryWndCaption);<br>&nbsp; end;<br><br>&nbsp; Result := True;<br>end;<br><br>function EnumWindowProc(AHwnd: HWnd; lparam: lParam): Boolean; stdcall;<br>var<br>&nbsp; aryWndCaption: array[0..255] of Char;<br>&nbsp; aryWndClassName: array[0..255] of Char;<br>&nbsp; sClassName: string;<br>begin<br>&nbsp; FillChar(aryWndCaption, 256, #0);<br>&nbsp; FillChar(aryWndClassName, 256, #0);<br>&nbsp; GetWindowText(AHWnd, aryWndCaption, 255);<br>&nbsp; GetClassName(AHWnd, aryWndClassName, 255);<br>&nbsp; sClassName := StrPas(aryWndClassName);<br>&nbsp; if (AnsiCompareText(sClassName, 'Progman') = 0) or<br>&nbsp; &nbsp; (AnsiCompareText(sClassName, 'WorkerW') = 0) then<br>&nbsp; &nbsp; EnumChildWindows(AHWnd, @EnumChildProc, 0);<br>&nbsp; Result := True;<br>end;<br><br>{$R *.DFM}<br><br>procedure TfrmMain.btnSearchClick(Sender: TObject);<br>begin<br>&nbsp; EnumWindows(@EnumWindowProc, 0);<br>end;<br>偶这段代码是为了搜索一个特定的窗口,你看着改一下就OK了
 
去看看windows核心编程那本书吧,讲的很详细。
 
后退
顶部