simulate a click on component

Hi!

I want to simulate a leftmousedown and a leftmouseup event on some coordinates I specify.

P.E. : Make a mousedown on a tmemo on position 1,1 and the mouseup on 100, 1), so that it's made the selection.
Also must work with TIEWebBrowser, in a page with frames (click on option 1 or 2 in frame 1).

It must use *only* winAPI instructions and not other controls. I think it uses the processmessage or sendmessage instruction, but I can't find the way of using it.


Thanks
feupAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

edeyCommented:
see WM_LBUTTONDOWN in the win32.hlp file, essentialy you need to know the handle of the window you're "clicking" on, then try something like this

sendMessage(WM_LBUTTONDOWN,1+(1 shl 16),0);
sendMessage(WM_LBUTTONUP,100+(1 shl 16),0);

GL
Mike
0
feupAuthor Commented:
I can know the handle of the window, but isn't more important the handle of the component?
0
edeyCommented:
the component has to be a window if you want to send it messages.  This doesn't mean that it has to be a form, for any component that can get focus is a kind of window.  You just have to know it's handle, before sending it messages.

GL
Mike
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

jpcsCommented:
I tried the follwing that worked with the tmemo, but not with the tiewebbrowser. Why?

var lp : tmessage;
    wnd : hwnd;

begin
Lp.lparamHi := 1;
Lp.LparamLo := 1;

wnd := webbrowser1.Handle;
//wnd := memo1.Handle;

Sendmessage(wnd,WM_RBUTTONDOWN,0,LP.Lparam);
sleep (200);
Sendmessage(wnd,WM_RBUTTONUP,0,LP.Lparam);
end;

Note that I tried the rightbutton because of the coordinates, but it didn't show the menu.
0
intheCommented:
a Twebbrowser needs a message handler as well else it wont handle many messages.here is an unit i have that shows an example message handler ,just take out the relevent lines about vk_back etc..:


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, OleCtrls, SHDocVw_TLB,ActiveX;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Edit1: TEdit;
    WebBrowser1: TWebBrowser;
    procedure Button1Click(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure FormDeactivate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
  private
    { Private declarations }
  public
  procedure MyMessageHandler(var Msg: TMsg; var Handled: Boolean);

    { Public declarations }
  end;

var
  Form1: TForm1;
  FOleInPlaceActiveObject: IOleInPlaceActiveObject;
  SaveMessageHandler: TMessageEvent;


implementation

{$R *.DFM}

procedure TForm1.MyMessageHandler(var Msg: TMsg; var Handled: Boolean);
var
  iOIPAO: IOleInPlaceActiveObject;
  Dispatch: IDispatch;
begin
  { exit if we don't get back a webbrowser object }
  if WebBrowser1 = nil then
  begin
    Handled := False;
    Exit;
  end;

  Handled:=(IsDialogMessage(WebBrowser1.Handle, Msg) = True);

  if (Handled) and (not WebBrowser1.Busy) then
  begin
    if FOleInPlaceActiveObject = nil then
    begin
      Dispatch := WebBrowser1.Application_;
      if Dispatch <> nil then
      begin
        Dispatch.QueryInterface(IOleInPlaceActiveObject, iOIPAO);
        if iOIPAO <> nil then
          FOleInPlaceActiveObject := iOIPAO;
      end;
    end;

    if FOleInPlaceActiveObject <> nil then
      if ((Msg.message = WM_KEYDOWN) or (Msg.message = WM_KEYUP)) and
         ((Msg.wParam = VK_BACK) or (Msg.wParam = VK_LEFT) or (Msg.wParam = VK_RIGHT)) then
        //nothing - do not pass on Backspace, Left or Right arrows
      else
        FOleInPlaceActiveObject.TranslateAccelerator(Msg);
  end;
end;

procedure TForm1.FormActivate(Sender: TObject);
begin
  SaveMessageHandler := Application.OnMessage;
  Application.OnMessage := MyMessageHandler;

end;

procedure TForm1.FormDeactivate(Sender: TObject);
begin
Application.OnMessage := SaveMessageHandler;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Application.OnMessage := SaveMessageHandler;
  FOleInPlaceActiveObject := nil;
end;

procedure TForm1.Button1Click(Sender: TObject);
var a,b,c,d:OleVariant;
begin
     WebBrowser1.Navigate(Edit1.Text,a,b,c,d);
end;

initialization
  OleInitialize(nil);

finalization
  OleUninitialize;

end.



Regards Barry
0
Roadrunner100598Commented:
This should work with any program:

procedure TForm1.Button1Click(Sender: TObject);
var
  LParam : longint;
  i      : integer;
  p      : TPoint;
  Wnd    : HWnd;
begin
  for i:=1 to 10 do begin
    p.x:=50+Random(100);
    p.y:=50+Random(100);
    // Make mouse cursor follow our clicks, this is actually not needed
    // and looks strange
    SetCursorPos(p.x,p.y);
    LParam:=p.y*65536+p.x;
    // Find the window the cursor is over
    Wnd:=WindowFromPoint(p);
    // Post a WM_LButtonDown to that window
    PostMessage(Wnd, WM_LButtonDown, 0 {WParam}, LParam);
    sleep(100);
    p.x:=p.x+10;
    p.y:=p.y+10;
    SetCursorPos(p.x,p.y);
    LParam:=p.y*65536+p.x;
    // Post a WM_LButtonUp to the same window
    PostMessage(Wnd, WM_LButtonUp  , 0 {WParam}, LParam);
    sleep(100);
  end;
end;

I checked with Paint (that's nice), Explorer, Delphi, Netscape Navigator...

Maybe you would also like to call 'BringWindowToTop(Wnd)' before sending the mousedown.


Beep beep, Roadrunner
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
men xinCEOCommented:
U can use this way too.

var LTP:tpoint;
begin
  LTP:=Form1.ClientToScreen(Point(Memo1.Left,Memo1.Top));
  SetCursorPos(LTP.x+2,LTP.y+2);
  mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);
  mouse_event(MOUSEEVENTF_MOVE,40,40,0,0);
  mouse_event(MOUSEEVENTF_LEFTUP,0,0,0,0);
end;

the mouse cursor can select text.
0
feupAuthor Commented:
hi!

Just one question... is there any way to do this WITHOUT moving the mouse cursor ober the component?

thanks
0
WebcamconnectsCommented:
i would like to know the answer to this also? how to do this without have the pointer moved so its a background click
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.