Date: 02-16-2022
Return to Index
created by gbSnippets
'this approach uses a Function called 'Settings'
'data is saved in a file called 'myapp.ini'
'location of the file is not critical, this example puts it in the app folder
'Settings "get" is used to get saved values when the program starts
'Settings "save" is used to save values when the program ends
'Primary Code:
'in the callback function, under 'Select Case CB.Msg", add these:
%WM_InitDialog
Settings_INI "get"
%WM_Destroy
Settings "save"
'code shows how to handle variables, arrays, and menu states
'code for saving size/location of a dialog is included
'dialog size is saved unless app is minimized or maximized
'the INI API use strings only, so numerical values are saved as strings
Sub Settings_INI(Task$)
Local x As Long, y As Long, iResult&
Local xResult As Asciiz*%Max_Path, yResult As Asciiz*%Max_Path
Local temp As Asciiz*%Max_Path, INIFileName As Asciiz*%Max_Path
'defines file name (any file name will work)
INIFileName = Exe.Path$ + "myapp.ini"
'allows clearing of INI content by using application command tail: 'myapp.exe \clear'
If Instr(Command$,"\clear") Then
Kill INIFileName
End If
If Task$ = "get" Then
'get dialog top/left from INI file and use to set Dialog location
Getprivateprofilestring "All", "Left", "300", xResult, %Max_Path, INIFileName
Getprivateprofilestring "All", "Top", "300", yResult, %Max_Path, INIFileName
Dialog Set Loc hDlg, Val(xResult$), Val(yResult$) 'left/top
'get dialog width/height from INI file and use to set Dialog size
GetPrivateProfileString "All", "Width", "300", xResult, %Max_Path, INIFileName
GetPrivateProfileString "All", "Height", "300", yResult, %Max_Path, INIFileName
Dialog Set Size hDlg,Val(xResult$), Val(yResult$) 'width/height
'get value for numeric variable
Getprivateprofilestring "All", "XSplit", "", temp, %Max_Path, INIFileName
XSplit = Val(temp)
'get value for string variable
Getprivateprofilestring "All", "CFN", "", temp, %Max_Path, INIFileName
YSplit = Val(temp)
'get menu state
Getprivateprofilestring "All", "ConfirmDelete", "", temp, %Max_Path, INIFileName
iResult& = Val(temp)
Menu Set State hMenu, ByCmd %ID_ConfirmDelete, iResult&
End If
If Task$ = "save" Then
'save dialog size/location unless minimized or maximized
If IsFalse(IsIconic(hDlg) Or IsZoomed(hDlg)) Then
Dialog Get Loc hDlg To x,y
WritePrivateProfileString "All", "Left", Str$(x), INIFileName
WritePrivateProfileString "All", "Top", Str$(y), INIFileName
Dialog Get Size hDlg To x,y
WritePrivateProfileString "All", "Width", Str$(x), INIFileName
WritePrivateProfileString "All", "Height", Str$(y), INIFileName
End If
'save string variable
temp = CurrentFileName$
WritePrivateProfileString "All", "CFN", temp, INIFileName
'save numeric variable
temp = Str$(XSplit)
WritePrivateProfileString "All", "XSplit", temp, INIFileName
'save menu state
Menu Get State hMenu, ByCmd %ID_ConfirmDelete TO iResult&
temp = Str$(iResult&)
WritePrivateProfileString "All", "ConfirmDelete", temp, INIFileName
End If
End Sub
'Compilable Example: (Jose Includes)
'This example saves the position/size of the dialog
#Compiler PBWin 9, PBWin 10
#Compile EXE
#Dim All
%Unicode=1
#Include "Win32API.inc"
Global hDlg As Dword
Function PBMain() As Long
Dialog New Pixels, 0, "Test Code",300,300,200,200, %WS_OverlappedWindow To hDlg
Control Add Label, hDlg, 100,"Move/resize, then close the window. When restarted, it will reopen to the saved size/position.", 40,10,150,60
Dialog Show Modal hDlg Call DlgProc
End Function
CallBack Function DlgProc() As Long
Select Case CB.Msg
Case %WM_InitDialog
Settings_INI "get"
Case %WM_Destroy
Settings_INI "save"
End Select
End Function
Sub Settings_INI(Task$)
Local x As Long, y As Long, iResult&
Local xResult As Asciiz*%Max_Path, yResult As Asciiz*%Max_Path
Local temp As Asciiz*%Max_Path, INIFileName As Asciiz*%Max_Path
'defines file name (any file name will work)
INIFileName = Exe.Path$ + "myapp.ini"
If Task$ = "get" Then
'get dialog top/left from INI file and use to set Dialog location
Getprivateprofilestring "All", "Left", "300", xResult, %Max_Path, INIFileName
Getprivateprofilestring "All", "Top", "300", yResult, %Max_Path, INIFileName
Dialog Set Loc hDlg, Val(xResult$), Val(yResult$) 'left/top
'get dialog width/height from INI file and use to set Dialog size
GetPrivateProfileString "All", "Width", "300", xResult, %Max_Path, INIFileName
GetPrivateProfileString "All", "Height", "300", yResult, %Max_Path, INIFileName
Dialog Set Size hDlg,Val(xResult$), Val(yResult$) 'width/height
End If
If Task$ = "save" Then
'save dialog size/location unless minimized or maximized
If IsFalse(IsIconic(hDlg) Or IsZoomed(hDlg)) Then
Dialog Get Loc hDlg To x,y
WritePrivateProfileString "All", "Left", Str$(x), INIFileName
WritePrivateProfileString "All", "Top", Str$(y), INIFileName
Dialog Get Size hDlg To x,y
WritePrivateProfileString "All", "Width", Str$(x), INIFileName
WritePrivateProfileString "All", "Height", Str$(y), INIFileName
End If
End If
End Sub
'gbs_00056
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm