QUESTION: I have a TImage component that I would like to be able to move during runtime. How could I accomplish this? ANSWER: Here is a sample program that allows you to move an image around on a form. It defines three events associated with the TImage: OnMouseDown, OnMouseMove, OnMouseUp. It declares four integers variables as private in the Form's header: PosX, PosY get the coordinates when the left mouse button is pressed, and DragX, DragY which store the new coordinates when the mouse is moved. It also declares the bool lMouse as private in the Form's header. lMouse is used to keep track of whether or not the left mouse button is pressed. void __fastcall MyForm::imgMainMouseDown(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) { if(Button == mbLeft) { PosX = X; //Initial Coordinates PosY = Y; lMouse = true; // Bool set to true when left mouse button is pressed } } //--------------------------------------------------------------------------- void __fastcall TForm1::imgMainMouseMove(TObject *Sender, TShiftState Shift, int X, int Y) { if (lMouse) { DragX = X-PosX; // Compute new Coordinates DragY = Y-PosY; // Move Image imgMain->Left = imgMain->Left +DragX; imgMain->Top = imgMain->Top + DragY; } } //--------------------------------------------------------------------------- void __fastcall TForm1::Image1MouseUp(TObject *Sender, TMouseButton Button, TShiftState Shift, int X, int Y) { lMouse = false; // Reset Bool to false } |
Last Modified: 11-JAN-00