delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback



Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi








Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (2)


Technique for handling mouse actionsGo to William Egge's websiteFormat this article printer-friendly!Bookmark function is only available for registered users!
This is used when you need to do different actions in response to the mouse, such as in a paint program.
Product:
Delphi all versions
Category:
GUI
Skill Level:
Scoring:
Last Update:
02/16/2002
Search Keys:
delphi delphi3000 article borland vcl code-snippet mouse paint 3d editor handle form action
Times Scored:
3
Visits:
3634
Uploader: William Egge
Company: Eggcentric
Reference: N/A
 
Question/Problem/Abstract:
For example a paint program requires that the mouse do different things depending on what mode you are in, or maybe you have built a 3D world editor or vector editor. Want a clean way to handle all those mouse actions without cluttering your form code?
Answer:



The answer to the problem is to create mouse handler classes which your form passes mouse actions to it - or even key strokes.
A mouse handler class would look like this. You can add key events of other events if you like but I'll keep this simple.

TMouseHandler = class
  procedure MouseDown(Sender: TObject; Button: TMouseButton;
    Shift: TShiftState; X, Y: Integer); virtual; abstract;
  procedure MouseMove(Sender: TObject; Shift: TShiftState; X,
    Y: Integer); virtual; abstract;
  procedure MouseUp(Sender: TObject; Button: TMouseButton;
    Shift: TShiftState; X, Y: Integer); virtual; abstract;
end;

Then you would make descendents of this class to handle diffent "modes" for example:

TDrawLine = class(TMouseHandler) ... ;
TPaint = class(TMouseHandler) ... ;
TDrawSquare = class(TMouseHandler) ... ;
etc...

You do not have to apply this to just a paint program, the teqnique can be applied to any app where the mouse must do different things.

The mouse events of the control will be forwarded to the current mouse handler.

For example:
procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  Handler.MouseDown(Sender, Button, Shift, X, Y)
end;

procedure TForm1.Image1MouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  Handler.MouseMove(Sender, Shift, X, Y)
end;

procedure TForm1.Image1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  Handler.MouseUp(Sender, Button, Shift, X, Y);
end;

I'll make a note here that you may also want to include the ability for the handler to paint. For example when drawing a line you may want to display a line as the mouse moves.


When it is time to switch modes, simply reassign the "Handler" variable with a different type instance and your main form code is kept clean.






Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
An example please !
    Pierre Mallet (Oct 9 2002 3:47PM)

This is, to my opinion, a good idea to clarify mouse events management.
But I can't apply your idea : I can't make the connection between the specialized mouse handler "Handler" and the procedure that process the mouse events in that case.
Could you write a (small) example ?
Much thanks in advance.

                                                  Pierre MALLET
Respond

RE: An example please !
William Egge (Oct 10 2002 2:18AM)

Hi,

I do not have time to create a functional demo but maybe I can explain in this email.

The solution is to create a new class that implements the mouse events.  Within that class, you can implement the needed functionality.

For example:

TMyHandler = class
procedure MouseDown... etc.
  ..

end;

In your form you would have an instance of your handler

TMyForm
.
.
.
  FMyHandler: TMyHandler;
.
.
end;

within your regular events you should call your handlers mouse methods.

But to make it powerfull as it should be, you need to make an abstract mouse handler and then create various descendents of that mouse handler to handle various tasks.

TAbstractHandler = class
  proceddure MouseDown... virtual; abstract;
  etc.
end;

TDrawLine = class(TAbstractHandler)
  // override methods
end;

TDrawCircle = class(TAbstractHandler)
  // override methods
end;

etc

and then in your form, you would have this:

TMyForm
.
.
.
  FMyHandler: TAbstractHandler;
.
.
end;

Somewhere in your code you would handle creating the appropriet MouseHandler.

Don't forget to free the old one though.  And don't forget to free it on the destroy of your form.


Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


   


  Community Ad of
D. Wischnewski
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)