VB.NET 參考代碼:
創(chuàng)新互聯(lián)公司-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設、高性價比梨樹網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式梨樹網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設找我們,業(yè)務覆蓋梨樹地區(qū)。費用合理售后完善,十多年實體公司更值得信賴。
Imports?Microsoft.Win32
Public?Class?Form1
Private?Sub?Button1_Click(sender?As?Object,?e?As?EventArgs)?Handles?Button1.Click
Dim?regKey?As?RegistryKey?=?Registry.LocalMachine.OpenSubKey("SYSTEM\CurrentControlSet\Control\Windows",?False)
For?Each?strSubKeyName?In?regKey.GetValueNames()
If?regKey.GetValueKind(strSubKeyName)?=?RegistryValueKind.String?Then
MessageBox.Show(regKey.GetValue(strSubKeyName))
'?regKey.SetValue(strSubKeyName,?"修改后的值")
End?If
Next
End?Sub
End?Class
將窗體KeyPreview屬性設置為True并對Form_KeyDown事件編程,可以獲取鍵盤箭頭鍵的鍵值。實現(xiàn)方法如下:
Option?Explicit
Private?Sub?Form_Load()
'允許窗體攔截鍵盤按鍵
Me.KeyPreview?=?True
End?Sub
Private?Sub?Form_KeyDown(KeyCode?As?Integer,?Shift?As?Integer)
'?KeyCode?鍵盤碼是鍵盤上每個按鍵的唯一編碼
'?在VB中,符號常數(shù)表示鍵盤碼,例如?vbKeyF1?表示F1鍵,
'?vbKeyHome?表示HOME鍵……?
Select?Case?KeyCode
Case?vbKeyLeft:?????Print?"左箭頭鍵值:"?+?CStr(KeyCode)
Case?vbKeyRight:????Print?"右箭頭鍵值:"?+?CStr(KeyCode)
Case?vbKeyUp:???????Print?"上箭頭鍵值:"?+?CStr(KeyCode)
Case?vbKeyDown:?????Print?"下箭頭鍵值:"?+?CStr(KeyCode)
End?Select
End?Sub
Private?Sub?Form1_KeyPress(ByVal?sender?As?Object,?ByVal?e?As?System.Windows.Forms.KeyPressEventArgs)?Handles?Me.KeyPress
If?Asc(0)?Then
textbox1.text="0"
end?if
end?sub
從一個VB編寫的注冊表程序中整理出來的,功能是獲取注冊表指定鍵值,內(nèi)含注釋,模塊可用于注冊表的操作中:
01
Function GetRegistryValue(ByVal hKey As RootKeyEnum, ByVal KeyName As String, _
02
ByVal ValueName As String, Optional DefaultValue As Variant) As Variant
03
Dim handle As Long
04
Dim resLong As Long
05
Dim resString As String
06
Dim resBinary() As Byte
07
Dim length As Long
08
Dim retVal As Long
09
Dim valueType As Long
10
Const KEY_READ = H20019
11
'// 默認結(jié)果
12
GetRegistryValue = IIf(IsMissing(DefaultValue), Empty, DefaultValue)
13
'// 打開鍵, 不存在則退出
14
If RegOpenKeyEx(hKey, KeyName, 0, KEY_READ, handle) Then Exit Function
15
'// 準備 1K resBinary 用于接收
16
length = 1024
17
ReDim resBinary(0 To length - 1) As Byte
18
'// 讀注冊表值
19
retVal = RegQueryValueEx(handle, ValueName, 0, valueType, resBinary(0), length)
20
'// 若resBinary 太小則重讀
21
If retVal = ERROR_MORE_DATA Then
22
'// resBinary放大,且重新讀取
23
ReDim resBinary(0 To length - 1) As Byte
24
retVal = RegQueryValueEx(handle, ValueName, 0, valueType, resBinary(0), _
25
length)
26
End If
27
'// 返回相應值類型
28
Select Case valueType
29
Case REG_DWORD, REG_DWORD_LITTLE_ENDIAN
30
'// REG_DWORD 和 REG_DWORD_LITTLE_ENDIAN 相同
31
CopyMemory resLong, resBinary(0), 4
32
GetRegistryValue = resLong
33
Case REG_DWORD_BIG_ENDIAN
34
'// Big Endian's 用在非-Windows環(huán)境, 如Unix系統(tǒng), 本地計算機遠程訪問
35
CopyMemory resLong, resBinary(0), 4
36
GetRegistryValue = SwapEndian(resLong)
37
Case REG_SZ, REG_EXPAND_SZ
38
resString = Space$(length - 1)
39
CopyMemory ByVal resString, resBinary(0), length - 1
40
If valueType = REG_EXPAND_SZ Then
41
'// 查詢對應的環(huán)境變量
42
GetRegistryValue = ExpandEnvStr(resString)
43
Else
44
GetRegistryValue = resString
45
End If
46
Case REG_MULTI_SZ
47
'// 復制時需指定2個空格符
48
resString = Space$(length - 2)
49
CopyMemory ByVal resString, resBinary(0), length - 2
50
GetRegistryValue = resString
51
Case Else ' 包含 REG_BINARY
52
'// resBinary 調(diào)整
53
If length UBound(resBinary) + 1 Then
54
ReDim Preserve resBinary(0 To length - 1) As Byte
55
End If
56
GetRegistryValue = resBinary()
57
End Select
58
'// 關閉注冊表