白天的错了,刚才查了一下,应该是 EnumWindows 通过进程PID枚举窗口 例子 回调过程 function EnumTopProc(wnd: HWND; LParam: DWORD): BOOL; stdcall; var ID: DWORD; WndCaption: array[0..254] of char; WndClassName: array[0..254] of char; begin GetWindowThreadProcessId(wnd, @ID); GetWindowText(wnd, @WndCaption, 254); GetClassName(wnd, @WndClassName, 254); if ID = LParam then begin //StrPas(WndCaption) {这个是窗口名} //wnd {窗口句柄} //StrPas(WndClassName) {窗口类} end; Result := True; end;EnumWindows(@EnumTopProc, PDI);
EnumWindows 通过进程PID枚举窗口
例子
回调过程
function EnumTopProc(wnd: HWND; LParam: DWORD): BOOL; stdcall;
var
ID: DWORD;
WndCaption: array[0..254] of char;
WndClassName: array[0..254] of char;
begin GetWindowThreadProcessId(wnd, @ID);
GetWindowText(wnd, @WndCaption, 254);
GetClassName(wnd, @WndClassName, 254); if ID = LParam then
begin
//StrPas(WndCaption) {这个是窗口名}
//wnd {窗口句柄}
//StrPas(WndClassName) {窗口类}
end;
Result := True;
end;EnumWindows(@EnumTopProc, PDI);
不需要发消息都可以的
比如你开10个IE窗口,但进程只有一个,你总不会认为这10个IE窗口的句柄是一样的吧
////////////////////
{Unit1.dfm}
object Form1: TForm1
Left = 192
Top = 114
Width = 507
Height = 302
Caption = 'Form1'
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'MS Sans Serif'
Font.Style = []
OldCreateOrder = False
PixelsPerInch = 96
TextHeight = 13
object Label1: TLabel
Left = 24
Top = 24
Width = 36
Height = 13
Caption = #36827#31243#21495
end
object Edit1: TEdit
Left = 72
Top = 16
Width = 121
Height = 21
TabOrder = 0
Text = '0'
end
object Button1: TButton
Left = 232
Top = 16
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 1
OnClick = Button1Click
end
object ListView1: TListView
Left = 16
Top = 56
Width = 457
Height = 185
Columns = <
item
Caption = #31383#21475#21517
Width = 180
end
item
Caption = #31383#21475#21477#26564
Width = 120
end
item
Caption = #31867#21517
Width = 120
end>
TabOrder = 2
ViewStyle = vsReport
end
end
//////////////////////////////////////////////////
{Unit1.pas}
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, StdCtrls;type
TForm1 = class(TForm)
Edit1: TEdit;
Button1: TButton;
Label1: TLabel;
ListView1: TListView;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}
function EnumTopProc(wnd: HWND; LParam: DWORD): BOOL; stdcall;
var
ID: DWORD;
WndCaption: array[0..254] of char;
WndClassName: array[0..254] of char;
ListItem:TListItem;
begin GetWindowThreadProcessId(wnd, @ID); //获得进程的PID
GetWindowText(wnd, @WndCaption, 254); //获得窗口名
GetClassName(wnd, @WndClassName, 254); //获得窗口类名 if ID = LParam then
begin
ListItem:=Form1.ListView1.Items.Add;//添加到ListView
ListItem.Caption:=StrPas(WndCaption);// 这个是窗口名
ListItem.SubItems.Add(FormatFloat('00000000',wnd));//窗口句柄
ListItem.SubItems.Add(StrPas(WndClassName));//窗口类名
end;
Result := True;
end;procedure TForm1.Button1Click(Sender: TObject);
begin
ListView1.Clear;
EnumWindows(@EnumTopProc, Strtointdef(edit1.Text,0)); //枚举进程的窗口
//@EnumTopProc 为回调过程
//Strtointdef(edit1.Text,0) 为引入的进程PID
end;end.