Solved

Detecting mouse event everywhere

Posted on 2006-06-15
4
324 Views
Last Modified: 2010-04-05
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
Comment
Question by:JackKuti
  • 3
4 Comments
 
LVL 17

Expert Comment

by:TheRealLoki
ID: 16915734
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
 
LVL 17

Expert Comment

by:TheRealLoki
ID: 16915752
oops, didn't read your question correctly. This is probably more than you needed
0
 
LVL 7

Expert Comment

by:kfoster11
ID: 16915968
Look at the Forms property TForm.WantEvents.  This should send all of the events from the child control to your form.  
0
 
LVL 17

Accepted Solution

by:
TheRealLoki earned 125 total points
ID: 16916095
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now