Android:怎样在代码中获得平板的开关机时间?
1个回答
展开全部
这个代码分为三个功能,一是让程序随系统自启动、二是在关机时(即退出本程序时)记录关机时间、三是本程序启动时显示前三次的关机时间。
一:让程序随系统自启动应该就不用说了吧,简单
二:关机记录时间可以在本程序的unload模块中写下记录过程,即 时间变量=time 即可,当然还要同时保存到某个文件或注册表的三个固定容器中,保存时注意三个位置的替换即可(删除第一个时间、将第二个移到第一个、第三个移到第二个、将本次记录的时间写到第三个位置)。
三:启动程序显示前三次关机时间也非常简单,显示时就将三个显示控件的caption等于上面第二条的这三个时间即可。
明白了吗,你这个代码实现起来确是非常简单的,一点都不难哦。
代码如下,自己注意安排控件哦【在默认form1窗体上加载Label1(0)、Label1(1)、Label1(2)、text1(0)、text1(1)、text1(2)、text2 、timer1 】
Dim a As String, b As String, c As String
Private Const REG_SZ As Long = 1
Private Const REG_DWORD As Long = 4
Private Const HKEY_CLASSES_ROOT = &H80000000
Private Const HKEY_CURRENT_USER = &H80000001
Private Const HKEY_LOCAL_MACHINE = &H80000002
Private Const HKEY_USERS = &H80000003
Private Const ERROR_NONE = 0
Private Const ERROR_BADDB = 1
Private Const ERROR_BADKEY = 2
Private Const ERROR_CANTOPEN = 3
Private Const ERROR_CANTREAD = 4
Private Const ERROR_CANTWRITE = 5
Private Const ERROR_OUTOFMEMORY = 6
Private Const ERROR_INVALID_PARAMETER = 7
Private Const ERROR_ACCESS_DENIED = 8
Private Const ERROR_INVALID_PARAMETERS = 87
Private Const ERROR_NO_MORE_ITEMS = 259
Private Const KEY_ALL_ACCESS = &H3F
Private Const REG_OPTION_NON_VOLATILE = 0
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Private Declare Function RegCreateKeyEx Lib "advapi32.dll" Alias "RegCreateKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, ByVal lpClass As String, ByVal dwOptions As Long, ByVal samDesired As Long, ByVal lpSecurityAttributes As Long, phkResult As Long, lpdwDisposition As Long) As Long
Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
Private Declare Function RegQueryValueExString Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, ByVal lpData As String, lpcbData As Long) As Long
Private Declare Function RegQueryValueExLong Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Long, lpcbData As Long) As Long
Private Declare Function RegQueryValueExNULL Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, ByVal lpData As Long, lpcbData As Long) As Long
Private Declare Function RegSetValueExString Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByVal lpValue As String, ByVal cbData As Long) As Long
Private Declare Function RegSetValueExLong Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpValue As Long, ByVal cbData As Long) As Long
Private Declare Function RegDeleteKey& Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String)
Private Declare Function RegDeleteValue& Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String)
Function SetValueEx(ByVal hKey As Long, sValueName As String, lType As Long, vValue As Variant) As Long
Dim lValue As Long
Dim sValue As String
Select Case lType
Case REG_SZ
sValue = vValue
SetValueEx = RegSetValueExString(hKey, sValueName, 0&, lType, sValue, Len(sValue) * 2)
Case REG_DWORD
lValue = vValue
SetValueEx = RegSetValueExLong(hKey, sValueName, 0&, lType, lValue, 4)
End Select
End Function
Function QueryValueEx(ByVal lhKey As Long, ByVal szValueName As String, vValue As Variant) As Long
Dim cch As Long
Dim lrc As Long
Dim lType As Long
Dim lValue As Long
Dim sValue As String
On Error GoTo QueryValueExError
lrc = RegQueryValueExNULL(lhKey, szValueName, 0&, lType, 0&, cch)
If lrc <> ERROR_NONE Then Error 5
Select Case lType
Case REG_SZ:
sValue = String(cch, 0)
lrc = RegQueryValueExString(lhKey, szValueName, 0&, lType, sValue, cch)
If lrc = ERROR_NONE Then
vValue = Left(sValue, cch - 1)
Else
vValue = Empty
End If
Case REG_DWORD:
lrc = RegQueryValueExLong(lhKey, szValueName, 0&, lType, lValue, cch)
If lrc = ERROR_NONE Then vValue = lValue
Case Else
lrc = -1
End Select
QueryValueExExit:
QueryValueEx = lrc
Exit Function
QueryValueExError:
Resume QueryValueExExit
End Function
Private Function CreateNewKey(lPredefinedKey As Long, sNewKeyName As String)
Dim hNewKey As Long
Dim lRetVal As Long
lRetVal = RegCreateKeyEx(lPredefinedKey, sNewKeyName, 0&, vbNullString, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, 0&, hNewKey, lRetVal)
RegCloseKey (hNewKey)
End Function
Private Function SetKeyValue(lPredefinedKey As Long, sKeyName As String, sValueName As String, vValueSetting As Variant, lValueType As Long)
Dim lRetVal As Long
Dim hKey As Long
lRetVal = RegOpenKeyEx(lPredefinedKey, sKeyName, 0, KEY_ALL_ACCESS, hKey)
lRetVal = SetValueEx(hKey, sValueName, lValueType, vValueSetting)
RegCloseKey (hKey)
End Function
Private Function QueryValue(lPredefinedKey As Long, sKeyName As String, sValueName As String)
Dim lRetVal As Long
Dim hKey As Long
Dim vValue As Variant
lRetVal = RegOpenKeyEx(lPredefinedKey, sKeyName, 0, KEY_ALL_ACCESS, hKey)
lRetVal = QueryValueEx(hKey, sValueName, vValue)
QueryValue = vValue
RegCloseKey (hKey)
End Function
Private Sub Form_Load()
SetKeyValue HKEY_LOCAL_MACHINE, "software\microsoft\windows\currentversion\run", "jilu", App.Path & App.EXEName & ".exe", REG_SZ
Text2.Visible = False
If QueryValue(HKEY_CURRENT_USER, "Software\jilu", "on") = "" Then
CreateNewKey HKEY_CURRENT_USER, "Software\jilu"
SetKeyValue HKEY_CURRENT_USER, "Software\jilu", "on", "1", REG_SZ
SetKeyValue HKEY_CURRENT_USER, "Software\jilu", "off", "0", REG_SZ
Text2.Visible = True
End If
If App.PrevInstance = True Then
SetKeyValue HKEY_CURRENT_USER, "Software\jilu", "off", "0", REG_SZ
End
End If
If QueryValue(HKEY_CURRENT_USER, "Software\jilu", "1") <> "" Then a = QueryValue(HKEY_CURRENT_USER, "Software\jilu", "1") Else a = "没有记录"
If QueryValue(HKEY_CURRENT_USER, "Software\jilu", "2") <> "" Then b = QueryValue(HKEY_CURRENT_USER, "Software\jilu", "2") Else b = "没有记录"
If QueryValue(HKEY_CURRENT_USER, "Software\jilu", "3") <> "" Then c = QueryValue(HKEY_CURRENT_USER, "Software\jilu", "3") Else c = "没有记录"
Text1(0).Text = a: Text1(1).Text = b: Text1(2).Text = c
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Cancel = 1: Me.Hide
SetKeyValue HKEY_CURRENT_USER, "Software\jilu", "off", "1", REG_SZ
End Sub
Private Sub Form_Unload(Cancel As Integer)
Dim d As String, x As String, y As String
d = CStr(Now())
If QueryValue(HKEY_CURRENT_USER, "Software\jilu", "2") <> "" Then x = QueryValue(HKEY_CURRENT_USER, "Software\jilu", "2") Else x = "没有记录"
If QueryValue(HKEY_CURRENT_USER, "Software\jilu", "3") <> "" Then y = QueryValue(HKEY_CURRENT_USER, "Software\jilu", "3") Else y = "没有记录"
SetKeyValue HKEY_CURRENT_USER, "Software\jilu", "1", x, REG_SZ
SetKeyValue HKEY_CURRENT_USER, "Software\jilu", "2", y, REG_SZ
SetKeyValue HKEY_CURRENT_USER, "Software\jilu", "3", d, REG_SZ
End Sub
Private Sub Timer1_Timer()
If QueryValue(HKEY_CURRENT_USER, "Software\jilu", "off") = "0" Then Me.Show
End Sub
一:让程序随系统自启动应该就不用说了吧,简单
二:关机记录时间可以在本程序的unload模块中写下记录过程,即 时间变量=time 即可,当然还要同时保存到某个文件或注册表的三个固定容器中,保存时注意三个位置的替换即可(删除第一个时间、将第二个移到第一个、第三个移到第二个、将本次记录的时间写到第三个位置)。
三:启动程序显示前三次关机时间也非常简单,显示时就将三个显示控件的caption等于上面第二条的这三个时间即可。
明白了吗,你这个代码实现起来确是非常简单的,一点都不难哦。
代码如下,自己注意安排控件哦【在默认form1窗体上加载Label1(0)、Label1(1)、Label1(2)、text1(0)、text1(1)、text1(2)、text2 、timer1 】
Dim a As String, b As String, c As String
Private Const REG_SZ As Long = 1
Private Const REG_DWORD As Long = 4
Private Const HKEY_CLASSES_ROOT = &H80000000
Private Const HKEY_CURRENT_USER = &H80000001
Private Const HKEY_LOCAL_MACHINE = &H80000002
Private Const HKEY_USERS = &H80000003
Private Const ERROR_NONE = 0
Private Const ERROR_BADDB = 1
Private Const ERROR_BADKEY = 2
Private Const ERROR_CANTOPEN = 3
Private Const ERROR_CANTREAD = 4
Private Const ERROR_CANTWRITE = 5
Private Const ERROR_OUTOFMEMORY = 6
Private Const ERROR_INVALID_PARAMETER = 7
Private Const ERROR_ACCESS_DENIED = 8
Private Const ERROR_INVALID_PARAMETERS = 87
Private Const ERROR_NO_MORE_ITEMS = 259
Private Const KEY_ALL_ACCESS = &H3F
Private Const REG_OPTION_NON_VOLATILE = 0
Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Private Declare Function RegCreateKeyEx Lib "advapi32.dll" Alias "RegCreateKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, ByVal lpClass As String, ByVal dwOptions As Long, ByVal samDesired As Long, ByVal lpSecurityAttributes As Long, phkResult As Long, lpdwDisposition As Long) As Long
Private Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
Private Declare Function RegQueryValueExString Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, ByVal lpData As String, lpcbData As Long) As Long
Private Declare Function RegQueryValueExLong Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Long, lpcbData As Long) As Long
Private Declare Function RegQueryValueExNULL Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, ByVal lpData As Long, lpcbData As Long) As Long
Private Declare Function RegSetValueExString Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByVal lpValue As String, ByVal cbData As Long) As Long
Private Declare Function RegSetValueExLong Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpValue As Long, ByVal cbData As Long) As Long
Private Declare Function RegDeleteKey& Lib "advapi32.dll" Alias "RegDeleteKeyA" (ByVal hKey As Long, ByVal lpSubKey As String)
Private Declare Function RegDeleteValue& Lib "advapi32.dll" Alias "RegDeleteValueA" (ByVal hKey As Long, ByVal lpValueName As String)
Function SetValueEx(ByVal hKey As Long, sValueName As String, lType As Long, vValue As Variant) As Long
Dim lValue As Long
Dim sValue As String
Select Case lType
Case REG_SZ
sValue = vValue
SetValueEx = RegSetValueExString(hKey, sValueName, 0&, lType, sValue, Len(sValue) * 2)
Case REG_DWORD
lValue = vValue
SetValueEx = RegSetValueExLong(hKey, sValueName, 0&, lType, lValue, 4)
End Select
End Function
Function QueryValueEx(ByVal lhKey As Long, ByVal szValueName As String, vValue As Variant) As Long
Dim cch As Long
Dim lrc As Long
Dim lType As Long
Dim lValue As Long
Dim sValue As String
On Error GoTo QueryValueExError
lrc = RegQueryValueExNULL(lhKey, szValueName, 0&, lType, 0&, cch)
If lrc <> ERROR_NONE Then Error 5
Select Case lType
Case REG_SZ:
sValue = String(cch, 0)
lrc = RegQueryValueExString(lhKey, szValueName, 0&, lType, sValue, cch)
If lrc = ERROR_NONE Then
vValue = Left(sValue, cch - 1)
Else
vValue = Empty
End If
Case REG_DWORD:
lrc = RegQueryValueExLong(lhKey, szValueName, 0&, lType, lValue, cch)
If lrc = ERROR_NONE Then vValue = lValue
Case Else
lrc = -1
End Select
QueryValueExExit:
QueryValueEx = lrc
Exit Function
QueryValueExError:
Resume QueryValueExExit
End Function
Private Function CreateNewKey(lPredefinedKey As Long, sNewKeyName As String)
Dim hNewKey As Long
Dim lRetVal As Long
lRetVal = RegCreateKeyEx(lPredefinedKey, sNewKeyName, 0&, vbNullString, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, 0&, hNewKey, lRetVal)
RegCloseKey (hNewKey)
End Function
Private Function SetKeyValue(lPredefinedKey As Long, sKeyName As String, sValueName As String, vValueSetting As Variant, lValueType As Long)
Dim lRetVal As Long
Dim hKey As Long
lRetVal = RegOpenKeyEx(lPredefinedKey, sKeyName, 0, KEY_ALL_ACCESS, hKey)
lRetVal = SetValueEx(hKey, sValueName, lValueType, vValueSetting)
RegCloseKey (hKey)
End Function
Private Function QueryValue(lPredefinedKey As Long, sKeyName As String, sValueName As String)
Dim lRetVal As Long
Dim hKey As Long
Dim vValue As Variant
lRetVal = RegOpenKeyEx(lPredefinedKey, sKeyName, 0, KEY_ALL_ACCESS, hKey)
lRetVal = QueryValueEx(hKey, sValueName, vValue)
QueryValue = vValue
RegCloseKey (hKey)
End Function
Private Sub Form_Load()
SetKeyValue HKEY_LOCAL_MACHINE, "software\microsoft\windows\currentversion\run", "jilu", App.Path & App.EXEName & ".exe", REG_SZ
Text2.Visible = False
If QueryValue(HKEY_CURRENT_USER, "Software\jilu", "on") = "" Then
CreateNewKey HKEY_CURRENT_USER, "Software\jilu"
SetKeyValue HKEY_CURRENT_USER, "Software\jilu", "on", "1", REG_SZ
SetKeyValue HKEY_CURRENT_USER, "Software\jilu", "off", "0", REG_SZ
Text2.Visible = True
End If
If App.PrevInstance = True Then
SetKeyValue HKEY_CURRENT_USER, "Software\jilu", "off", "0", REG_SZ
End
End If
If QueryValue(HKEY_CURRENT_USER, "Software\jilu", "1") <> "" Then a = QueryValue(HKEY_CURRENT_USER, "Software\jilu", "1") Else a = "没有记录"
If QueryValue(HKEY_CURRENT_USER, "Software\jilu", "2") <> "" Then b = QueryValue(HKEY_CURRENT_USER, "Software\jilu", "2") Else b = "没有记录"
If QueryValue(HKEY_CURRENT_USER, "Software\jilu", "3") <> "" Then c = QueryValue(HKEY_CURRENT_USER, "Software\jilu", "3") Else c = "没有记录"
Text1(0).Text = a: Text1(1).Text = b: Text1(2).Text = c
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Cancel = 1: Me.Hide
SetKeyValue HKEY_CURRENT_USER, "Software\jilu", "off", "1", REG_SZ
End Sub
Private Sub Form_Unload(Cancel As Integer)
Dim d As String, x As String, y As String
d = CStr(Now())
If QueryValue(HKEY_CURRENT_USER, "Software\jilu", "2") <> "" Then x = QueryValue(HKEY_CURRENT_USER, "Software\jilu", "2") Else x = "没有记录"
If QueryValue(HKEY_CURRENT_USER, "Software\jilu", "3") <> "" Then y = QueryValue(HKEY_CURRENT_USER, "Software\jilu", "3") Else y = "没有记录"
SetKeyValue HKEY_CURRENT_USER, "Software\jilu", "1", x, REG_SZ
SetKeyValue HKEY_CURRENT_USER, "Software\jilu", "2", y, REG_SZ
SetKeyValue HKEY_CURRENT_USER, "Software\jilu", "3", d, REG_SZ
End Sub
Private Sub Timer1_Timer()
If QueryValue(HKEY_CURRENT_USER, "Software\jilu", "off") = "0" Then Me.Show
End Sub
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询