[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 358
  • Last Modified:

Detecting mouse event everywhere

How can I detect mouse event everywhere on the form, that contains listboxes, labels, panels... For example, i start OnMouseMove event on ListBox with left button pressed and I drag mouse outside of the ListBox and then I want released left button to be detected. Simply said, how to do it, that whole application monitors mouse events?
0
JackKuti
Asked:
JackKuti
  • 3
1 Solution
 
TheRealLokiSenior DeveloperCommented:
Normally you'd have to use a windows hook (SetWindowsHookEx() API.) and put your code in a .DLL, but there is a trick using JOURNAL
http://www.delphitricks.com/source-code/windows/trap_mouse_events_outside_of_my_application.html
0
 
TheRealLokiSenior DeveloperCommented:
oops, didn't read your question correctly. This is probably more than you needed
0
 
kfoster11CEOCommented:
Look at the Forms property TForm.WantEvents.  This should send all of the events from the child control to your form.  
0
 
TheRealLokiSenior DeveloperCommented:
hmm, i'm not familiar with the WantEvents
here's another way of doing it though

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    ListBox1: TListBox;
{ these events are hooked up to the form's mousemove, mouseup, and mousedown}
    procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
    procedure FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure FormMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);

{ these events are hooked up to _every_ wincontrol you are interested in keeping track of
  simply set each wincontrol's "OnMouseMove,OnMOuseUp, and OnMouseDown" events to point to these 3}
    procedure ControlAdjustment_MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
    procedure ControlAdjustment_MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure ControlAdjustment_MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  private
    { Private declarations }
{  these events are the actual MouseMove, MouseUp, and MouseDown including the controls you are over.
   these events are the ones you should use (e.g. if you wish to move controls around etc) }
    procedure WholeForm_MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
    procedure WholeForm_MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure WholeForm_MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  public
    { Public declarations }
  end;


var
  Form1: TForm1;

implementation

{$R *.dfm}

// these events are hooked up to the form's mousemove, mouseup, and mousedown
procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
    begin
        WholeForm_MouseMove(Sender, Shift, X, Y);
    end;

procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    begin
        WholeForm_MouseDown(Sender, Button, Shift, X, Y);
    end;

procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    begin
        WholeForm_MouseUp(Sender, Button, Shift, X, Y);
    end;

{ these events are hooked up to _every_ wincontrol you are interested in keeping track of
  simply set each wincontrol's "OnMouseMove,OnMOuseUp, and OnMouseDown" events to point to these 3}
procedure TForm1.ControlAdjustment_MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
    begin
        WholeForm_MouseMove(Sender, Shift, (Sender as TWinControl).Left + X, (Sender as TWinControl).Top + Y);
    end;

procedure TForm1.ControlAdjustment_MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    begin
        WholeForm_MouseDown(Sender, Button, Shift, (Sender as TWinControl).Left + X, (Sender as TWinControl).Top + Y);
    end;

procedure TForm1.ControlAdjustment_MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    begin
        WholeForm_MouseUp(Sender, Button, Shift, (Sender as TWinControl).Left + X, (Sender as TWinControl).Top + Y);
    end;

{  these events are the actual MouseMove, MouseUp, and MouseDown including the TWincontrols you are over.
   these events are the ones you should use (e.g. if you wish to move controls around etc) }
procedure TForm1.WholeForm_MouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
    begin
        caption := format('x = %d, y = %d', [x,y]);
        listbox1.items.add(format('x = %d, y = %d', [x,y]));
    end;

procedure TForm1.WholeForm_MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    begin
        caption := format('x = %d, y = %d, mouse down', [x,y]);
        listbox1.items.add(format('x = %d, y = %d, mouse down', [x,y]));
    end;

procedure TForm1.WholeForm_MouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    begin
        caption := format('x = %d, y = %d, mouse up', [x,y]);
        listbox1.items.add(format('x = %d, y = %d, mouse up', [x,y]));
    end;

end.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now