比较难的问题来了,如何监听显示器关闭或打开这一状态,以前的帖子看了没答案
例如我工作关系,关闭显示器时就把电脑设置为静音,想用AUTOIT写个小程序,当检测到显示器关闭时就设置静音,当检测到显示器打开了,就恢复声音以前的帖子都翻过,没有找到答案。
现在不知道如何做到监听显示器关闭和打开,不知道有没有大侠能实现,不吝指教,谢谢 显示器和主机是分离的,之间只不过用数据线有连接。
显示器开或关对你的主机是完全没影响的。
显示器开或关对你的系统也是完全没影响的。
就国内现在最牛的内控软件,都无法监控到显示器是开还是关,所以我个人认为你这个要求是没办法达到的。 这个有点难度,关键是获取不到显示器的状态。 论坛里倒是有一个用AU3操作显示器关闭的贴子,您可以反过来,用AU3控制显示器关闭,并且静音。 你觉得鼠标键盘无操作2分钟后自动关闭声音这个办法怎么样? 真的好难,帮你顶 本帖最后由 虫子樱桃 于 2015-12-22 09:45 编辑
参考链接如下,但是不懂C++,只有让P老大、风行者、afan等高人看看啦
http://cboard.cprogramming.com/windows-programming/103275-monitor-state-detection.html#post755545
对于显示器休眠,自动关闭显示器,可以参考官网帖子 https://www.autoitscript.com/forum/topic/165765-notification-when-monitor-is-goinig-to-sleep/
显示器直接控制的UDF https://www.autoitscript.com/forum/topic/154350-monitor-configuration-udf/
嗯,就是这样,我是可爱的搬运工
经过搜索,找到个UDF,望对你有所帮助
; #FUNCTION# ====================================================================================================================
; Name ..........: _DisplayState
; Description ...: Checks if a display is on or off.
; Syntax ........: _DisplayState()
; Parameters ....: None
; Return values..: Success - 1 = screen is on
; 0 = screen is off
; Failure - -1 and sets the @error flag to non-zero.
; Author ........: syk3s
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........: @@MsdnLink@@ RegisterPowerSettingNotification
; Example .......: No
; ===============================================================================================================================
Func _DisplayState($hWndGUI = 0, $MsgID = 0, $WParam = 0, $LParam = 0)
Local Const $_GUID_MONITOR_POWER_ON = '{02731015-4510-4526-99E6-E5A17EBD1AEA}'
Local Const $_tagGUID = "ulong Data1;ushort Data2;ushort Data3;byte Data4"
If IsDeclared($_iDisplayState) = 0 Then Global $_iDisplayState = -1
If $hWndGUI = 0 And $MsgID = 0 Then
Global $_iDisplayState = -1
Local Const $_WM_POWERBROADCAST = 0x0218
Local $hTemp, $tGUID, $Ret
Local $hTemp = GUICreate("Display state")
If IsHWnd($hTemp) = 0 Then
Return SetError(1, 0, -1)
EndIf
If GUIRegisterMsg($_WM_POWERBROADCAST, "_DisplayState") = 0 Then
Return SetError(2, 0, -1)
EndIf
;register for a display state broadcast
$tGUID = DllStructCreate($_tagGUID)
If @error Then
Return SetError(3, 0, -1)
EndIf
$Ret = DllCall('ole32.dll', 'uint', 'CLSIDFromString', 'wstr', $_GUID_MONITOR_POWER_ON, 'ptr', DllStructGetPtr($tGUID))
If (@error) Or ($Ret) Then
Return SetError(4, 0, -1)
EndIf
$Ret = DllCall('user32.dll', 'ptr', 'RegisterPowerSettingNotification', 'hwnd', $hTemp, 'ptr', DllStructGetPtr($tGUID), 'dword', 0) ;register for the event
If (@error) Or (Not $Ret) Then
Return SetError(5, 0, -1)
EndIf
DllCall('user32.dll', 'int', 'UnregisterPowerSettingNotification', 'ptr', $Ret) ;unregister
GUIDelete($hTemp)
If $_iDisplayState <> 0 And $_iDisplayState <> 1 Then
Return SetError(6, 0, -1)
Else
Return $_iDisplayState
EndIf
Else
Local $stPOWERBROADCAST_SETTING = DllStructCreate($_tagGUID & ";DWORD;byte", $LParam)
Local $aResult = DllCall("ole32.dll", "int", "StringFromGUID2", "struct*", DllStructGetPtr($stPOWERBROADCAST_SETTING), "wstr", "", "int", 40) ;get string GUID back
If $aResult = $_GUID_MONITOR_POWER_ON Then $_iDisplayState = DllStructGetData($stPOWERBROADCAST_SETTING, 6, 1)
EndIf
EndFunc ;==>_DisplayState
回复 7# 虫子樱桃
菜鸟问问:这个函数怎么用?我执行报错啊!需要include哪些文件? 回复 8# ysping
直接编译,然后命令行调用 我把这段代码复制到单独的au3文件中,运行脚本没反应:face (13): 回复 9# 虫子樱桃
编译成exe后运行没任何返回。
弱弱的问一下:为什么不需要添加#include <***.au3>呢? 修正下 7 # UDF 在 AU3新版中的问题#include <AutoItConstants.au3>
#include <MsgBoxConstants.au3>
; #FUNCTION# ====================================================================================================================
; Name ..........: _DisplayState 函数名:... _DisplayState
; Description ...: Checks if a display is on or off. 描 述:...检查显示器是否是开或关。
; Syntax ........: _DisplayState() 语 法:..._DisplayState()
; Parameters ....: None 参 数:...无
; Return values..: Success - 1 = screen is on 返回1 表示屏幕开
; 0 = screen is off 返回0 表示屏幕关
; Failure - -1 and sets the @error flag to non-zero.失败 返回 -1,并置 @error 为非 0
; Author ........: syk3s
; Modified ......:
; Remarks .......:
; Related .......:
; Link ..........: @@MsdnLink@@ RegisterPowerSettingNotification
; Example .......: No
; ===============================================================================================================================
MsgBox(0,0,_DisplayState())
Func _DisplayState($hWndGUI = 0, $MsgID = 0, $WParam = 0, $LParam = 0)
Local Const $_GUID_MONITOR_POWER_ON = '{02731015-4510-4526-99E6-E5A17EBD1AEA}'
Local Const $_tagGUID = "ulong Data1;ushort Data2;ushort Data3;byte Data4"
If IsDeclared("_iDisplayState") = 0 Then Global $_iDisplayState = -1
If $hWndGUI = 0 And $MsgID = 0 Then
Global $_iDisplayState = -1
Local Const $_WM_POWERBROADCAST = 0x0218
Local $hTemp, $tGUID, $Ret
Local $hTemp = GUICreate("Display state")
If IsHWnd($hTemp) = 0 Then
Return SetError(1, 0, -1)
EndIf
If GUIRegisterMsg($_WM_POWERBROADCAST, "_DisplayState") = 0 Then
Return SetError(2, 0, -1)
EndIf
;register for a display state broadcast
$tGUID = DllStructCreate($_tagGUID)
If @error Then
Return SetError(3, 0, -1)
EndIf
$Ret = DllCall('ole32.dll', 'uint', 'CLSIDFromString', 'wstr', $_GUID_MONITOR_POWER_ON, 'ptr', DllStructGetPtr($tGUID))
If (@error) Or ($Ret) Then
Return SetError(4, 0, -1)
EndIf
$Ret = DllCall('user32.dll', 'ptr', 'RegisterPowerSettingNotification', 'hwnd', $hTemp, 'ptr', DllStructGetPtr($tGUID), 'dword', 0) ;register for the event
If (@error) Or (Not $Ret) Then
Return SetError(5, 0, -1)
EndIf
DllCall('user32.dll', 'int', 'UnregisterPowerSettingNotification', 'ptr', $Ret) ;unregister
GUIDelete($hTemp)
If $_iDisplayState <> 0 And $_iDisplayState <> 1 Then
Return SetError(6, 0, -1)
Else
Return $_iDisplayState
EndIf
Else
Local $stPOWERBROADCAST_SETTING = DllStructCreate($_tagGUID & ";DWORD;byte", $LParam)
Local $aResult = DllCall("ole32.dll", "int", "StringFromGUID2", "struct*", DllStructGetPtr($stPOWERBROADCAST_SETTING), "wstr", "", "int", 40) ;get string GUID back
If $aResult = $_GUID_MONITOR_POWER_ON Then $_iDisplayState = DllStructGetData($stPOWERBROADCAST_SETTING, 6, 1)
EndIf
EndFunc ;==>_DisplayState 回复 12# zghwelcome
测试了三台win7,一台32bit,两台64bit,只有一台64bit返回的结果是正确的,其他都是-1。 捕获的是笔记本盖子的打开合上状态消息笔记本外接显示设备或台式机就不要折腾了
页:
[1]