Solved

Simple mouse tracking in Delphi

Posted on 2012-04-02
2
611 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
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This is an explanation of a simple data model to help parse a JSON feed
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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 …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

772 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now