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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

i want to move an image to any location i want on the form 2

Status
Not open for further replies.
Jan 20, 2007
237
US
Hi,
i would like to know what is the trick to move an image i have in my form, from the default location(the actual location when the form load) i have it now, to any other location desired by the user, i know we need to use image property Mousemove and have nButton =1 for it for the left click to move the image but i need something else to be added to code, to just move it around and located where i want, can anyone advise please ?
Thanks a lot
 
It's easier than that. Just set the image's DragMode property to 1. That will let the user drag it around the form using the mouse. When the user drops the image, its DragDrop event fires. That will tell you the location of the point where the image has been dropped. You them use that information to programmatically move the image to that new location.

At its simplest, the code in the DragDrop looks like this:

Code:
LPARAMETERS oSource, nXCoord, nYCoord
oSource.Move(nXCoord, nYCoord)

[highlight #EDD400]CORRECTION:[/highlight] The above code should go in the DragDrop event of the form, not the image as stated above. Apologies.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Ernesto,

You have to control the drag and drop of the image object.

This is a demo that drags and drops a shape, but it can be used with an image as well. You can see that you'll have to add extra code if you want to prevent some areas of your form to be used as a drop spot for the image.

Code:
LOCAL Test AS DragNDropper

m.Test = CREATEOBJECT("DragNDropper")
m.Test.Show(1)
m.Test = .NULL.

DEFINE CLASS DragNDropper AS Form

	ADD OBJECT NotOverMe AS Label ;
		WITH Top = 10, Left = 200, ;
		Caption = "Not over me!"

	ADD OBJECT OverMe AS Label ;
		WITH Top = 30, Left = 200, ;
		Caption = "Over me!"

	ADD OBJECT ABox AS Shape ;
		WITH Top = 10, Left = 10, ;
			BackColor = RGB(128, 0, 0), ;
			BorderColor = RGB(128, 128, 0), ;
			BorderWidth = 5, ;
			Height = 100, Width = 100, ;
			DragMode = 0, ;
			XStart = 0, YStart = 0

	FUNCTION ABox.MouseDown (MouseButton AS Integer, AddKey AS Integer, XCoord AS Integer, YCoord AS Integer)

		IF m.MouseButton = 1 AND m.AddKey = 0
			This.XStart = m.XCoord
			This.YStart = m.YCoord
			This.Drag(1)
		ENDIF

	ENDFUNC

	FUNCTION ABox.DragDrop (Source AS Object, XCoord AS Integer, YCoord AS Integer)

		Thisform.DragDrop(m.Source, m.XCoord, m.YCoord)

	ENDFUNC

	FUNCTION OverMe.DragDrop (Source AS Object, XCoord AS Integer, YCoord AS Integer)

		Thisform.DragDrop(m.Source, m.XCoord, m.YCoord)

	ENDFUNC

	FUNCTION DragDrop (Source AS Object, XCoord AS Integer, YCoord AS Integer)

		m.Source.Top = m.Source.Top + (m.YCoord - m.Source.YStart)
		m.Source.Left = m.Source.Left + (m.XCoord - m.Source.XStart)
		This.Refresh()

	ENDFUNC

ENDDEFINE
 
Just to add ....

My method will work provided you are only dragging within a single container. If you just want to drag from one part of the form to another, that should be fine. It won't work if you want to drag from one form to another, or to or from a container within the form. In those cases, you would have to use OLE Drag and Drop, which is a little more complicated.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Hi Mike,
i added the code provided but actually i when i drag the image, the image moves but once i release the mouse, the image comeback to the original location so actually it does not move to the location dragged, i just missing something else here ?
i just trying to move the image inside the form itself from one location to another in the same form

Thanks
 
Just note this isn't working that flawless when you drag the image on a "crowded" part of the form (meaning: there already are other controls on it). And while that's not a big problem, because you'd also want to drag it to an empty space, a container or a pageframe is blocking the form's DragDrop event. Besides, you'd then want to let the image control not just change its coordinates but also its parent objhect to become whatever you dragged it onto.

The form designer often enough makes it easier to copy and paste a control, or cut and paste. After copy or cut you then first activate the target and paste.

Bye, Olaf.

Olaf Doschke Software Engineering
 
One other point to keep in mind ....

The coordinates that you pass to the Move method (nXCoord, nYCoord) refer to the top-left corner of the image. That's fine. But the user might expect the drop point to match the position of the mouse pointer. So if the mouse is in the centre of the image during the drag, they might expect it to be dropped in such a way that the pointer is still in the centre when it is dropped.

This is probably not worth worrying about, but if it is an issue, you would need to adjust the coordinates to take it into account.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Just for the record,
Mike said:
The coordinates that you pass to the Move method (nXCoord, nYCoord) refer to the top-left corner of the image. That's fine. But the user might expect the drop point to match the position of the mouse pointer. So if the mouse is in the centre of the image during the drag, they might expect it to be dropped in such a way that the pointer is still in the centre when it is dropped.
the demo above handles that case.
 
António said:
the demo above handles that case

Quite right. You are adjusting the Top and Left of the object. That's better than using the Move method.

On a small point, António, you don't need to do a Refresh in the DragDrop event (unless you have some reason for doing so which is not connected to the drag-and-drop).

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
Mike said:
On a small point, António, you don't need to do a Refresh in the DragDrop event

You're right, Mike, the Refresh() is unnecessary in the demo.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top