Oft gefragt, hier die Antwort:
Delphi-Quellcode:
procedure ClickBtnByID(hParent: THandle; BtnID: DWORD);
var
hBtn : THandle;
begin
if hParent <> 0 then
begin
SetActiveWindow(hParent);
hBtn := GetDlgItem(hParent, BtnID);
if hBtn <> 0 then
begin
SendMessage(hBtn, BM_CLICK, 0, 0);
end;
end;
end;
Aufruf zum Beispiel so:
Delphi-Quellcode:
var
hMsgBox : THandle;
begin
hMsgBox := FindWindow(nil, 'The lazy dog');
if hMsgBox <> 0 then
begin
ClickBtnByID(hMsgBox, MSG_NO);
end
else
Writeln('Fenster nicht gefunden');
Readln;
end.
Und gleich noch ein paar ID-Konstanten für die MessageBox Schaltflächen:
Delphi-Quellcode:
const
MSG_OK = $00000001;
MSG_CANCEL = $00000002; // = MSG_OK if OK button only
MSG_YES = $00000006;
MSG_NO = $00000007;