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





Article #16241: Using mouse_event() to similate mouse events

 Question and Answer Database

FAQ1241D.txt   Using mouse_event() to similate mouse events
Category   :Windows API
Platform    :All
Product    :All 32 bit  

Question:
How do I use the mouse_event() procedure to simulate a mouse
 event?

Answer:
The following example demonstrates using the Windows API 
function mouse_event() to simulate a mouse event. When Button2 
is clicked, the example code places the mouse over Button1 and 
clicks it. Mouse Coordinates given are in "Mickeys", where 
their are 65535 "Mickeys" to a screen's width.

procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowMessage('Button 1 clicked');
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  Pt : TPoint;
begin
 {Allow Button2 to repaint it's self}
  Application.ProcessMessages;
 {Get the point in the center of button 1}
  Pt.x := Button1.Left + (Button1.Width div 2);
  Pt.y := Button1.Top + (Button1.Height div 2);
 {Convert Pt to screen coordinates}
  Pt := ClientToScreen(Pt);
 {Convert Pt to mickeys}
  Pt.x := Round(Pt.x * (65535 / Screen.Width));
  Pt.y := Round(Pt.y * (65535 / Screen.Height));
 {Move the mouse}
  Mouse_Event(MOUSEEVENTF_ABSOLUTE or
              MOUSEEVENTF_MOVE,
              Pt.x,
              Pt.y,
              0,
              0);
 {Simulate the left mouse button down}
  Mouse_Event(MOUSEEVENTF_ABSOLUTE or
              MOUSEEVENTF_LEFTDOWN,
              Pt.x,
              Pt.y,
              0,
              0);;
 {Simulate the left mouse button up}
  Mouse_Event(MOUSEEVENTF_ABSOLUTE or
              MOUSEEVENTF_LEFTUP,
              Pt.x,
              Pt.y,
              0,
              0);;
end;

7/16/98 4:31:28 PM
 

Last Modified: 01-SEP-99