开发者

Creating a function to dig for Windows Handle by Classname(s) Only

So I just got an answer to my question about getting the Skype Chatbox handle.

I am now trying to create a simple function, that digs for a handle. Here is how I am hoping to be able to use it:

MyHWND := DigForHandle(['Notepad','Edit'],['Untitled - Notepad','']);

Params:

1) Array of String: Holds the Class Hierachy.

2) Array of String: Holds the Window Caption Hierachy.

As you see, the 2nd entry开发者_JAVA百科 in the 2nd parameter is empty, since the Edit Class does not have a Window Caption.

Would it be possible to create such function? :)


Try this

uses
  Windows, Messages, TlHelp32, SysUtils;

type
  PGetWindowParam = ^TGetWindowParam;
  TGetWindowParam = record
    ProcID: DWORD;
    WindowCaption: string;
    Result: HWND;
  end;

function DigForHandle(const ProcName, Caption: string; const Hierachy: array of string): HWND;
function FindPID(const ExeFileName: string): DWORD;

implementation

function FindPID(const ExeFileName: string): DWORD;
var
  ContinueLoop: BOOL;
  ProcessEntry32: TProcessEntry32;
  SnapshotHandle: THandle;
  TempExeFileName: string;
begin
  Result := 0;
  SnapshotHandle := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  if SnapshotHandle <> 0 then
  begin
    FillChar(ProcessEntry32, SizeOf(ProcessEntry32), 0);
    ProcessEntry32.dwSize := Sizeof(ProcessEntry32);
    ContinueLoop := Process32First(SnapshotHandle, ProcessEntry32);
    while ContinueLoop do
    begin
      TempExeFileName := ExtractFileName(ProcessEntry32.szExeFile);
      if SameText(TempExeFileName, ExeFileName) then
      begin
        Result := ProcessEntry32.th32ProcessID;
        Break;
      end;

      ContinueLoop := Process32Next(SnapshotHandle, ProcessEntry32);
    end;
    CloseHandle(SnapshotHandle);
  end;
end;

function GetWindow(Wnd: HWND; P: LParam): BOOL; stdcall;
var
  Param: PGetWindowParam;
  ProcID: DWORD;
  WindowTitle: array[0..256] of Char;
begin
  Result := True; // assume it doesn't match; keep searching
  Param := PGetWindowParam(P);

  ProcID := 0;
  GetWindowThreadProcessID(Wnd, @ProcID);
  if ProcID <> Param^.ProcID then
    Exit;

  FillChar(WindowTitle, SizeOf(WindowTitle), 0);
  if SendMessage(Wnd, WM_GETTEXT, SizeOf(WindowTitle) - SizeOf(Char), LPARAM(@WindowTitle[0])) <= 0 then
    Exit;

  if AnsiSameStr(WindowTitle, Param^.WindowCaption) then
  begin
    Param^.Result := Wnd;
    Result := False;
  end;
end;

function DigForHandle(const ProcName, Caption: string; const Hierachy: array of string): HWND;
var
  Param: TGetWindowParam;
  I: Integer;
  ParentWnd: HWND;
begin
  Result := 0;

  FillChar(Param, SizeOf(Param), 0);
  Param.ProcID := FindPID(ProcName);
  if Param.ProcID = 0 then
    Exit;

  Param.Result := 0;
  Param.WindowCaption := Caption;
  EnumWindows(@GetWindow, LPARAM(@Param));
  if Param.Result = 0 then
    Exit;

  I := 0;
  ParentWnd := Param.Result;
  while (ParentWnd <> 0) and (I < Length(Hierachy)) do
  begin
    Param.Result := 0;
    Param.WindowCaption := Hierachy[I];
    EnumChildWindows(ParentWnd, @GetWindow, LPARAM(@Param));
    if Param.Result = 0 then
      Break;
    ParentWnd := Param.Result;
    Inc(I);
  end;

  if I >= Length(Hierachy) then
    Result := Param.Result;
end;


When I thought about it, I realized that it was actually rather simple - the code I had though, was "confusing" for me, which was why I asked a question here. After trying it out, I found that doing it this way, its a lot easier to read, and not as complicated (IMO).

Function DigForHandle(ClassHierachy, TextHierachy : Array of String):HWND;
Var
 Handle : HWND;
 I : Integer;
 PClass,PText : PChar;

Begin

 Result := 0;

 I := 0;
 while (I <= Length(ClassHierachy)-1) do
 begin
   PClass := PChar(ClassHierachy[I]);
   PText  := PChar(TextHierachy[I]);

   if PClass = '' then PClass := Nil;
   if PText = '' then PText := Nil;


   Result := FindWindowEx(Result,0,PClass,PText);

   Inc(I);
 end;



End;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜