Jul 28, 2008

Resize or reposition a desktop form to fit the Windows desktop

In your VFP application, you want your users to have the application form re-appear as it was left behind the last time. So, you store the screen coordinates (in your apps mechanism that saves the application settings) and restore the form to the saved coordinates, the next time it is run.
But in the mean time, the user may have altered the screen resolution, changed to a p.c. with a different screen resolution or moved/resized the Windows startbar.

I these scenario's, your application form may not fit in the Windows desktop anymore.
Below, you find the code of SETFORMSIZE.PRG, which repositions the form to fit the desktop again.

To test it:
- create a form with the desktop property to .True.
- Put this code in the load method of the form:
This.Top = -100
This.Left = -100
This.Width = 100000
This.Height = 100000
DO SETFORMSIZE WITH This

- Run the form


************************************************************************
* SETFORMSIZE.PRG *
************************************************************************
PARAMETERS oForm

LOCAL nWinHeight, nWinWidth, nCorrection

* Width en height of screenspace minus space occuped by the startmenu bar
* wherever that is placed or whatever size it is
nWinHeight = SYSMETRIC(22)
nWinWidth = SYSMETRIC(21)
nCorrection = sysmetric(3)*2

* First: check if the form is off screen above and to the left
IF oForm.Left < 0
oForm.Left = 0
ENDIF
IF oForm.Top < 0
oForm.Top = 0
ENDIF

* Next: check if the form is to high or wide to fit in the screen
IF (oForm.Top + oForm.Height) > nWinHeight && Oh Oh, Formheight beyond screen
nHowMuch = (oForm.Top + oForm.Height ) - nWinHeight
IF nHowMuch > oForm.Top && Oh oh, form does not fit in the screen
IF oForm.MinHeight >= (nWinHeight - oForm.Height )
oForm.Height = nWinHeight - nCorrection
ENDIF

nNewTop = 0
ELSE
nNewTop = (oForm.Top - nHowMuch) - nCorrection
ENDIF
ELSE
nNewTop = oForm.Top
ENDIF
IF (oForm.Left + oForm.Width) > nWinWidth && Oh Oh, Formwidth beyond screen
nHowMuch = (oForm.Left + oForm.Width ) - nWinWidth
IF nHowMuch > oForm.Left && Oh oh, form does not fit in the screen
IF oForm.MinWidth >= (nWinWidth - oForm.Width )
oForm.Width = nWinWidth - nCorrection
ENDIF
nNewLeft = 0
ELSE
nNewLeft = (oForm.Left - nHowMuch) - nCorrection
ENDIF
ELSE
nNewLeft = oForm.Left
ENDIF

oForm.Left = nNewLeft
oForm.Top = nNewTop