Solved

Simple mouse tracking in Delphi

Posted on 2012-04-02
2
634 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
[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
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

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

Suggested Solutions

Title # Comments Views Activity
egit plugin on eclipse 8 129
Reccomended programming language for client-server applications 12 196
C qsort compare function issue 6 70
junit initializtion error 2 22
A short article about problems I had with the new location API and permissions in Marshmallow
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …

752 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