Solved

Simple mouse tracking in Delphi

Posted on 2012-04-02
2
645 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 36

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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Simple Linear Regression
Introduction to Processes

636 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