我想檢測監視器狀態。
為此,我注冊了WM_POWERBROADCAST訊息。
在lParam此訊息的包含PBT_POWERSETTINGCHANGE。
typedef struct {
GUID PowerSetting;
DWORD DataLength;
UCHAR Data[1];
} POWERBROADCAST_SETTING, *PPOWERBROADCAST_SETTING;
GUID 在VB6中是這樣定義的:
Private Type GUID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(0 To 7) As Byte
End Type
如何
DWORD DataLength;
UCHAR Data[1];
翻譯成VB6?
uj5u.com熱心網友回復:
該結構的UCHAR Data[1]成員POWERBROADCAST_SETTING表示依賴于PowerSettingandDataLength成員的位元組陣列。根據檔案,Data成員可以是 GUID 或 DWORD。所以VB6中最簡單的方法是為固定成員宣告一個結構,然后根據成員在第二步中獲取剩余資料PowerSetting。
Public Type Guid
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(0 To 7) As Byte
End Type
Private Type PowerBroadcastSetting
PowerSetting As Guid
DataLength As Long
End Type
視窗程序應如下所示:
Public Function WindowProc(ByVal hWnd As Long, ByVal iMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Dim g As Guid
Dim L As Long
Dim pbs As PowerBroadcastSetting
Select Case iMsg
Case WM_POWERBROADCAST
Select Case wParam
Case PBT_APMPOWERSTATUSCHANGE
DebugPrint "PBT_APMPOWERSTATUSCHANGE"
Case PBT_APMRESUMEAUTOMATIC
DebugPrint "PBT_APMRESUMEAUTOMATIC"
Case PBT_APMRESUMESUSPEND
DebugPrint "PBT_APMRESUMESUSPEND"
Case PBT_APMSUSPEND
DebugPrint "PBT_APMSUSPEND"
Case PBT_POWERSETTINGCHANGE
CopyMemory pbs, ByVal lParam, Len(pbs)
DebugPrint "PBT_POWERSETTINGCHANGE " & GuidToString(pbs.PowerSetting)
Select Case GuidToString(pbs.PowerSetting)
Case GUID_POWERSCHEME_PERSONALITY
CopyMemory g, ByVal lParam Len(pbs), 16
DebugPrint "New power scheme: " & GuidToString(g)
Case GUID_SESSION_DISPLAY_STATUS
CopyMemory L, ByVal lParam Len(pbs), 4
DebugPrint "Display status: " & L
Case GUID_MONITOR_POWER_ON
CopyMemory L, ByVal lParam Len(pbs), 4
DebugPrint "Primary Monitor state: " & L
Case GUID_CONSOLE_DISPLAY_STATE
CopyMemory L, ByVal lParam Len(pbs), 4
DebugPrint "Console Display state: " & L
End Select
End Select
'An application should return TRUE if it processes this message.
WindowProc = 1
Exit Function
End Select
'Pass message to original window proc
WindowProc = CallWindowProc(ProcOld, hWnd, iMsg, wParam, lParam)
End Function
使用以下 API 宣告:
Public Const GWL_WNDPROC As Long = (-4)
Private Const WM_POWERBROADCAST As Long = 536
Public Type Guid
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(0 To 7) As Byte
End Type
Private Type PowerBroadcastSetting
PowerSetting As Guid
DataLength As Long
End Type
'Power status has changed.
Private Const PBT_APMPOWERSTATUSCHANGE = 10
'Operation is resuming automatically from a low-power state. This message is sent every time the system resumes.
Private Const PBT_APMRESUMEAUTOMATIC As Long = 18
'Operation is resuming from a low-power state. This message is sent after PBT_APMRESUMEAUTOMATIC if the resume is triggered by user input, such as pressing a key.
Private Const PBT_APMRESUMESUSPEND As Long = 7
'System is suspending operation.
Private Const PBT_APMSUSPEND As Long = 4
'A power setting change event has been received.
Private Const PBT_POWERSETTINGCHANGE As Long = 32787
'Power Setting GUIDs
'The active power scheme personality has changed. All power schemes map to one of these personalities.
'The Data member is a GUID that indicates the new active power scheme personality.
Public Const GUID_POWERSCHEME_PERSONALITY As String = "{245D8541-3943-4422-B025-13A784F679B7}"
'The display associated with the application's session has been powered on or off.
'The Data member is a DWORD with one of the following values.
'0x0 - The display is off.
'0x1 - The display is on.
'0x2 - The display is dimmed.
Public Const GUID_SESSION_DISPLAY_STATUS As String = "{2B84C20E-AD23-4DDF-93DB-05FFBD7EFCA5}"
Public Const GUID_MONITOR_POWER_ON As String = "{02731015-4510-4526-99E6-E5A17EBD1AEA}"
' Windows 8
Public Const GUID_CONSOLE_DISPLAY_STATE As String = "{6FE69556-704A-47A0-8F24-C28D936FDA47}"
'Notifications are sent using WM_POWERBROADCAST messages with a wParam parameter of PBT_POWERSETTINGCHANGE.
Public Const DEVICE_NOTIFY_WINDOW_HANDLE As Long = 0
Private Declare Sub CopyMemory Lib "kernel32.dll" Alias "RtlMoveMemory" (lpDest As Any, lpSource As Any, ByVal cbCopy As Long)
Public Declare Function RegisterPowerSettingNotification Lib "user32.dll" (ByVal hRecipient As Long, PowerSettingGuid As Guid, ByVal Flags As Long) As Long
Public Declare Function UnregisterPowerSettingNotification Lib "user32.dll" (ByVal Handle As Long) As Long
Private Declare Function StringFromGUID2 Lib "ole32.dll" (rguid As Guid, ByVal lpsz As Long, ByVal cchMax As Long) As Long
Private Declare Function CLSIDFromString Lib "ole32.dll" (ByVal lpsz As Long, pclsid As Guid) As Long
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Private Declare Sub OutputDebugString Lib "kernel32" Alias "OutputDebugStringA" (ByVal lpOutputString As String)
和輔助函式:
Public Function GuidToString(g As Guid) As String
Dim L As Long
Dim b(0 To 77) As Byte
'we have space for 38 unicode chars (guid incl. brackets) terminating zero (78 bytes)
L = StringFromGUID2(g, VarPtr(b(0)), 39)
'strip terminating 0, convert to string
GuidToString = Left(b, L - 1)
End Function
Public Function GuidFromString(ByVal gs As String) As Guid
CLSIDFromString StrPtr(gs), GuidFromString
End Function
Public Sub DebugPrint(ByVal s As String)
OutputDebugString s & vbCrLf
End Sub
VB6中的測驗表格:
Option Explicit
Private isSubclassed As Boolean
Private hScheme As Long
Private hDisplay As Long
Private hMonitor As Long
Private hConsole As Long
Private Sub cmdRegister_Click()
Unregister
Register
End Sub
Private Sub cmdUnregister_Click()
Unregister
End Sub
Private Sub Register()
ProcOld = SetWindowLong(hWnd, GWL_WNDPROC, AddressOf WindowProc)
isSubclassed = True
MsgBox "Subclassed"
'Register Power Events
hScheme = RegisterPowerSettingNotification(hWnd, GuidFromString(GUID_POWERSCHEME_PERSONALITY), DEVICE_NOTIFY_WINDOW_HANDLE)
hDisplay = RegisterPowerSettingNotification(hWnd, GuidFromString(GUID_SESSION_DISPLAY_STATUS), DEVICE_NOTIFY_WINDOW_HANDLE)
hConsole = RegisterPowerSettingNotification(hWnd, GuidFromString(GUID_CONSOLE_DISPLAY_STATE), DEVICE_NOTIFY_WINDOW_HANDLE)
MsgBox "Registered " & hScheme & " " & hDisplay & " " & hMonitor & " " & hConsole
End Sub
Private Sub Unregister()
'Unregister Power Events
If hScheme Then
UnregisterPowerSettingNotification hScheme
hScheme = 0
End If
If hDisplay Then
UnregisterPowerSettingNotification hDisplay
hDisplay = 0
End If
If hMonitor Then
UnregisterPowerSettingNotification hMonitor
hMonitor = 0
End If
If hConsole Then
UnregisterPowerSettingNotification hConsole
hConsole = 0
End If
'Unsubclass
If isSubclassed Then
SetWindowLong hWnd, GWL_WNDPROC, ProcOld
isSubclassed = False
MsgBox "Unsubclassed"
End If
End Sub
Private Sub Form_Unload(Cancel As Integer)
Unregister
End Sub
編輯:添加GUID_CONSOLE_DISPLAY_STATE。以下是在 Windows 10 上使用DebugView捕獲的輸出:
顯示幕在不活動后被 Windows 的電源管理置于待機狀態:
[7752] PBT_POWERSETTINGCHANGE {6FE69556-704A-47A0-8F24-C28D936FDA47}
[7752] 控制臺顯示狀態:2
[7752] PBT_POWERSETTINGCHANGE {2B84C20E-AD23-4DDF50FF-97
顯示狀態:2
15 秒后:
[7752] PBT_POWERSETTINGCHANGE {6FE69556-704A-47A0-8F24-C28D936FDA47}
[7752] Console Display state: 0
[7752] PBT_POWERSETTINGCHANGE {02731015-4510-4526-99E6-E5A17EBD1AEA}
[7752] Primary Monitor state: 0
[7752] PBT_POWERSETTINGCHANGE {2B84C20E-AD23-4DDF-93DB-05FFBD7EFCA5}
[7752] Display status: 0
WakeUp:
[7752] PBT_POWERSETTINGCHANGE {6FE69556-704A-47A0-8F24-C28D936FDA47}
[7752] Console Display state: 1
[7752] PBT_POWERSETTINGCHANGE {02731015-4510-4526-99E6-E5A17EBD1AEA}
[7752] Primary Monitor state: 1
[7752] PBT_POWERSETTINGCHANGE {2B84C20E-AD23-4DDF-93DB-05FFBD7EFCA5}
[7752] Display status: 1
If you switch off the displays manually, there will be no notifications, at least with my hardware. Not sure, if on other systems the events will be raised.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/314741.html
