home *** CD-ROM | disk | FTP | other *** search
/ Mastering Visual Basic 6 / mastvb6.iso / ch_code / ch13 / registry / registry.frm (.txt) next >
Encoding:
Visual Basic Form  |  1996-04-30  |  7.1 KB  |  206 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.          Height          =   255
  37.          Left            =   1560
  38.          TabIndex        =   9
  39.          Top             =   360
  40.          Width           =   2775
  41.       End
  42.       Begin VB.Label Label7 
  43.          Height          =   255
  44.          Left            =   1560
  45.          TabIndex        =   8
  46.          Top             =   840
  47.          Width           =   2895
  48.       End
  49.    End
  50.    Begin VB.Frame Frame1 
  51.       Caption         =   "Window Width"
  52.       Height          =   1455
  53.       Left            =   120
  54.       TabIndex        =   1
  55.       Top             =   600
  56.       Width           =   4575
  57.       Begin VB.Label Label5 
  58.          Height          =   255
  59.          Left            =   1560
  60.          TabIndex        =   5
  61.          Top             =   840
  62.          Width           =   2895
  63.       End
  64.       Begin VB.Label Label4 
  65.          Height          =   255
  66.          Left            =   1560
  67.          TabIndex        =   4
  68.          Top             =   360
  69.          Width           =   2775
  70.       End
  71.       Begin VB.Label Label3 
  72.          Caption         =   "Subkey Value:"
  73.          Height          =   255
  74.          Left            =   240
  75.          TabIndex        =   3
  76.          Top             =   840
  77.          Width           =   1215
  78.       End
  79.       Begin VB.Label Label2 
  80.          Caption         =   "Subkey Name:"
  81.          Height          =   255
  82.          Left            =   240
  83.          TabIndex        =   2
  84.          Top             =   360
  85.          Width           =   1215
  86.       End
  87.    End
  88.    Begin VB.Label Label6 
  89.       Height          =   255
  90.       Left            =   1920
  91.       TabIndex        =   6
  92.       Top             =   120
  93.       Width           =   2775
  94.    End
  95.    Begin VB.Label Label1 
  96.       Caption         =   "Registry Key:"
  97.       Height          =   255
  98.       Left            =   240
  99.       TabIndex        =   0
  100.       Top             =   120
  101.       Width           =   1335
  102.    End
  103. Attribute VB_Name = "Form1"
  104. Attribute VB_GlobalNameSpace = False
  105. Attribute VB_Creatable = False
  106. Attribute VB_PredeclaredId = True
  107. Attribute VB_Exposed = False
  108. '  ******************************
  109. '  ******************************
  110. '  ** MASTERING VB6            **
  111. '  ** by Evangelos Petroutos   **
  112. '  ** SYBEX, 1998              **
  113. '  ******************************
  114. '  ******************************
  115. Option Explicit
  116. Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" _
  117.     (ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
  118. Private Declare Function RegDeleteKey Lib "advapi32.dll" Alias "RegDeleteKeyA" _
  119.     (ByVal hKey As Long, ByVal lpSubKey As String) As Long
  120. Private Declare Function RegDeleteValue Lib "advapi32.dll" Alias "RegDeleteValueA" _
  121.     (ByVal hKey As Long, ByVal lpValueName As String) As Long
  122. Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" _
  123.     (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, _
  124.     lpType As Long, lpData As Any, lpcbData As Long) As Long
  125. Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" _
  126.     (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, _
  127.     ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long
  128. Const ERROR_SUCCESS = 0&
  129. Const ERROR_BADDB = 1009&
  130. Const ERROR_BADKEY = 1010&
  131. Const ERROR_CANTOPEN = 1011&
  132. Const ERROR_CANTREAD = 1012&
  133. Const ERROR_CANTWRITE = 1013&
  134. Const ERROR_REGISTRY_RECOVERED = 1014&
  135. Const ERROR_REGISTRY_CORRUPT = 1015&
  136. Const ERROR_REGISTRY_IO_FAILED = 1016&
  137. Const HKEY_CLASSES_ROOT = &H80000000
  138. Const HKEY_CURRENT_USER = &H80000001
  139. Const HKEY_LOCAL_MACHINE = &H80000002
  140. Const REG_SZ = 1
  141. Const regKey = "\Sybex\Mastering VB 5.0"
  142. Private Sub Form_Load()
  143. Dim retValue As Long
  144. Dim result As Long
  145. Dim keyID As Long
  146. Dim keyValue As String
  147. Dim subKey As String
  148. Dim bufSize As Long
  149.     Label6.Caption = regKey
  150.     'Create key
  151.     retValue = RegCreateKey(HKEY_LOCAL_MACHINE, regKey, keyID)
  152.     If retValue = 0 Then
  153.         'Create width
  154.         subKey = "Window Width"
  155.         retValue = RegQueryValueEx(keyID, subKey, 0&, REG_SZ, _
  156.                    0&, bufSize)
  157.         'No value, set it
  158.         If bufSize < 2 Then
  159.             keyValue = Me.Width
  160.             retValue = RegSetValueEx(keyID, subKey, 0&, _
  161.                                         REG_SZ, ByVal keyValue, Len(keyValue) + 1)
  162.         Else
  163.                                     
  164.             keyValue = String(bufSize + 1, " ")
  165.             retValue = RegQueryValueEx(keyID, subKey, 0&, REG_SZ, _
  166.                        ByVal keyValue, bufSize)
  167.             keyValue = Left$(keyValue, bufSize - 1)
  168.             Me.Width = keyValue
  169.         End If
  170.         'Set values on form
  171.         Label4.Caption = subKey
  172.         Label5.Caption = Me.Width
  173.         'Create height
  174.         subKey = "Window Height"
  175.         retValue = RegQueryValueEx(keyID, subKey, 0&, REG_SZ, _
  176.                    0&, bufSize)
  177.         If bufSize < 2 Then
  178.             keyValue = Me.Height
  179.             retValue = RegSetValueEx(keyID, subKey, 0&, _
  180.                        REG_SZ, ByVal keyValue, Len(keyValue) + 1)
  181.         Else
  182.             keyValue = String(bufSize + 1, " ")
  183.             retValue = RegQueryValueEx(keyID, subKey, 0&, REG_SZ, _
  184.                        ByVal keyValue, bufSize)
  185.             keyValue = Left$(keyValue, bufSize - 1)
  186.             Me.Height = keyValue
  187.         End If
  188.         'Set values on form
  189.         Label8.Caption = subKey
  190.         Label7.Caption = Me.Height
  191.     End If
  192. End Sub
  193. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
  194. Dim keyValue As String
  195. Dim retValue As Long
  196. Dim keyID As Long
  197.     retValue = RegCreateKey(HKEY_LOCAL_MACHINE, regKey, keyID)
  198.     keyValue = Me.Width
  199.     retValue = RegSetValueEx(keyID, "Window Width", 0&, _
  200.                REG_SZ, ByVal keyValue, Len(keyValue) + 1)
  201.     keyValue = Me.Height
  202.     retValue = RegSetValueEx(keyID, "Window Height", 0&, _
  203.                REG_SZ, ByVal keyValue, Len(keyValue) + 1)
  204.     MsgBox "Registry updated"
  205. End Sub
  206.