const sWinFinkExe :
string = '
WINFx4.exe';
var frm_main : Tfrm_main;
extWindowsHandle : THandle;
...
function GetWindowThreadProcessId(hWnd: HWND;
var dwProcessId: DWORD): DWORD;
stdcall;
external '
user32.dll'
name '
GetWindowThreadProcessId';
function MyEnumWindowProc(AHandle: THandle; LParam: LongWord): boolean;
stdcall;
var ProcessID: THandle;
begin
ProcessID := 0;
GetWindowThreadProcessID(AHandle, ProcessID);
Result :=
not (ProcessID = LParam);
if not Result
then extWindowsHandle := AHandle;
end;
function GetWindowHandleByExeName(
const AExeName:
string): THandle;
var SnapShot: THandle;
p: TProcessEntry32;
ProcessHandle: THandle;
begin
extWindowsHandle := 0;
ProcessHandle := 0;
p.dwSize := SizeOf(p);
SnapShot := CreateToolhelp32Snapshot(TH32CS_SnapProcess, 0);
try
if Process32First(SnapShot, p)
then
repeat
if AnsiLowerCase(AExeName) = AnsiLowerCase(p.szExeFile)
then
ProcessHandle := p.th32ProcessID;
until (ProcessHandle <> 0)
or not Process32Next(SnapShot, p);
EnumWindows(@MyEnumWindowProc, ProcessHandle);
Result := extWindowsHandle;
finally
CloseHandle(SnapShot);
end;
end;
...
procedure Tfrm_main.btn_sendKeyClick(Sender: TObject);
var iSendKeyCode : integer;
begin
extWindowsHandle := GetWindowHandleByExeName(sWinFx4Exe);
iSendKeyCode:= 68;
SendMessage (extWindowsHandle,
// Handle des externen Programmes
WM_CHAR,
// Übergabe = Keycode
68,
// KeyCode von "ALT+D" (DateiMenü)
0
// Dritter Parameter
);
end;