首先新建一個(gè)工程,在新建的窗體上添加以下控件并設(shè)置相應(yīng)屬性:
控件名 屬性 值
Label1 Caption
注冊(cè)表鍵值:
Label2 Caption 注冊(cè)姓名:
Label3 Caption 空
Label4 Caption 注冊(cè)公司:
Label5 Caption 空
Label6 Caption 空
Text1 Text 空
Text2 Text 空
Command1 Caption 寫入
注冊(cè)表 Command2 Caption 退出
然后,在程序的聲明部分添加如下代碼:
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 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
Const regkey = "softwareMy SoftMy program"
接著為各個(gè)控件添加代碼,代碼如下:
Private Sub Command1_Click()
Dim keyvalue As String
Dim retvalue As Long
Dim keyid As Long
retvalue = RegCreateKey(h da0 key_local_machine, regkey, keyid)
keyvalue = Text1.Text
retvalue = RegSetValueEx(keyid, "注冊(cè)名", 0&, REG_SZ, ByVal keyvalue, Len(keyvalue) 1)
keyvalue = Text2.Text
retvalue = RegSetValueEx(keyid, "注冊(cè)公司", 0&, REG_SZ, ByVal keyvalue, Len(keyvalue) 1)
If Text1.Text <> "" And Text2.Text <> "" Then
Label3.Caption = Text1.Text
Label5.Caption = Text2.Text
End If
End Sub
Private Sub Command2_Click()
Unload Me
End
End Sub
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
retvalue = RegCreateKey(hkey_local_machine, regkey, keyid)
If retvalue = 0 Then
subkey = "注冊(cè)名"
retvalue = RegQueryValueEx(keyid, subkey, 0&, REG_SZ, 0&, bufsize)
If bufsize < 2 Then
keyvalue = ""
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)
Text1.Text = keyvalue
End If
Label3.Caption = keyvalue
subkey = "注冊(cè)公司"
retvalue = RegQueryValueEx(keyid, subkey, 0&, REG_SZ, 0&, bufsize)
If bufsize < 2 Then
keyvalue = ""
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)
Text2.Text = keyvalue
End If
Label5.Caption = keyvalue
End If
End Sub
好了,存盤,按F5運(yùn)行程序,在兩個(gè)Text中分別寫入你的“注冊(cè)姓名”和“注冊(cè)公司”的名稱,然后單擊
“寫入
注冊(cè)表”按鈕,現(xiàn)在打開“
注冊(cè)表編輯器”,找到HKEY_LOCAL_MACHINESOFTWAREMy SoftMy Program,看看右邊是不是生成了“注冊(cè)名”和“注冊(cè)公司”兩個(gè)鍵值,是的話,恭喜你,你成功了。