我有一个应用程序他在进程中的名称为:main.exe
我通过API的函数取得了 main.exe的句柄
main.exe的窗体中有好多个控件。现在的问题是我如何才可以通过一个类似循环的方式来
取得所有控件的句柄。如果不使用循环的方式FindWindowEx只能找到第一个控件的句柄。
我的代碼如下:
procedure TForm1.Button1Click(Sender: TObject);
var
ProcessName:string;//進程名
FSnapshotHandle:THandle;//進程快照句柄
FProcessEntry32:TProcessEntry32;//進程入口的結構體信息
ContinueLoop:BOOL;
myHwnd:THandle;
myHwnd2:THandle;
hd:THandle;
arr: array[0..255] of Char;
Buf: array[0..1024] of Char;
begin
FSnapshotHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
FProcessEntry32.dwSize:=Sizeof(FProcessEntry32);
ContinueLoop:=Process32First(FSnapshotHandle,FProcessEntry32);
while ContinueLoop do
begin
ProcessName:=FProcessEntry32.szExeFile;
if(ProcessName='Main.exe') then
begin
myHwnd:=GetHWndByPID(FProcessEntry32.th32ProcessID); //應用程序句柄
end;
ContinueLoop:=Process32Next(FSnapshotHandle,FProcessEntry32);
end;
CloseHandle(FSnapshotHandle);//釋放快照句柄 myHwnd2:=FindWindowEx(myHwnd,0,nil,nil); //查找應用程序中的控件句柄
SendMessage(myHwnd2,WM_GETTEXT,255,Longint(@arr)); //取得控件標題
GetClassName(myHwnd2,Buf, 1024); // 得到類名
Label4.Caption:=IntTostr(myHwnd2)+arr+Buf; //這樣只是得到了第一個控件的值和類名,可是我怎麼才能行到第二個,第三個.....第N個控件的句柄呢
end;
我通过API的函数取得了 main.exe的句柄
main.exe的窗体中有好多个控件。现在的问题是我如何才可以通过一个类似循环的方式来
取得所有控件的句柄。如果不使用循环的方式FindWindowEx只能找到第一个控件的句柄。
我的代碼如下:
procedure TForm1.Button1Click(Sender: TObject);
var
ProcessName:string;//進程名
FSnapshotHandle:THandle;//進程快照句柄
FProcessEntry32:TProcessEntry32;//進程入口的結構體信息
ContinueLoop:BOOL;
myHwnd:THandle;
myHwnd2:THandle;
hd:THandle;
arr: array[0..255] of Char;
Buf: array[0..1024] of Char;
begin
FSnapshotHandle:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
FProcessEntry32.dwSize:=Sizeof(FProcessEntry32);
ContinueLoop:=Process32First(FSnapshotHandle,FProcessEntry32);
while ContinueLoop do
begin
ProcessName:=FProcessEntry32.szExeFile;
if(ProcessName='Main.exe') then
begin
myHwnd:=GetHWndByPID(FProcessEntry32.th32ProcessID); //應用程序句柄
end;
ContinueLoop:=Process32Next(FSnapshotHandle,FProcessEntry32);
end;
CloseHandle(FSnapshotHandle);//釋放快照句柄 myHwnd2:=FindWindowEx(myHwnd,0,nil,nil); //查找應用程序中的控件句柄
SendMessage(myHwnd2,WM_GETTEXT,255,Longint(@arr)); //取得控件標題
GetClassName(myHwnd2,Buf, 1024); // 得到類名
Label4.Caption:=IntTostr(myHwnd2)+arr+Buf; //這樣只是得到了第一個控件的值和類名,可是我怎麼才能行到第二個,第三個.....第N個控件的句柄呢
end;
#include "stdafx.h"
#include<stdio.h>
#include<windows.h>int main(int argc, char* argv[])
{
HWND h=FindWindowW(0,L"计算器");wchar_t Text1[256],Text2[256];
h=GetWindow(h,GW_CHILD);
do
{
SendMessageW(h,WM_GETTEXT,256,(LPARAM)Text1);
GetClassNameW(h,Text2,256);
printf("handle:%p,title:%s,class:%s\n",h,Text1,Text2);
}while(h=GetWindow(h,GW_HWNDNEXT));
return 0;
}
#include<stdio.h>
#include<windows.h>int main(int argc, char* argv[])
{
HWND h=FindWindowW(0,L"计算器");wchar_t Text1[256],Text2[256];
h=GetWindow(h,GW_CHILD);
do
{
SendMessageW(h,WM_GETTEXT,256,(LPARAM)Text1);
GetClassNameW(h,Text2,256);
wprintf(L"handle:%p,title:%s,class:%s\n",h,Text1,Text2);
}while(h=GetWindow(h,GW_HWNDNEXT));
return 0;
}
Handle : THandle;
begin
Handle := FindWindow(nil, PChar(Edit1.Text));
if Handle <> 0 then
begin
GetWindowRect(Handle, pRect);
EnumChildWindows(Handle, @EnumChildWindowProc, 0);
end;
end;
function EnumChildWindowProc(hWnd : Integer; lParam : Integer): Boolean; stdcall;
var
cName : array[0..254] of Char;
wText : array[0..254] of Char;
cRect : TRect;
begin
GetClassName(hWnd, cName, 255);
GetWindowText(hWnd, wText, 255);
GetWindowRect(hWnd, cRect);
Result := True;
end;
var
Handle : THandle;
begin
Handle := FindWindow(nil, PChar(Edit1.Text));
if Handle <> 0 then
begin
GetWindowRect(Handle, pRect);
EnumChildWindows(Handle, @EnumChildWindowProc, 0);
end;
end;
function EnumChildWindowProc(hWnd : Integer; lParam : Integer): Boolean; stdcall;
var
cName : array[0..254] of Char;
wText : array[0..254] of Char;
cRect : TRect;
begin
GetClassName(hWnd, cName, 255);
GetWindowText(hWnd, wText, 255);
GetWindowRect(hWnd, cRect);
Result := True;
end;
這一句編譯通不過提示:
Variable required
我用的是delphi6
pRect需要定義嗎,如果不定議的話好像提示錯誤耶,我是菜鳥,請多多指教,我能加你的即時通信工具嗎
如果不方便透的話你加我喲:QQ77190062 MSN:[email protected] SKPYE:liaodm
FindWindowEx()吧
执行一次就是找到第一个,执行2次就是找到第2个。
我就是这么用的一直,很稳定。
我現在需要得到Label上面的Caption值,怎麼辦,有高手站出來幫忙嗎?
var
Handle : THandle;
begin
Handle := FindWindow(nil, PChar(Edit1.Text));
if Handle <> 0 then
begin
EnumChildWindows(Handle, @EnumChildWindowProc, 0);
end;
end;
function EnumChildWindowProc(hWnd : Integer; lParam : Integer): Boolean; stdcall;
var
cName : array[0..254] of Char;
wText : array[0..254] of Char;
begin
GetClassName(hWnd, cName, 255);
GetWindowText(hWnd, wText, 255);
Result := True;
end;