在我的 GUI 应用程序中,我运行控制台应用程序并需要处理其窗口。我尝试使用 EnumWindows(),请参见下面的代码,但它不起作用。列表中没有我的控制台应用程序。
type
TEnumWindowsData = record
ProcessId: Cardinal;
WinHandle: THandle;
List: TStrings; // For test only
end;
PEnumWindowsData = ^TEnumWindowsData;
function FindWindow(hWnd: THandle; lParam: LPARAM): BOOL; stdcall;
var
ParamData: TEnumWindowsData;
ProcessId: Cardinal;
WinTitle: array[0..200] of Char; // For test only
begin
ParamData := PEnumWindowsData(lParam)^;
GetWindowThreadProcessId(hWnd, ProcessId);
if ProcessId <> ParamData.ProcessId then
Result := True
else begin
ParamData.WinHandle := hWnd;
Result := False;
end;
// For test only
GetWindowText(hWnd, WinTitle, Length(WinTitle) - 1);
ParamData.List.Add(IntToStr(ProcessId) + ' ' + IntToStr(hWnd) + ' ' + WinTitle);
end;
procedure TForm1.Button1Click(Sender: TObject);
function RunApp(const AProgram: string): Cardinal;
var
StartupInfo: TStartupInfo;
ProcessInformation: TProcessInformation;
begin
Result := 0;
...
if CreateProcess(nil, PChar(AProgram), nil, nil, False,
NORMAL_PRIORITY_CLASS, nil, nil, StartupInfo, ProcessInformation)
then
Result := ProcessInformation.dwProcessId;
...
end;
var
ParamData: TEnumWindowsData;
begin
ParamData.ProcessId := RunApp('cmd.exe /C D:\TMP\TEST.exe');
ParamData.WinHandle := 0;
ParamData.List := Memo1.Lines;
EnumWindows(@FindWindow, THandle(@ParamData));
FWindowHandle := ParamData.WinHandle;
end;