Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Simple mouse tracking in Delphi

Posted on 2012-04-02
2
Medium Priority
?
652 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 1500 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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

Make the most of your online learning experience.
If you are a mobile app developer and especially develop hybrid mobile apps then these 4 mistakes you must avoid for hybrid app development to be the more genuine app developer.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Simple Linear Regression

670 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