我有一个应用程序他在进程中的名称为: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; 

解决方案 »

  1.   

    这个用GetWindow和EnumChildWindows可以得到
      

  2.   


    #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;
    }
      

  3.   

    有点错#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);
    wprintf(L"handle:%p,title:%s,class:%s\n",h,Text1,Text2);
    }while(h=GetWindow(h,GW_HWNDNEXT));
    return 0;
    }
      

  4.   

    GW_HWNDNEXT  在delphi裡面沒有吧
      

  5.   

    GW_CHILD是什麼意思也不懂,我太差了,真不好意思
      

  6.   

    GW_CHILD用来找指定窗口的第一个子窗口,它等于5
      

  7.   

    如果里面有panel之类的控件,还要对panel应用GetWindow(h,GW_CHILD);才能找到那里面的控件
      

  8.   

    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;
      

  9.   


    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;
      

  10.   

    GetWindowRect得到包围窗口的矩形
      

  11.   

    EnumChildWindows(Handle, @EnumChildWindowProc, 0);
    這一句編譯通不過提示:
    Variable required 
    我用的是delphi6
      

  12.   

    GetWindowRect(Handle, pRect);
    pRect需要定義嗎,如果不定議的話好像提示錯誤耶,我是菜鳥,請多多指教,我能加你的即時通信工具嗎
    如果不方便透的話你加我喲:QQ77190062  MSN:[email protected]  SKPYE:liaodm
      

  13.   

    你就用
    FindWindowEx()吧
    执行一次就是找到第一个,执行2次就是找到第2个。
    我就是这么用的一直,很稳定。
      

  14.   

    可是怎麼也找不到Label的句柄,好像Label沒有句柄,暈倒
    我現在需要得到Label上面的Caption值,怎麼辦,有高手站出來幫忙嗎?
      

  15.   

    用EnumChildWindows这个api遍历啊!
      

  16.   

    lable控件不属于窗体类,所以它没有HWND属性
      

  17.   

    label是没有句柄的啊,他是从TGraphicControl继承下来的
      

  18.   


    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;
      

  19.   

    你不用去管pRect,以后等你用到了,就知道是什么东西了