Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Centering Form On Any Size Computer Monitor
Message
From
04/01/2007 15:11:27
 
 
To
04/01/2007 14:15:32
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Environment versions
Visual FoxPro:
VFP 7 SP1
OS:
Windows XP SP2
Network:
Windows XP
Database:
Visual FoxPro
Miscellaneous
Thread ID:
01182424
Message ID:
01182497
Views:
9
Use the AutoCenter property setting on the Property Editor for the form. But you might want to consider saving the form's position at Destroy() event and then restoring it in the Init() event as users may have a different preference for position than you. You can still set the AutoCenter property for the form as a default, and then use the following to restore the user's preferences:
*-*  To save the settings
LOCAL lcWindow, lcFormName
DECLARE INTEGER GetSystemMetrics IN user32 INTEGER nIndex
WITH thisform
	lcFormName = .Name
	lcWindow = TRANSFORM(.Top) + ';' + TRANSFORM(.Left) + ';' + TRANSFORM(.Width) + ';' + TRANSFORM(.Height) + ';' + TRANSFORM(.WindowState) + ";" + TRANSFORM(GetSystemMetrics(SM_CMONITORS))
	=SetString_VFP("Form",lcFormName,lcWindow)  && This is where you store the setting
ENDWITH
Then to restore the settings:
LOCAL lcWindow, llReturn, lnTop, lnLeft, lnWidth, lnHeight, lnWinStat, lcFormName
DECLARE INTEGER GetSystemMetrics IN user32 INTEGER nIndex
WITH thisform
	lcFormName = .Name
	lcWindow = GetString_VFP("Form",lcFormName)  && This is where you retrieve the saved settings
	IF EMPTY(lcWindow)
		llReturn = .F.
	ELSE
		IF VAL(GETWORDNUM(lcWindow,6,";"))>1 .AND. GetSystemMetrics(SM_CMONITORS) = 1
*-*			Adjust restore position - previously on two monitors, now one monitor
			lnTop     = 4
			lnLeft    = 4
			lnWidth   = SYSMETRIC(1) * 0.95
			lnHeight  = SYSMETRIC(2) * 0.85
			lnWinStat = 0
		ELSE
			lnTop     = VAL(GETWORDNUM(lcWindow,1,";"))
			lnLeft    = VAL(GETWORDNUM(lcWindow,2,";"))
			lnWidth   = VAL(GETWORDNUM(lcWindow,3,";"))
			lnHeight  = VAL(GETWORDNUM(lcWindow,4,";"))
			lnWinStat = VAL(GETWORDNUM(lcWindow,5,";"))
		ENDIF
*-*		Check if a form window at the left and top coordinates
		DO WHILE GetVFPForms(lnLeft,lnTop)  && This is checking for a form already in this position
			lnLeft = lnLeft + SYSMETRIC(9)
			lnTop  = lnTop  + SYSMETRIC(9)
		ENDDO
*-*		Restore window position
		.LockScreen  = .T.
		.Top         = lnTop
		.Left        = lnLeft
		.Width       = lnWidth
		.Height      = lnHeight
		.WindowState = lnWinStat
		.LockScreen  = .F.
		llReturn = .T.
	ENDIF
ENDWITH
RETURN llReturn
The function GetVFPForms:
*-***********************************************************************************************
*-*  Routine for determining if a window is already present at the left and top coordinates
*-*
FUNCTION GetVFPForms
LPARAMETERS pnLeft,pnTop
LOCAL llFound, loWindow, loForm, lhDesktop, lhFirstChild, lhLastChild, lhCurrent, lcWinCap
DECLARE INTEGER GetWindow        IN user32 INTEGER hwnd, INTEGER wFlag
DECLARE INTEGER GetWindowRect    IN user32 INTEGER hwnd, STRING @lpRect 
DECLARE INTEGER GetWindowText    IN user32 INTEGER hwnd, STRING @lpString, INTEGER cch
DECLARE INTEGER GetDesktopWindow IN user32
#DEFINE GW_HWNDLAST   1
#DEFINE GW_HWNDNEXT   2
#DEFINE GW_CHILD      5
llFound = .F.
IF pnLeft > 0 .AND. pnTop > 0
	CREATE CURSOR c_wintext (winhandle I, wintitle C(200))
*-*	Get VFP forms
	loWindow = CREATEOBJECT("Twindow", _VFP.hWnd, 0)
*-*	Get Forms in Windows
	lhDesktop    = GetDesktopWindow()
	lhFirstChild = GetWindow(lhDesktop, GW_CHILD)
	lhLastChild  = GetWindow(lhFirstChild, GW_HWNDLAST)
	lhCurrent    = lhFirstChild
	DO WHILE .T.
		lcWinCap = GetWinText(lhCurrent)
		INSERT INTO c_wintext (winhandle,wintitle) VALUES (lhCurrent,lcWinCap)
		IF lhCurrent = lhLastChild
			EXIT
		ENDIF
		lhCurrent = GetWindow(lhCurrent, GW_HWNDNEXT)
	ENDDO
*-*	Check for position overlap
	SELECT c_wintext
	SCAN FOR !EMPTY(wintitle)
		lpRect = REPLICATE(CHR(0),16)
		=GetWindowRect(c_wintext.winhandle, @lpRect)
		lnLeft = buf2dword(SUBSTR(lpRect,1,4))
		lnTop  = buf2dword(SUBSTR(lpRect,5,4))
		IF lnLeft = pnLeft .AND. lnTop = pnTop
			llFound = .T.
			EXIT
		ENDIF
	ENDSCAN
	USE
ENDIF
RETURN llFound
ENDFUNC


*-***********************************************************************************************
*-* Converts buffer to integer
*-*
FUNCTION Buf2Dword(lcBuffer)
RETURN ASC(SUBSTR(lcBuffer, 1,1)) + ;
	BITLSHIFT(ASC(SUBSTR(lcBuffer, 2,1)),  8) + ;
	BITLSHIFT(ASC(SUBSTR(lcBuffer, 3,1)), 16) + ;
	BITLSHIFT(ASC(SUBSTR(lcBuffer, 4,1)), 24)
ENDFUNC


*-***********************************************************************************************
*-* Converts buffer to integer
*-*
FUNCTION GetWinText(phHdl)
LOCAL lcBuffer, lnResult
lcBuffer = SPACE(200)
lnResult = GetWindowText(phHdl, @lcBuffer, LEN(lcBuffer))
RETURN UPPER(LEFT(lcBuffer, lnResult))
ENDFUNC

*-***********************************************************************************************
*-*  Retreives FoxPro based forms (In-Screen mode)
*-*
DEFINE CLASS Twindow As Custom
	PROCEDURE  Init
		LPARAMETERS pnHandle, pnParent
		LOCAL lhChild, loChild, lhNext, loNext, lcWinCap
		IF pnHandle = 0
			RETURN .F.
		ENDIF
		lcWinCap = GetWinText(pnHandle)
		INSERT INTO c_wintext (winhandle,wintitle) VALUES (pnHandle,lcWinCap)
		lhChild = GetWindow(pnHandle, GW_CHILD)
		loChild = CREATEOBJECT("Twindow", lhChild, pnHandle)
		IF pnParent <> 0
			lhNext = GetWindow(pnHandle, GW_HWNDNEXT)
			loNext = CREATEOBJECT("Twindow", lhNext, pnParent)
		ENDIF
	ENDPROC
ENDDEFINE
Previous
Reply
Map
View

Click here to load this message in the networking platform