Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Smoothly moving an object
Message
General information
Forum:
Visual FoxPro
Category:
Forms & Form designer
Environment versions
Visual FoxPro:
VFP 9 SP1
OS:
Windows 10
Database:
Visual FoxPro
Miscellaneous
Thread ID:
01656712
Message ID:
01656764
Views:
123
This message has been marked as the solution to the initial question of the thread.
Likes (2)
Hi Peter,

I see a couple of problems in your code, one does not help at all with your issue, and is the unnecessary reference this.oObj, but I assume you are just using this code to show the problem and in your real code it makes sense to have said reference. The second issue that for me is very important is that the way you calculate left and top properties is wrong, you can easily see the mistake if you change the number of steps from 30 to say 3000, due to the transformation of the left and top properties to an integer value the object would never move, this shows you that you should make the calculations on an independent property before assigning the value to left and top. Lastly, and this is the direct answer to your question, to force the redraw you can force windows to do the update of the screen using RedrawWindow API. So, this should do the trick (not sure why did you use Pause(2 / m.lnStep) instead of Pause(1 / m.lnStep) so I changed that too.
#Define RDW_UPDATENOW 0x0100

oform = Newobject("testmove")
oform.Show
Read Events

Define Class testmove As Form
	BorderStyle = 2
	Caption = "testmove"
	AlwaysOnTop = .T.
	BackColor = Rgb(255,255,255)
	Name = "testmove"
	scalemode = 3
	oObj = NULL
	width = 900
	height = 700

Procedure MouseDown
	Lparameters nButton, nShift, nXCoord, nYCoord
	this.moveobject( nXCoord, nYCoord )
	return

function init

	Declare INTEGER RedrawWindow in User32.dll INTEGER hWnd, INTEGER lprcUpdate, INTEGER hrgnUpdate, INTEGER flags

	lcNewObjName = 'theObject'
	this.newobject( m.lcNewObjName, 'label' )
	this.oObj = this.&lcNewObjName
	this.&lcNewObjName..visible = .t.
	addproperty(this.oObj, 'fullleft', this.oObj.left)
	addproperty(this.OObj, 'fulltop', this.oObj.top)
	return

function queryunload
	clear dlls Sleep
	clear events
	return

function keypress
	LPARAMETERS nKeyCode, nShiftAltCtrl
	if inlist( nKeyCode, 27, 113, 32 )
		clear events
	endif
	return

function moveobject

	lparameter tnToLeft, tnToTop

	local lnStep, ln, lnT, lnL

	with this
		
		lnStep  = 30
		lnT = ( m.tnToTop  - .oObj.fulltop  ) / m.lnStep
		lnL = ( m.tnToLeft - .oObj.fullleft ) / m.lnStep
		
		with .oObj

			for ln = 1 to m.lnStep
				*
				.caption = transform( m.ln ) + ': ' + TRANSFORM(.fulltop, '9999.99') + ', ' + TRANSFORM(.fullleft, '9999.99')
				.fulltop  = .fulltop  + m.lnT
				.fullleft = .fullleft + m.lnL
				*
				.top = .fulltop && this will round up .top
				.left = .fullleft && this will round up .left
				RedrawWindow(THISFORM.HWnd, 0, 0, RDW_UPDATENOW)
				Pause(1 / m.lnStep)
			next
		.refresh()
		endwith
	endwith

enddefine

FUNCTION Pause( tnSeconds )
	DECLARE Sleep in WIN32API integer
	Sleep( max( 0, tnSeconds ) * 1000 )
	RETURN
>Hi all,
>
>I want an object (now a label) to smoothly move to another location on the form. Based on the y,x coordinates of the label and of the position that the mouse is down, the object moves in 30 steps. However, only some of those steps are actually displayed. I get the impression that the refresh-rate of VFP is simply too low for such a type of smooth moving.
>
>Any deas?
>
>
>
oform = Newobject("testmove")
>oform.Show
>Read Events
>
>Define Class testmove As Form
>	BorderStyle = 2
>	Caption = "testmove"
>	AlwaysOnTop = .T.
>	BackColor = Rgb(255,255,255)
>	Name = "testmove"
>	scalemode = 3
>	oObj = NULL
>	width = 900
>	height = 700
>
>Procedure MouseDown
>	Lparameters nButton, nShift, nXCoord, nYCoord
>	this.moveobject( nXCoord, nYCoord )
>	return
>
>function init
>	lcNewObjName = 'theObject'
>	this.newobject( m.lcNewObjName, 'label' )
>	this.oObj = this.&lcNewObjName
>	this.&lcNewObjName..visible = .t.
>	return
>
>function queryunload
>	clear dlls Sleep
>	clear events
>	return
>
>function keypress
>	LPARAMETERS nKeyCode, nShiftAltCtrl
>	if inlist( nKeyCode, 27, 113, 32 )
>		clear events
>	endif
>	return
>
>function moveobject
>
>	lparameter tnToLeft, tnToTop
>
>	local lnStep, ln, lnT, lnL
>
>	with this
>		
>		lnStep  = 30
>		lnT = ( m.tnToTop  - .oObj.top  ) / m.lnStep
>		lnL = ( m.tnToLeft - .oObj.left ) / m.lnStep
>		
>		with .oObj
>
>			for ln = 1 to m.lnStep
>				*
>				.caption = transform( m.ln )
>				.top  = .top  + m.lnT
>				.left = .left + m.lnL
>				*
>				pause( 2 / m.lnStep )
>			next
>
>		endwith
>	endwith
>
>enddefine
>
>FUNCTION Pause( tnSeconds )
>	DECLARE Sleep in WIN32API integer
>	Sleep( max( 0, tnSeconds ) * 1000 )
>	RETURN
"The five senses obstruct or deform the apprehension of reality."
Jorge L. Borges?

"Premature optimization is the root of all evil in programming."
Donald Knuth, repeating C. A. R. Hoare

"To die for a religion is easier than to live it absolutely"
Jorge L. Borges
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform