Description
This example illustrates how to press the left
button of the mouse cyclically on each control contained
on Form1 through software.
This is achieve with a loop and the SendMessage function which
sends the WM_LBUTTONDOWN message to the control
specified by the corresponding handle (Control->Handle)
SendMessage(pControl->Handle, WM_LBUTTONDOWN , 0, 0);
A delay of 1 Second is intoduced between iterations with the call
Sleep(1000);
Form1 has the following components: a TTimer, a TEdit,
a TMemo and four TButtons.
Files
SendMessageLBUTTONDOWN.cpp
#include <vcl.h>
#pragma hdrstop
#include "SendMessageLBUTTONDOWN.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
// Scan all components derived from TWinControl
// on Form1 and click on each component with the
// Left Button of the mouse by sending the WM_LBUTTONDOWN
// Windows message.
HWND hwnd;
if((hwnd = FindWindow(NULL, "Form1")))
{
//Loop though all the controls on Form1
//
for( int i=0; i < this->ControlCount; i++ )
{
// Get the pointer to the next control on Form1
TWinControl* pControl = dynamic_cast <TWinControl*>(this->Controls[i]);
//Send Windows Message WM_LBUTTONDOWN
SendMessage(pControl->Handle, WM_LBUTTONDOWN , 0, 0);
// Pause for 1 second
Sleep(1000);
}
}
else
{
ShowMessage("Error: Cannot find the window caption");
}
}
//---------------------------------------------------------------------------
SendMessageLBUTTONDOWN.h
#ifndef SendMessageLBUTTONDOWNH
#define SendMessageLBUTTONDOWNH
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <ExtCtrls.hpp>
#include <Buttons.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TTimer *Timer1;
TEdit *Edit1;
TMemo *Memo1;
TButton *Button1;
TButton *Button2;
TButton *Button3;
TButton *Button4;
void __fastcall Timer1Timer(TObject *Sender);
private: // User declarations
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
Copyright © 1997-2003 Rodolfo A. Frino. All rights reserved.