Link to home
Start Free TrialLog in
Avatar of feup
feup

asked on

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
Avatar of edey
edey

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
Avatar of feup

ASKER

I can know the handle of the window, but isn't more important the handle of the component?
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
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.
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
ASKER CERTIFIED SOLUTION
Avatar of Roadrunner100598
Roadrunner100598

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Avatar of feup

ASKER

hi!

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

thanks
i would like to know the answer to this also? how to do this without have the pointer moved so its a background click