Simulate Mouse Click Within Your Program Area Form

Posted on 2005-04-13
Last Modified: 2010-04-05
hi, I need to simulate a mouse click at a certain point within the form area.

how do I do that?

with doing it from the desktop the code is
mouse_event(MOUSEEVENTF_LEFTDOWN, 100,100, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, 100,100, 0, 0);

but the thing with that is you move the window, you got to change the x/y, if the x/y was within the program form area, then the program can be hidden or moved and it can still be clicked.

anyone know how I can do this?

Question by:SaLz
    LVL 17

    Accepted Solution


    The following works if you move the form (I don't think mouse clicks could be simulated if the form is hidden/minimized):

    procedure TForm1.ClickAt(x,y: integer);
      p: TPoint;
      // move the pointer
      p := Point(x,y);
      p := self.ClientToScreen(p);
      SetCursorPos(p.x, p.y);
      // click
      mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
      mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);

    procedure TForm1.Button1Click(Sender: TObject);
      ClickAt(10,10); // relative coordinates in Form1 client area

    Regards, Geo
    LVL 12

    Expert Comment

    LVL 11

    Expert Comment

    SendMessage(handle, wm_lbuttondown, 0, MakeLParam(40, 30));
    SendMessage(handle, wm_lbuttonup  , 0, MakeLParam(40, 30));

    40 is X coordinate, 30 - Y
    LVL 1

    Expert Comment

    I don't understand the question, if the above doesn't answer it then can you please try to explain more clearly what you are trying to do?

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
    In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    25 Experts available now in Live!

    Get 1:1 Help Now