1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44:
| procedure TForm1.Button1Click(Sender: TObject);
function EnumWindowsProc(hWnd: HWND; lParam: LPARAM): BOOL; stdcall; begin TList(lParam).Add(Pointer(hWnd)); Result := True; end;
var SI: TStartupInfo; PI: TProcessInformation; List: TList; I: Integer; AppHWnd: HWND; EditHWnd: HWND; ProcessId: DWORD; begin
FillChar(SI, SizeOf(TStartupInfo), 0); SI.cb := SizeOf(TStartupInfo); if CreateProcess(nil, 'notepad.exe', nil, nil, False, 0, nil, nil, SI, PI) then begin WaitForInputIdle(PI.hProcess, INFINITE); CloseHandle(PI.hProcess); CloseHandle(PI.hThread); List := TList.Create; try EnumWindows(@EnumWindowsProc, LPARAM(List)); for I := 0 to List.Count - 1 do if GetWindowThreadProcessId(HWND(List.Items[I]), @ProcessId) <> 0 then if ProcessId = PI.dwProcessId then begin AppHWnd := HWND(List.Items[I]); EditHWnd := FindWindowEx(AppHWnd, 0, 'Edit', nil); if IsWindow(AppHWnd) and IsWindow(EditHWnd) then SendMessage(EditHWnd, WM_SETTEXT, 0, LPARAM(PChar('Hallo Welt!' + #0))); Break; end; finally List.Free; end; end;
end; |