home *** CD-ROM | disk | FTP | other *** search
/ Mastering Visual Basic 5 / MasteringVisualBasic5.iso / ch_code / ch12 / registry / registry.frm (.txt) next >
Encoding:
Visual Basic Form  |  1997-02-20  |  7.5 KB  |  218 lines

  1. VERSION 5.00
  2. Begin VB.Form Form1 
  3.    Caption         =   "Using the Registry"
  4.    ClientHeight    =   4260
  5.    ClientLeft      =   60
  6.    ClientTop       =   345
  7.    ClientWidth     =   4995
  8.    LinkTopic       =   "Form1"
  9.    ScaleHeight     =   4260
  10.    ScaleWidth      =   4995
  11.    StartUpPosition =   3  'Windows Default
  12.    Begin VB.Frame Frame2 
  13.       Caption         =   "Window Height"
  14.       Height          =   1455
  15.       Left            =   120
  16.       TabIndex        =   7
  17.       Top             =   2400
  18.       Width           =   4575
  19.       Begin VB.Label Label10 
  20.          Caption         =   "Subkey Name:"
  21.          Height          =   255
  22.          Left            =   240
  23.          TabIndex        =   11
  24.          Top             =   360
  25.          Width           =   1215
  26.       End
  27.       Begin VB.Label Label9 
  28.          Caption         =   "Subkey Value:"
  29.          Height          =   255
  30.          Left            =   240
  31.          TabIndex        =   10
  32.          Top             =   840
  33.          Width           =   1215
  34.       End
  35.       Begin VB.Label Label8 
  36.          Caption         =   "Label4"
  37.          Height          =   255
  38.          Left            =   1560
  39.          TabIndex        =   9
  40.          Top             =   360
  41.          Width           =   2775
  42.       End
  43.       Begin VB.Label Label7 
  44.          Caption         =   "Label5"
  45.          Height          =   255
  46.          Left            =   1560
  47.          TabIndex        =   8
  48.          Top             =   840
  49.          Width           =   2895
  50.       End
  51.    End
  52.    Begin VB.Frame Frame1 
  53.       Caption         =   "Window Width"
  54.       Height          =   1455
  55.       Left            =   120
  56.       TabIndex        =   1
  57.       Top             =   600
  58.       Width           =   4575
  59.       Begin VB.Label Label5 
  60.          Caption         =   "Label5"
  61.          Height          =   255
  62.          Left            =   1560
  63.          TabIndex        =   5
  64.          Top             =   840
  65.          Width           =   2895
  66.       End
  67.       Begin VB.Label Label4 
  68.          Caption         =   "Label4"
  69.          Height          =   255
  70.          Left            =   1560
  71.          TabIndex        =   4
  72.          Top             =   360
  73.          Width           =   2775
  74.       End
  75.       Begin VB.Label Label3 
  76.          Caption         =   "Subkey Value:"
  77.          Height          =   255
  78.          Left            =   240
  79.          TabIndex        =   3
  80.          Top             =   840
  81.          Width           =   1215
  82.       End
  83.       Begin VB.Label Label2 
  84.          Caption         =   "Subkey Name:"
  85.          Height          =   255
  86.          Left            =   240
  87.          TabIndex        =   2
  88.          Top             =   360
  89.          Width           =   1215
  90.       End
  91.    End
  92.    Begin VB.Label Label6 
  93.       Caption         =   "Label6"
  94.       Height          =   255
  95.       Left            =   1920
  96.       TabIndex        =   6
  97.       Top             =   120
  98.       Width           =   2775
  99.    End
  100.    Begin VB.Label Label1 
  101.       Caption         =   "Registry Key:"
  102.       Height          =   255
  103.       Left            =   240
  104.       TabIndex        =   0
  105.       Top             =   120
  106.       Width           =   1335
  107.    End
  108. Attribute VB_Name = "Form1"
  109. Attribute VB_GlobalNameSpace = False
  110. Attribute VB_Creatable = False
  111. Attribute VB_PredeclaredId = True
  112. Attribute VB_Exposed = False
  113. Option Explicit
  114. Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" _
  115.     (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
  116. Private Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" _
  117.     (ByVal hKey As Long, ByVal lpSubKey As String) As Long
  118. Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" _
  119.     (ByVal hKey As Long, ByVal lpValueName As String) As Long
  120. Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" _
  121.     (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, _
  122.     lpType As Long, lpData As Any, lpcbData As Long) As Long
  123. Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" _
  124.     (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, _
  125.     ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long
  126. Const ERROR_SUCCESS = 0&
  127. Const ERROR_BADDB = 1009&
  128. Const ERROR_BADKEY = 1010&
  129. Const ERROR_CANTOPEN = 1011&
  130. Const ERROR_CANTREAD = 1012&
  131. Const ERROR_CANTWRITE = 1013&
  132. Const ERROR_REGISTRY_RECOVERED = 1014&
  133. Const ERROR_REGISTRY_CORRUPT = 1015&
  134. Const ERROR_REGISTRY_IO_FAILED = 1016&
  135. Const HKEY_CLASSES_ROOT = &H80000000
  136. Const HKEY_CURRENT_USER = &H80000001
  137. Const HKEY_LOCAL_MACHINE = &H80000002
  138. Const REG_SZ = 1
  139. 'Dim regKey  As String
  140. Const regKey = "\Sybex\Mastering VB 5.0"
  141. Private Sub Form_Load()
  142.     Dim retValue As Long
  143.     Dim result As Long
  144.     Dim keyID As Long
  145.     Dim keyValue As String
  146.     Dim subKey As String
  147.     Dim bufSize As Long
  148.     Label6.Caption = regKey
  149.     'Create key
  150.     retValue = RegCreateKey(HKEY_LOCAL_MACHINE, regKey, keyID)
  151.     If retValue = 0 Then
  152.      
  153.         'Create width
  154.         subKey = "Window Width"
  155.         retValue = RegQueryValueEx(keyID, subKey, 0&, REG_SZ, _
  156.                                     0&, bufSize)
  157.                                     
  158.         'No value, set it
  159.         If bufSize < 2 Then
  160.             keyValue = Me.Width
  161.             retValue = RegSetValueEx(keyID, subKey, 0&, _
  162.                                         REG_SZ, ByVal keyValue, Len(keyValue) + 1)
  163.         Else
  164.                                     
  165.             keyValue = String(bufSize + 1, " ")
  166.             
  167.             retValue = RegQueryValueEx(keyID, subKey, 0&, REG_SZ, _
  168.                                         ByVal keyValue, bufSize)
  169.             
  170.             keyValue = Left$(keyValue, bufSize - 1)
  171.             
  172.             Me.Width = keyValue
  173.         End If
  174.         
  175.         'Set values on form
  176.         Label4.Caption = subKey
  177.         Label5.Caption = Me.Width
  178.         
  179.         'Create height
  180.         subKey = "Window Height"
  181.         retValue = RegQueryValueEx(keyID, subKey, 0&, REG_SZ, _
  182.                                     0&, bufSize)
  183.                                     
  184.         If bufSize < 2 Then
  185.             keyValue = Me.Height
  186.             retValue = RegSetValueEx(keyID, subKey, 0&, _
  187.                                         REG_SZ, ByVal keyValue, Len(keyValue) + 1)
  188.         Else
  189.         
  190.             keyValue = String(bufSize + 1, " ")
  191.             
  192.             retValue = RegQueryValueEx(keyID, subKey, 0&, REG_SZ, _
  193.                                         ByVal keyValue, bufSize)
  194.             
  195.             keyValue = Left$(keyValue, bufSize - 1)
  196.             
  197.             Me.Height = keyValue
  198.         End If
  199.         
  200.         'Set values on form
  201.         Label8.Caption = subKey
  202.         Label7.Caption = Me.Height
  203.     End If
  204.                                     
  205. End Sub
  206. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
  207.     Dim keyValue As String
  208.     Dim retValue As Long
  209.     Dim keyID As Long
  210.     retValue = RegCreateKey(HKEY_LOCAL_MACHINE, regKey, keyID)
  211.     keyValue = Me.Width
  212.     retValue = RegSetValueEx(keyID, "Window Width", 0&, _
  213.                                 REG_SZ, ByVal keyValue, Len(keyValue) + 1)
  214.     keyValue = Me.Height
  215.     retValue = RegSetValueEx(keyID, "Window Height", 0&, _
  216.                                 REG_SZ, ByVal keyValue, Len(keyValue) + 1)
  217. End Sub
  218.