[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

simulate a click on component

Posted on 1999-10-27
9
Medium Priority
?
403 Views
Last Modified: 2011-10-30
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
0
Comment
Question by:feup
9 Comments
 
LVL 6

Expert Comment

by:edey
ID: 2163720
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
 

Author Comment

by:feup
ID: 2163734
I can know the handle of the window, but isn't more important the handle of the component?
0
 
LVL 6

Expert Comment

by:edey
ID: 2163785
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 1

Expert Comment

by:jpcs
ID: 2163932
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
 
LVL 17

Expert Comment

by:inthe
ID: 2163960
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
 
LVL 1

Accepted Solution

by:
Roadrunner100598 earned 600 total points
ID: 2184480
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
 
LVL 1

Expert Comment

by:men xin
ID: 2211337
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
 

Author Comment

by:feup
ID: 2291514
hi!

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

thanks
0
 

Expert Comment

by:Webcamconnects
ID: 37053178
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
This is an update to some code that someone else posted on Experts Exchange. It is an alternate approach, I think a little easier to use, & makes sure that things like the Task Bar will update.
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
Is your organization moving toward a cloud and mobile-first environment? In this transition, your IT department will encounter many challenges, such as navigating how to: Deploy new applications and services to a growing team Accommodate employee…
Suggested Courses

613 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