0

というわけで、Skype チャットボックス ハンドルの取得に関する質問への回答を得ました。

私は今、ハンドルを掘る単純な関数を作成しようとしています。これが私がそれを使用できることを望んでいる方法です:

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

パラメータ:

1) 文字列の配列: クラス階層を保持します。

2) 文字列の配列: ウィンドウ キャプション階層を保持します。

Editご覧のとおり、クラスにはウィンドウ キャプションがないため、2 番目のパラメーターの 2 番目のエントリは空です。

そのような機能を作成することは可能でしょうか? :)

4

2 に答える 2

0

これを試して

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;
于 2011-04-25T17:08:09.840 に答える
-1

考えてみると、実際にはかなり単純であることに気付きました。しかし、私が持っていたコードは「混乱」していたので、ここで質問しました。試してみたところ、このようにすると、読みやすく、それほど複雑ではないことがわかりました(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;
于 2011-04-28T08:27:18.913 に答える