Solved

Detecting mouse event everywhere

Posted on 2006-06-15
4
330 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

Gigs: Get Your Project Delivered by an Expert

Select from 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.

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…
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…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

813 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

11 Experts available now in Live!

Get 1:1 Help Now