VB程序问题

VB里如何实现获取CPU温度,设置自动关机时间和判断系统是否静音
2025-04-25 01:13:09
推荐回答(3个)
回答1:

自动定时关机
Private Sub Command1_Click()
Timer1.Interval = 1000
End Sub
Private Sub Timer1_Timer()
If Time = FormatDateTime(Text1 & ":" & text2 & ":" & text3) Then Shell "shutdown -s "
End Sub
判断静音
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hwnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
Private Declare Function SendMessage Lib "user32.dll" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Const BM_GETCHECK = &HF0
Private Const BM_SETCHECK = &HF1
Private Sub Command1_Click()
Dim hwnd0 As Long
Shell "sndvol32.exe"
hwnd0 = FindWindow(vbNullString, "主音量")
Dim hwnd1 As Long
hwnd1 = FindWindowEx(hwnd0, 0&, "Button", "全部静音(&M)")
Dim State As Long
State = SendMessage(hwnd1, BM_GETCHECK, ByVal CLng(0), ByVal CLng(0))
If State = 0 Then Print "未静音" Else Print "静音"
End Sub

获取CPU温度
Public Function GetCPUTemp() As Double
Dim i As Integer
Dim mCPU As Variant
Dim u As Variant
Dim s As String
Set mCPU = GetObject("WINMGMTS:{impersonationLevel=impersonate}!root\wmi").ExecQuery("SELECT CurrentTemperature From MSAcpi_ThermalZoneTemperature")
For Each u In mCPU
s = s & u.CurrentTemperature
Next
Set mCPU = Nothing
GetCPUTemp = (s - 2732) / 10

End Function
Private Sub Form_Load()
Timer1.Interval = 500
End Sub
Private Sub Timer1_Timer()
Print GetCPUTemp()
End Sub

回答2:

这个有难度,你查查相关的API函数吧, 自动关机,可以使用shutdonw

回答3:

用WMI实现可能更方便些