DEK25,
This program, using API calls, stores and retrieves the form size parameters in the windows registry under your name (DEK25) as registry key and "your program" (DEK25sProgram) as sub string, upon closing and running the program, respectively.
Copy and paste this code on a form and add the 8 or nine labels as indicated.
Remove the msgbox calls if you don't want to see them.
Run the program, resize the form dramatically (dragging the edges) and end by clicking the X box in the top right corner -- this action WRITES (input) to the registry.
When you run it again, the form appears the same size it was last by obtaining those values stored in the registry upon last closing -- this is a READ (Get) from the registry.
This will serve as a tutorial and you be able to adapt it to
your uses.
Evangelos Petroutsos's "Mastering Visual Basic 6" deserves credit for most of this bit.
Hope this helps,
Ortho
Option Explicit
Private Declare Function RegCreateKey Lib "advapi32.dll" _
Alias "RegCreateKeyA" (ByVal hKey As Long, _
ByVal lpSubKey As String, phkResult _
As Long) As Long
Private Declare Function RegDeleteKey Lib "advapi32.dll" _
Alias "RegDeleteKeyA" (ByVal hKey As Long, _
ByVal lpSubKey As String) As Long
Private Declare Function RegDeleteValue Lib "advapi32.dll" _
Alias "RegDeleteValueA" (ByVal hKey As Long, _
ByVal lpValueName As String) As Long
Private Declare Function RegQueryValueEx Lib "advapi32.dll" _
Alias "RegQueryValueExA" (ByVal hKey As Long, _
ByVal lpValueName As String, ByVal lpReserved As Long, _
lpType As Long, lpData As Any, lpcbData As Long) As Long
Private Declare Function RegSetValueEx Lib "advapi32.dll" _
Alias "RegSetValueExA" (ByVal hKey As Long, _
ByVal lpValueName As String, ByVal Reserved As Long, _
ByVal dwType As Long, lpData As Any, _
ByVal cbData As Long) As Long
Const ERROR_SUCCESS = 0&
Const ERROR_BADDB = 1009&
Const ERROR_BADKEY = 1010&
Const ERROR_CANTOPEN = 1011&
Const ERROR_CANTREAD = 1012&
Const ERROR_CANTWRITE = 1013&
Const ERROR_REGISTRY_RECOVERED = 1014&
Const ERROR_REGISTRY_CORRUPT = 1015&
Const ERROR_REGISTRY_IO_FAILED = 1016&
Const HKEY_CLASSES_ROOT = &H80000000
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const REG_SZ = 1
'dim regKey as string
Const regKey = "\DEK25\DEK25sProgram"
Private Sub Form_Load()
Dim retValue As Long
Dim result As Long
Dim keyID As Long
Dim keyValue As String
Dim subKey As String
Dim bufSize As Long
Label6.Caption = regKey
'Create key
retValue = RegCreateKey(HKEY_LOCAL_MACHINE, regKey, keyID)
MsgBox "RetValue: " & retValue
MsgBox "BufSize: " & bufSize
'since there is NO code after this "if", retValue must ALWAYS equal "0."
If retValue = 0 Then
' Save width
subKey = "Window Width"
retValue = RegQueryValueEx(keyID, subKey, 0&, _
REG_SZ, 0&, bufSize)
MsgBox "RetValue: " & retValue
MsgBox "BufSize: " & bufSize
' No value, set it
If bufSize < 2 Then
keyValue = Me.Width
retValue = RegSetValueEx(keyID, subKey, 0&, _
REG_SZ, ByVal keyValue, Len(keyValue) + 1)
Else
'value present, get it
keyValue = String(bufSize + 1, " ")
retValue = RegQueryValueEx(keyID, subKey, _
0&, REG_SZ, ByVal keyValue, bufSize)
keyValue = Left$(keyValue, bufSize - 1)
Me.Width = keyValue
End If
' Set values on form
Label4.Caption = subKey
Label5.Caption = Me.Width
' Save height
subKey = "Window Height"
retValue = RegQueryValueEx(keyID, subKey, 0&, _
REG_SZ, 0&, bufSize)
If bufSize < 2 Then
keyValue = Me.Height
retValue = RegSetValueEx(keyID, subKey, 0&, _
REG_SZ, ByVal keyValue, Len(keyValue) + 1)
Else
keyValue = String(bufSize + 1, " ")
retValue = RegQueryValueEx(keyID, subKey, 0&, REG_SZ, _
ByVal keyValue, bufSize)
keyValue = Left$(keyValue, bufSize - 1)
Me.Height = keyValue
End If
' Set values on form
Label8.Caption = subKey
Label7.Caption = Me.Height
End If
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Dim keyValue As String
Dim retValue As Long, keyID As Long
retValue = RegCreateKey(HKEY_LOCAL_MACHINE, regKey, keyID)
keyValue = Me.Width
retValue = RegSetValueEx(keyID, "Window Width", 0&, _
REG_SZ, ByVal keyValue, Len(keyValue) + 1)
keyValue = Me.Height
retValue = RegSetValueEx(keyID, "Window Height", 0&, _
REG_SZ, ByVal keyValue, Len(keyValue) + 1)
End Sub