- - * - WhiteUnicorn - * - -




* #WhiteUnicorn/ StartPage/ Documentation/DelphiFAQ >


Frequently Asked Questions

How can I simulate a button click in code?

Question:

How can I simulate a button click in code?

Answer:

Given a Window handle, you can send the equivalent mouse message to
simulate a click to any Windows control. You should be cautious not to
send too many Windows messages at a time to avoid overflowing the
message buffer.

Example:

procedure PressAButton(h : THandle);
begin
  PostMessage(h, WM_LBUTTONDOWN, 0, 0);
  PostMessage(h, WM_LBUTTONUP, 0, 0);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  PressAButton(Button2.Handle);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  ShowMessage('Click');
end;



* #WhiteUnicorn/ StartPage/ Documentation/DelphiFAQ >



- - * - Anastasija aka WhiteUnicorn - * - - LJLiveJournal
PFPhotoFile