Главная страница | назад





Article #17313: How can I simulate a button click in code?

 Question and Answer Database

FAQ2313C.txt   How can I simulate a button click in code?
Category   :Windows API
Platform    :All
Product    :C++Builder  3.x

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:

void PressAButton(HWND h);

void TForm1::PressAButton(HWND h)
{
   PostMessage(h, WM_LBUTTONDOWN, 0 ,0);
   PostMessage(h, WM_LBUTTONUP, 0, 0);
}

void __fastcall TForm1::Button1Click(TObject *Sender)
{
   PressAButton(Button2->Handle);
}

void __fastcall TForm1::Button2Click(TObject *Sender)
{
   ShowMessage("CLICK");
}

7/2/98 10:32:32 AM
 

Last Modified: 01-SEP-99