Solved

Simple mouse tracking in Delphi

Posted on 2012-04-02
2
617 Views
Last Modified: 2012-04-03
Hi there I am trying to make a simple mouse tracking program in Delphi, what I want to do it increase a counter every time the mouse moves over 2 points on the screen while going across a narrow "bar" in between the 2 blocks, but I only want it to add to the counter if the mouse stays on the bar and gets to the other block and I want to reset the counter if the mouse leaves the bar or blocks. So I have this code so far:
interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls;

type
  TfrmWeightlifting = class(TForm)
    pnlLeftWeight: TPanel;
    pnlRightWeight: TPanel;
    pnlBar: TPanel;
    procedure pnlLeftWeightMouseMove(Sen<wbr ></wbr>der: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure pnlRightWeightMouseMove(Se<wbr ></wbr>nder: TObject; Shift: TShiftState;
      X, Y: Integer);
    procedure pnlBarMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  frmWeightlifting: TfrmWeightlifting;

implementation

{$R *.dfm}

procedure TfrmWeightlifting.pnlLeftW<wbr ></wbr>eightMouse<wbr ></wbr>Move(Sende<wbr ></wbr>r: TObject; Shift: TShiftState; X,
  Y: Integer);
var
  Count : integer;
begin
  Count := 0;
  pnlLeftWeight.Color := clblack;
  pnlRightWeight.Color := clBtnFace;
  if FormMouseMove(Sender: TObject;
  Shift: TShiftState; X, Y: Integer) = true then
    Count := 0;
  else
    Count := Count + 1;
end;

procedure TfrmWeightlifting.pnlRight<wbr ></wbr>WeightMous<wbr ></wbr>eMove(Send<wbr ></wbr>er: TObject;
  Shift: TShiftState; X, Y: Integer);
begin
  pnlRightWeight.Color := clblack;
  pnlLeftWeight.Color := clBtnFace;
end;

procedure TfrmWeightlifting.pnlBarMo<wbr ></wbr>useMove(Se<wbr ></wbr>nder: TObject;
  Shift: TShiftState; X, Y: Integer);
begin
  pnlBar.Color := clblack;
  pnlLeftWeight.Color := clBtnFace;
  pnlRightWeight.Color := clBtnFace;
end;

procedure TfrmWeightlifting.FormMous<wbr ></wbr>eMove(Send<wbr ></wbr>er: TObject;
  Shift: TShiftState; X, Y: Integer);
begin
  pnlRightWeight.Color := clBtnFace;
  pnlLeftWeight.Color := clBtnFace;
  pnlBar.Color := clBtnFace;
end;

end.

Open in new window


I am getting an error on the if statement
if FormMouseMove(Sender: TObject;
  Shift: TShiftState; X, Y: Integer) = true then

I know that's wrong, what I'm trying to do is evaluate whether the mouse moves anywhere on the form besides the left block, bar or right block. What would be the correct statement for what I am trying to achieve?
0
Comment
Question by:Dmon443
2 Comments
 
LVL 35

Accepted Solution

by:
mccarl earned 500 total points
ID: 37799629
It's a long time since I have looked at Delphi, (and that also means that I can't actually test the code below) but it may at least give you an idea on how to proceed...

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtCtrls;

type
  TfrmWeightlifting = class(TForm)
  Count : integer;
  LastVisitedLeftPanel: boolean;
  OutOfBounds: boolean;

    pnlLeftWeight: TPanel;
    pnlRightWeight: TPanel;
    pnlBar: TPanel;
    procedure pnlLeftWeightMouseMove(Sen<wbr ></wbr>der: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure pnlRightWeightMouseMove(Se<wbr ></wbr>nder: TObject; Shift: TShiftState;
      X, Y: Integer);
    procedure pnlBarMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
  private
    { Private declarations }
  public
    { Public declarations }
    constructor Create();
  end;

var
  frmWeightlifting: TfrmWeightlifting;

implementation

{$R *.dfm}

constrcutor TfrmWeightlifting.Create();
begin
   Count := 0;
   OutOfBounds := true;
end;

procedure TfrmWeightlifting.pnlLeftW<wbr ></wbr>eightMouse<wbr ></wbr>Move(Sende<wbr ></wbr>r: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  pnlLeftWeight.Color := clblack;
  pnlRightWeight.Color := clBtnFace;
  if not OutOfBounds and not LastVisitedLeftPanel then
    Count := Count + 1;
  
  OutOfBounds := false;
  LastVisitedLeftPanel := true;
end;

procedure TfrmWeightlifting.pnlRight<wbr ></wbr>WeightMous<wbr ></wbr>eMove(Send<wbr ></wbr>er: TObject;
  Shift: TShiftState; X, Y: Integer);
begin
  pnlRightWeight.Color := clblack;
  pnlLeftWeight.Color := clBtnFace;
  if not OutOfBounds and LastVisitedLeftPanel then
    Count := Count + 1;
  
  OutOfBounds := false;
  LastVisitedLeftPanel := false;
end;

procedure TfrmWeightlifting.pnlBarMo<wbr ></wbr>useMove(Se<wbr ></wbr>nder: TObject;
  Shift: TShiftState; X, Y: Integer);
begin
  pnlBar.Color := clblack;
  pnlLeftWeight.Color := clBtnFace;
  pnlRightWeight.Color := clBtnFace;
end;

procedure TfrmWeightlifting.FormMous<wbr ></wbr>eMove(Send<wbr ></wbr>er: TObject;
  Shift: TShiftState; X, Y: Integer);
begin
  pnlRightWeight.Color := clBtnFace;
  pnlLeftWeight.Color := clBtnFace;
  pnlBar.Color := clBtnFace;

  OutOfBounds := true;
  Count := 0;
end;

end.

Open in new window


The above is assuming the in Delphi the MouseMove event when over one of the objects, DOESN'T bubble up to the FormMouseMove handler as well. But try it and see how you go...
0
 

Author Closing Comment

by:Dmon443
ID: 37800337
A few mistakes like the variables were declared in the wrong place, need to be declared after the type variables, not inside but the logic was right and I got it to work with the help provided here.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupNoAdj 7 95
java  and programming certification ? 4 93
Looking for example pivot year code used in Y2K 4 68
Scripting vs. Programming languages 25 165
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Displaying an arrayList in a listView using the default adapter is rarely the best solution. To get full control of your display data, and to be able to refresh it after editing, requires the use of a custom adapter.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

770 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