hDrummer
Дата: 06.03.2003 11:35:40
Using FindWindow won't necessarily get you a window that belongs to the
process you created. A more reliable method would be to use EnumWindows,
using the return value from GetWindowThreadProcessId to check for a match
with the values set in your TProcessInfo record above.
Try this:
type
PFindProcessWindowStruct = ^TFindProcessWindowStruct;
TFindProcessWindowStruct = record
Process: DWord;
Title: PChar;
Window: hWnd;
end;
function fpwCallback(wnd: hWnd; Param: lParam): Bool; stdcall;
var
fpws: PFindProcessWindowStruct;
ProcID: DWord;
len: Integer;
ptitle: PChar;
begin
Result := True;
fpws := PFindProcessWindowStruct(Param);
GetWindowThreadProcessId(wnd, @ProcID);
if ProcID = fpws.Process then begin
if Assigned(fpws.Title) then begin
len := GetWindowTextLength(wnd) + 1;
ptitle := StrAlloc(len);
try
GetWindowText(wnd, ptitle, len);
if StrComp(ptitle, fpws.Title) <> 0 then exit;
finally
StrDispose(ptitle);
end;
end;
Result := False;
fpws.Window := wnd;
end;
end;
// AProcess: The process ID for the window you're looking for
// ATitle: The title of the window you're looking for. Specify nil to search
for any name
// Result: Handle to the window or 0 if the window wasn't found.
function FindProcessWindow(const AProcess: DWord; const ATitle: PChar):
hWnd;
var
fpws: TFindProcessWindowStruct;
begin
fpws.Process := AProcess;
fpws.Title := ATitle;
fpws.Window := 0;
EnumWindows(fpwCallback, lParam(@fpws));
Result := fpws.Window;
end;
code by Rob Kennedy (rkennedy@cs.wisc.edu)