Here is an updated EnableDisplayDevice:
Code:
; Enables, disables or toggles a display device.
;
; DeviceName: The name of the device, e.g. \\.\DISPLAY1
; Alternatively, it can be the index of the device, which might
; not be the same as the number shown in Display Settings.
; Action: The action to take.
; 0 Disable (false is synonymous with 0)
; 1 Enable (true is synonymous with 1)
; -1 Toggle
; NoReset: If true, settings will be saved to the registry, but not applied.
;
; The following can be used to apply settings saved in the registry:
; DllCall("ChangeDisplaySettings", "uint", 0, "uint", 1)
;
; Return values:
; DISP_CHANGE_SUCCESSFUL 0
; DISP_CHANGE_RESTART 1
; DISP_CHANGE_FAILED -1
; DISP_CHANGE_BADMODE -2
; DISP_CHANGE_NOTUPDATED -3
; DISP_CHANGE_BADFLAGS -4
; DISP_CHANGE_BADPARAM -5
;
EnableDisplayDevice(DeviceName, Action=1, NoReset=false)
{
if (Action = -1) || (DeviceName+0 != "")
{
VarSetCapacity(DisplayDevice, 424), NumPut(424, DisplayDevice, 0)
VarSetCapacity(ThisDeviceName, 32, 0)
Index = 0
Loop {
if !DllCall("EnumDisplayDevices", "UInt", 0, "UInt", A_Index-1, "UInt", &DisplayDevice, "UInt", 0)
return -5
ThisDeviceState := NumGet(DisplayDevice, 164)
Index += 1
DllCall("lstrcpynA", "Str", ThisDeviceName, "UInt", &DisplayDevice+4, "int", 32)
if (DeviceName = Index || DeviceName = ThisDeviceName)
{
if Action = -1
Action := !(ThisDeviceState & 1)
DeviceName := ThisDeviceName
break
}
}
}
VarSetCapacity(devmode, 156, 0), NumPut(156, devmode, 36, "UShort")
if Action
NumPut(0x000020, devmode, 40) ; Enable by setting position = {0,0}
else
NumPut(0x180020, devmode, 40) ; Disable by setting size = {0,0}
err := DllCall("ChangeDisplaySettingsEx", "str", DeviceName, "uint", &devmode, "uint", 0, "uint", 0x10000001, "uint", 0)
if !err && !NoReset
err := DllCall("ChangeDisplaySettings", "uint", 0, "uint", 1)
return err, ErrorLevel:=Action
}
It can be used as follows:
Code:
; disable display 2
EnableDisplayDevice(2, false)
Sleep, 10000
; simultaneously enable display 2 and disable display 1
EnableDisplayDevice(2, true, true)
EnableDisplayDevice(1, false)
Sleep, 10000
; ensure both are enabled
EnableDisplayDevice(2, true, true)
EnableDisplayDevice(1)
(Edit: corrected comment about device numbering and display settings.)
Lexikos' code still works well... under AHK-Basic. Could anybody adopt it to AHK_L? I don't have any knowledge on DllCall, NumPut and VarSetCapacity.
Thanks in advance.