Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Best way to save user's environment
Message
General information
Forum:
Visual FoxPro
Category:
Forms & Form designer
Miscellaneous
Thread ID:
00535841
Message ID:
00535951
Views:
15
Nadya,

Ok, now here is code from my application object for saving forms on shutdown (SaveFormsSettings) and restoring forms on startup (ShowStartupElements). This code demonstrates how to save and restore properties of an object. I didn't include code that sets the properties of the form because I do it in various methods of the form but I pass the SettingObject to the form in the Init and have a property on the form called oSetting so it is available from any method. Here's the Init of my form:
PROCEDURE INIT
LPARAMETERS toApp,toService,toSetting

*-- Run the parent Code
DODEFAULT(toApp)

THIS.oService = toService
THIS.oSetting = toSetting
ENDPROC
You can then set your form properties from the oSetting properties here or anywhere else you see fit. Again some of the code is specific to my framework but I am sure you can figure it out.

First, my forms have a procedure named GetSettingObject which returns a SettingObject. Here is the SettingObject definition and GetSettingObject code:

* Add any properties that you want to save about the form to this class.
DEFINE CLASS form_setting AS _custom
  HEIGHT = 48
  WIDTH = 113
  nactivemenuid = 0
  nactivepageid = 1
  nactiveitemid = 0
  nactiveinterfaceid = 0
  lviewtoolbar_form = .T.
  lviewstatusbar = .T.
  lviewoutlookbar = .T.
  uparm1 = .NULL.
  uparm2 = .NULL.
  uparm3 = .NULL.
  uparm4 = .NULL.
  uparm5 = .NULL.
  lviewtoolbar_std = .T.
  lviewtoolbar_menuitemview = .T.
  nheight = 500
  ntop = 5
  nwidth = 750
  nleft = 5
  nwindowstate = 0
  nsliderxcoord = 105
  oparmrs = .NULL.
  lopennewwindowfromoutlookbar = .F.
  lopennewwindowfromgrid = .T.
  NAME = "form_setting"
  lviewfolderlist = .F.
  lautosize = .F.
  lalwaysontop = .F.
  lviewtoolbar_appmenu = .F.
ENDDEFINE
* Here's the code from the form.
PROCEDURE getsettingobject
LPARAMETER tlIncludeParms
LOCAL loSetting

loSetting = NEWOBJECT('form_setting','_interfacecontrol.vcx','interface.exe')

loSetting.nActiveMenuID      = THIS.nActiveMenuID
loSetting.nActivePageID      = THIS.nActivePageID
loSetting.nActiveItemID      = THIS.nActiveItemID
loSetting.nActiveInterfaceID = THIS.nActiveInterfaceID

*IF tlIncludeParms
loSetting.uParm1  = THIS.oSetting.uParm1
loSetting.uParm2  = THIS.oSetting.uParm2
loSetting.uParm3  = THIS.oSetting.uParm3
loSetting.uParm4  = THIS.oSetting.uParm4
loSetting.uParm5  = THIS.oSetting.uParm5
*ENDIF

loSetting.lOpenNewWindowFromGrid       = THIS.lOpenNewWindowFromGrid
loSetting.lOpenNewWindowFromOutlookBar = THIS.lOpenNewWindowFromOutlookBar

IF PEMSTATUS(THIS,'oSlider',5)
  loSetting.nSliderXCoord = THIS.oSlider.LEFT
ENDIF

IF NOT THIS.WINDOWSTATE = 2
  loSetting.nTOP    = THIS.TOP
  loSetting.nLEFT   = THIS.LEFT
  loSetting.nHEIGHT = THIS.HEIGHT
  loSetting.nWIDTH  = THIS.WIDTH
ELSE
  IF VARTYPE(THIS.oSetting) = 'O'
    loSetting.nTOP    = THIS.oSetting.nTOP
    loSetting.nLEFT   = THIS.oSetting.nLEFT
    loSetting.nHEIGHT = THIS.oSetting.nHEIGHT
    loSetting.nWIDTH  = THIS.oSetting.nWIDTH
  ENDIF
ENDIF

loSetting.lViewToolBar_form         = THIS.lViewToolBar_form
loSetting.lViewToolBar_std          = THIS.lViewToolBar_std
loSetting.lViewToolBar_MenuItemView = THIS.lViewToolBar_MenuItemView
loSetting.lViewToolBar_AppMenu      = THIS.lViewToolBar_AppMenu
loSetting.lViewStatusBar            = THIS.lViewStatusBar
loSetting.lViewOutLookBar           = THIS.lViewOutLookBar
loSetting.lViewFolderList           = THIS.lViewFolderList
loSetting.lAlwaysOnTop              = THIS.ALWAYSONTOP
loSetting.nWindowState              = THIS.WINDOWSTATE
loSetting.lAutoSize                 = .F.

RETURN loSetting
ENDPROC
* Now the two methods from my application class which save and restore the forms.
PROCEDURE showstartupelements
LOCAL lnFormIndex,lcFormSettingKey,loSetting,lnFormCount,lnPropertyCount,;
  lnProperty,lcProperty,luPropertyValue,lnMenuPad

IF VARTYPE(goSplash) = 'O'
  goSplash.cMessage = "Restoring user settings..."
ENDIF

loSetting = NEWOBJECT('form_setting','_interfacecontrol.vcx','interface.exe')

IF NOT THIS.oService.lIniFound
  loSetting.nActiveInterfaceID        = -1
  loSetting.lViewOutlookbar           = .F.
  loSetting.lViewFolderList           = .F.
  loSetting.lViewToolbar_AppMenu      = .F.
  loSetting.lViewToolbar_Form         = .F.
  loSetting.lViewToolbar_MenuItemView = .F.
  loSetting.lViewToolbar_Std          = .F.
ENDIF

*DODebug('We are restoring form settings...')

lnFormCount = THIS.oService.GetUserSettings(lnFormCount,"nFormCount")

IF VARTYPE(lnFormCount) = "N"

  FOR lnFormIndex = 1 TO lnFormCount

    lcFormSettingKey  = 'FORM_'+ALLTRIM(STR(lnFormIndex))

    lnPropertyCount = AMEMBERS(laProperties,loSetting)

    FOR lnProperty = 1 TO lnPropertyCount
      lcProperty = laProperties[lnProperty]
      IF PEMSTATUS(loSetting,laProperties[lnProperty],4) && User defined
        IF THIS.oService.GetUserSettings(lcFormSettingKey,lcProperty,@luPropertyValue)
          STORE (luPropertyValue) TO ('loSetting.'+lcProperty)
        ENDIF
      ENDIF
    ENDFOR
    THIS.DoForm(loSetting,lcFormSettingKey)
  ENDFOR
ENDIF

IF THIS.nFormCount = 0
  THIS.DoForm(loSetting)
ENDIF

RETURN THIS.nFormCount > 0
ENDPROC

PROCEDURE saveformsettings
LOCAL liIndex,lcFormSettingKey,loSetting,lnPropertyCount,lnProperty,luValue,;
  laProperties[1],lnMenuPad,lcParameters,lnInterfaceCount,lnPosition

*DODebug('We are saving form settings...')

IF THIS.nFormCount > 0
  THIS.oService.SaveUserSettings(,"nFormCount",THIS.nFormCount)

  FOR liIndex = 1 TO THIS.nFormCount
    IF VARTYPE(THIS.aForms[liIndex]) = 'O'

      lcFormSettingKey  = 'FORM_'+ALLTRIM(STR(liIndex))
      loSetting         = THIS.aForms[liIndex].GetSettingObject(.T.)

      lnPropertyCount = AMEMBERS(laProperties,loSetting)

      FOR lnProperty = 1 TO lnPropertyCount
        IF PEMSTATUS(loSetting,laProperties[lnProperty],4) && User defined
          luValue = EVAL('loSetting.'+laProperties[lnProperty])
          THIS.oService.SaveUserSettings(lcFormSettingKey,;
            laProperties[lnProperty],;
            luValue)
        ENDIF
      ENDFOR
    ENDIF
* - Subtract 2 for the application menu pads to clear list
    THIS.oService.SaveUserSettings(lcFormSettingKey,;
      "nInterfaceMenuCount",;
      THIS.aForms[liIndex].oMenu.Interface.CONTROLCOUNT-2)

*    DODebug('We are saving form settings...')

    lnPosition   = 0
    lnInterfaceCount = THIS.aForms[liIndex].oMenu.Interface.CONTROLCOUNT
    FOR lnMenuPad = 1 TO THIS.aForms[liIndex].oMenu.Interface.CONTROLCOUNT
      IF NOT SUBSTR(THIS.aForms[liIndex].oMenu.Interface.CONTROLS(lnMenuPad).NAME,1,1) = '_'
        lcParameters = SUBST(THIS.aForms[liIndex].oMenu.Interface.CONTROLS(lnMenuPad).COMMAND,;
          AT('(',THIS.aForms[liIndex].oMenu.Interface.CONTROLS(lnMenuPad).COMMAND)+1)
        lcParameters = SUBST(lcParameters,1,LEN(lcParameters)-1)

        lnPosition = THIS.aForms[liIndex].oMenu.Interface.CONTROLS(lnMenuPad).nPad_Position
        THIS.oService.SaveUserSettings(lcFormSettingKey,;
          'InterfaceMenu_'+ALLTRIM(STR(lnPosition)),;
          lcParameters)
      ENDIF
    ENDFOR
  ENDFOR
ENDIF
ENDPROC
I almost forgot... This code should be in the resize of the form to capture width and height changes as they occur. This allows you to keep the last width and height values of the form in case the form is maximized (.WINDOWSTATE = 2) when you call GetSettingObject on shutdown.
WITH THIS
  IF VARTYPE(.oSetting) = 'O' AND .WINDOWSTATE = 0
    .oSetting.nHEIGHT = .HEIGHT
    .oSetting.nWIDTH  = .WIDTH
  ENDIF
ENDWITH
That should do it.

Will
Heavy Metal Pedal - click with care
Previous
Reply
Map
View

Click here to load this message in the networking platform