Solved

Detecting mouse event everywhere

Posted on 2006-06-15
4
342 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

718 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