我尝试在 Lazarus (Free Pascal) 中使用以下代码获取主窗口句柄:
function FindMainWindow(Pid: LongWord): LongWord;
type
TParam = record
Window: HWnd;
Test: Integer;
Pid: LongWord;
end;
PParam = ^TParam;
var
Params: TParam;
function _FindMainWindow(Wnd: HWnd; MyLParam: PParam): Bool; stdcall;
var
WinPid: DWord;
begin
with MyLParam^ do
begin
Test := 2;
GetWindowThreadProcessID(Wnd, @WinPid);
Result := (WinPid <> Pid) or (not IsWindowVisible(Wnd))
or (not IsWindowEnabled(Wnd));
if not Result then begin
Window := Wnd;
end;
end;
end;
begin
Params.Pid := Pid;
Params.Test := 1;
EnumWindows(@_FindMainWindow, LParam(@Params));
ShowMessage('Done!');
ShowMessage(IntToStr(Params.Test));
Result := Params.Window;
end;
问题是Params.Test
运行回调后仍然为 1。我想修改函数Params
中的_FindMainWindow
。
注意:我无法直接访问Params
,_FindMainWindow
因为我收到“访问冲突”错误。