yes i know, still do you know how to do it?
take a look at this code, it's not useful but it'll give you a good idea of what can be done
http://www.delphipages.com
or
http://delphi.about.com/od
Main Topics
Browse All Topicsis there any way to convert the amout of pixels traveled by a mouse into centimeters? (or millimeter, or meters, or whatever)
i already have a mouse hook, so that;s no problem, i know where is the mouse is all the time.
code will be greatly appreciated.
thanks
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
yes i know, still do you know how to do it?
take a look at this code, it's not useful but it'll give you a good idea of what can be done
http://www.delphipages.com
or
http://delphi.about.com/od
Amigo is right.
You would be able to calculate the position of a mouse using the CursorPos position I think to get the start and end points. This would give you the number of pixels travelling in the X and axis, but you dont have a reference point due to screen resolution.
You would need to work from a standard resolution, and calculate a base pixel distance to cover a centimetre. You could then extrapolate for other resolutions from this base figure.
So say you worked on a 800x600 screen and you found that 1cm = 20 pixels on the X axis and 1cm = 15 pixels on the Y axis.
Therefore a 1200x1024 screen, X axis (1200/800)x20 = 30 pixels, Y axis (1024/600)x15 pixels.
I think my maths is correct.
seems like the example has a few typos. try this (havent tested tho)
procedure PixelsPerMM(
canvas: TCanvas;
var x, y: single) ;
var
H:HDC;
hres,vres,
hsiz,vsiz:integer;
begin
H:=canvas.handle;
hres := GetDeviceCaps(H,HORZRES) ; {display width in pixels}
vres := GetDeviceCaps(H,VERTRES) ; {display height in pixels}
hsiz := GetDeviceCaps(H,HORZSIZE) ; {display width in mm}
vsiz := GetDeviceCaps(H,VERTSIZE) ; {display height in mm}
x := hres/hsiz;
y := vres/vsiz;
end;
~~~~~~~~~~~~~~~~~~~~~~~~~
var
cx, cy : single;
mmx, mmy : integer;
begin
PixelsPerMM(Handle,cx,cy) ;
mmx := Trunc(468 / cx) ;
mmy := Trunc(60 / cy) ;
end;
by the way if you want to know why i need this code visit my page at
www.geocities.com/uf_new
i'm writing a mouse application with ergonomics in mind, since i developed an RSI case. part of it is how much mouse movement a user produces on each session
This should do it for you.
Regards,
Russell
function PixelHorizToMM(PixelX: Integer): Integer;
var hdcDesk: HDC;
begin
hdcDesk:=GetDC(0);
try
result:=Trunc(PixelX * (GetDeviceCaps(hdcDesk, HORZSIZE) / GetDeviceCaps(hdcDesk, HORZRES)));
finally
ReleaseDC(0, hdcDesk);
end;
end;
function PixelVertToMM(PixelY: Integer): Integer;
var hdcDesk: HDC;
begin
hdcDesk:=GetDC(0);
try
result:=Trunc(PixelY * (GetDeviceCaps(hdcDesk, VERTSIZE) / GetDeviceCaps(hdcDesk, VERTRES)));
finally
ReleaseDC(0, hdcDesk);
end;
end;
function PixelPointToMM(PixelPt: TPoint): TPoint;
begin
result:=Point(PixelHorizTo
end;
When dealing with the mouse, one of the most common structures is the TPoint, which is a structure to hold both the horiz/vert position. The X, Y values are integer data types, and the resolution is assumed to be in pixels.
All I did was provide you a helper function that takes both a horiz and vert pixel measurement and converts it to millimeter in one call. Eg:
var ptCursorInMM: TPoint;
begin
// Convert mouse position in pixels to point structure in mm
ptCursorInMM:=PixelPointTo
// ptCursorInMM.X is the horiz measurement now in MM
// ptCursorInMM.Y is the vert measurement now in MM
end;
Or, for example, you are trying to convert the width/height of something in pixels -> mm. Its nothing more than a conversion helper function, thats all. So I will say again, if you don't need it, then don't use it.
Russell
ok, every time the mouse move i call this function (quick and dirty):
posx and posy are integers that are updated every time the mouse move (by the mouse hook) so they are always updated with the latest position of the mouse.
procedure TForm1.Mousetravel;
var
traveled, currpos:tpoint;
total:integer;
begin
currpos.X:=posx;
currpos.Y:=posy;
traveled:=PixelPointToMM(c
total:=traveled.X+traveled
form2.StaticText2.Caption:
end;
even tho i move the mouse the values are being updated randomly and without any logic (seemingly).
i will give you the points, since the answer seems to work, but again what i need in the total (in mm, cm or whatever in metric) that the mouse traveled until it stops. so, any ideas?
thanks
this is how the function looks at the moment (to km)
procedure TForm1.Mousetravel;
var
traveled, currpos:tpoint;
total:integer;
km:double;
begin
currpos.X:=posx;
currpos.Y:=posy;
traveled:=PixelPointToMM(c
total:=traveled.X+traveled
km:=(total*0.001)*0.001;
form2.StaticText2.Caption:
(form2.StaticText2.Caption
km, ffNumber, 8, 4);
end;
urif,
Two things stand out. First, I already mention this; you will find the resolution between pixels/mm to be such that a double value should be used. Eg, on my system
3.2 pxls = 1 mm, or 1pxl = 0.3125 mm
So moving one pixel would yield 0 mm in integer format. (It takes 3.2 pixels to yield 1 mm).
Second, your math for determining distance is incorrect. Take for example the following. You start at a position of (0,0) and then move to (1,1). Using your math, you will have calculated a move of 2 units.
total:=traveled.X+traveled
This is incorrect, as the distance traveled is actually 1 unit.
You didn't mention this before, only that you required a conversion from pixels to mm. The conversion routines ARE correct, but again, should probably be scaled to double for greater accuracy. If you would like, I will provide these as well as the correct algo for distancing.
Regards.
Russell
The following is code for:
- pixel->mm conversion routines that yeild a double result
- handling for a point structure of double (X,Y)
- distance caclulation using the double point structure, where X = horiz distance traveled, and Y = vert distance traveled.
The one thing that bothers me is that you indicate posx/posy represent the current mouse position as updated by your mouse hook. If this is the case, then what are you measuring distance from? I.e., where is the starting point? You don't appear to store the last position checked, so as it stands, your calculations would be from the screen position of (0,0). Am I missing something?
Regards,
Russell
--------
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
type
TDoublePoint = packed record
x: Double;
y: Double;
end;
function DoublePoint(X, Y: Double): TDoublePoint;
function PixelHorizToMM(PixelX: Integer): Double;
function PixelVertToMM(PixelY: Integer): Double;
function PixelPointToMM(PixelPt: TPoint): TDoublePoint;
function Distance(Traveled: TDoublePoint): Double;
type
TForm1 = class(TForm)
procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
procedure FormCreate(Sender: TObject);
private
// Private declarations
FLastPoint: TDoublePoint;
public
// Public declarations
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
function DoublePoint(X, Y: Double): TDoublePoint;
begin
result.x:=X;
result.y:=Y;
end;
function PixelHorizToMM(PixelX: Integer): Double;
var hdcDesk: HDC;
begin
hdcDesk:=GetDC(0);
try
result:=PixelX * (GetDeviceCaps(hdcDesk, HORZSIZE) / GetDeviceCaps(hdcDesk, HORZRES));
finally
ReleaseDC(0, hdcDesk);
end;
end;
function PixelVertToMM(PixelY: Integer): Double;
var hdcDesk: HDC;
begin
hdcDesk:=GetDC(0);
try
result:=PixelY * (GetDeviceCaps(hdcDesk, VERTSIZE) / GetDeviceCaps(hdcDesk, VERTRES));
finally
ReleaseDC(0, hdcDesk);
end;
end;
function PixelPointToMM(PixelPt: TPoint): TDoublePoint;
begin
result:=DoublePoint(PixelH
end;
function Distance(Traveled: TDoublePoint): Double;
begin
// The Traveled values represent the amount of distance traveled, where X is the
// horizontal distance, and Y is the vertical distance. These values can be signed
// as either positive or negative. This allows calculating from a virtual starting
// point of (0,0).
result:=Abs(Sqrt(Sqr(0-Tra
end;
procedure TForm1.FormMouseMove(Sende
var dpCurrent: TDoublePoint;
dpTraveled: TDoublePoint;
begin
// Get current position
dpCurrent:=PixelPointToMM(
// Calculate the distance traveled in (X, Y) values
dpTraveled:=DoublePoint(dp
// Display distance in mm
Caption:=FloatToStr(Distan
// Update the last point
FLastPoint:=dpCurrent;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
// Set the last starting point
FLastPoint:=DoublePoint(0,
end;
//////////////////////////
// Applying the procedures to your code
//////////////////////////
//
//
// procedure TForm1.Mousetravel;
// var
// traveled: TDoublePoint;
// total: Double;
// km: Double;
// begin
// traveled:=PixelPointToMM(P
// total:=Distance(traveled);
// km:=(total * 0.001) * 0.001;
//
// ... remaining code to display or do whatever
//
// end;
//
//////////////////////////
end.
Business Accounts
Answer for Membership
by: AmigoJackPosted on 2005-10-09 at 02:55:53ID: 15047353
"pixel" is another dimension length than "meter". always remember that your monitor can display 1024x768 or 800x600 or 640x480 or whatever pixels while it always fills the screen. a mousemove of virtually 2cm on a 800x600 display would be virtually 4cm on a 400x300 display (as example). see what i mean?