Re: programmatically creating mouse clicks

From: Terry Russell (trochilus_at_optusnet.com.au)
Date: 01/12/04

  • Next message: Andrew Rybenkov: "Re: programmatically creating mouse clicks"
    Date: Mon, 12 Jan 2004 17:14:45 +1030
    
    

    "David Novo" <dave@xxx.denovosoftware.com> wrote in message
    news:40021786$1@newsgroups.borland.com...
    > Hello,
    >
    > I needs to create a mouse click. I have found many threads talking about
    > either doing button.click on sending WM_LMOUSEDOWN.
    >
    > However, that is not what I need. I am writing a program that is doing a
    > demo for a user. I move the mouse with the WINAPI SETCURSORPOS to where I
    > want (in this case a button). If I send a WM_LMOUSEDOWN message to the
    > button the code executes properly, but the button does not graphically
    > press down as it does when I really click on it with the mouse. Is there
    > any way to actually send a message that actually clicks the button down?

    You must realise, there is no spoon

    procedure slidemouse(p:tpoint);
    // animate mouse cursor to a point
    // could be smoother
    var m:tpoint;
    begin
      m:=mouse.cursorpos;
      repeat
         if m.x > p.x then m.x:=m.x-1;
         if m.x < p.x then m.x:=m.x+1;
         if m.y > p.y then m.y:=m.y-1;
         if m.y < p.y then m.y:=m.y+1;
         mouse.cursorpos:=m;
         sleep(0);
         application.processmessages;
      until (m.x=p.x) and (m.y=p.y);
    end;

    var h:thandle; // victim window handle
    var classname:array[1..128] of char;
    var i:integer;
    var oldc:tpoint;
    var newc:trect;
    var oldfg:thandle;

     // here bring victim to front so viewer can
    // see it , and check it really is a button
     res:=getclassname(h,@classname,127);
     if res <> 0 then
     begin
       if strpas(@classname)='Button' then // or Tbutton
       begin
        oldfg:=getforegroundwindow;
        setforegroundwindow(h);
        oldc:=mouse.CursorPos;
        getwindowrect(h,newc);
        slidemouse(newc.TopLeft);
        for i:=1 to 10 do
        begin // flash a few times..doesn't click
          sendmessage(h,BM_SETSTATE,1,0);
          sleep(150);
          sendmessage(h,BM_SETSTATE,0,0);
          sleep(150)
        end;
        // now since we want to actually click, not just
        // show where it is
        // click button direct..not by mouse, mouse position irrelevant
        // same as ldown/lup
        sendmessage(h,BM_CLICK,0,0);
        setforegroundwindow(oldfg);
        slidemouse(oldc);
       end;
     end;

    now , if you are moving the mouse you should do something to
    make sure they don't click unwanted things ,
    change focus, activate untoward events etc
    that is another problem


  • Next message: Andrew Rybenkov: "Re: programmatically creating mouse clicks"
  • Quantcast