Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

TImage Mouse X Y Position

Status
Not open for further replies.

Doom2pro

Programmer
Jul 2, 2004
6
0
0
US
Hello, I know how to get the positions of the mouse over a TImage but my problem is this, When I drop a TImage onto a form and load a picture into it, set it's Align to alClient
and run the the program and move my mouse over the image the X and Y positions are correct.

If I set the TImage's Stretch to true and run the program the X and Y positions are correct again, not a problem...

However; if I (with the Stretch to true) enlarge the form (change it's size) the TImage changes it's size and is stretched or shrunk but the X and Y position's are no longer valid, the percentage X and Y are off is proportional to how bigger or smaller the image got.

It seems to do this because the TImage's Height and Width change but the number of pixels doesnt, because stretch is true. I need to get the correct position of the mouse's X and Y over the stretched pixels so the question is how do I compensate?

I'm sure someone has bumped into this problem before, anyway thanks in advance :)

-Richard

 
I found out how to correct X an Y

Code:
//In header
private:
	int OrigH, OrigW, RealX, RealY;

//In cpp file
//----------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner): TForm(Owner)
{
 //Set original width and height
 OrigH = Image1->Height;
 OrigW = Image1->Width;
}
//----------------------------------------------------------
void __fastcall TForm1::Image1MouseMove(TObject *Sender, TShiftState Shift, int X, int Y)
{
 //Calculate for stretching
 if(Image1->Stretch)
 {
	//RealX = MulDiv(<Bad X>, <Original Width>,
	//<Current Width>);
	RealX = MulDiv(X, OrigW, Image1->Width);

	//RealY = MulDiv(<Bad Y>, <Original Height>,
	//<Current Height>);
	RealY = MulDiv(Y, OrigH, Image1->Height);
 }
 else
 {
	//Use normal X and Y
	RealX = X;
	RealY = Y;
 }
}

I hope that helps anyone who runs into the same problem I had :)

-Richard
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top