I need a script that will toggle my secondary display/monitor, and place it at certain X and Y upon turning on.
On my research I stumbled upon lexikos' old EnableDisplayDevice.ahk and DisplayPosition.ahk scripts, that was brought up recently ( https://autohotkey.com/boards/viewtopic.php?f=5&t=21302 ). Unfortunately, none of following attempts worked out:
Code: Select all
tmp := EnableDisplayDevice(2, false)
MsgBox % tmp ; Returns "-2", which according to comments is "DISP_CHANGE_BADMODE"
Sleep 15000
tmp := EnableDisplayDevice(2, true)
MsgBox % tmp ; Returns "-2", which according to comments is "DISP_CHANGE_BADMODE"
Sleep 15000
tmp := DisplayPosition_Set(2, 1920, 123)
MsgBox % tmp ; Returns "-5", which according to comments is "DISP_CHANGE_BADPARAM". That being said I changed the "2" to "\\.\DISPLAY2" (yes, quoted), and as result the script would just crash without anything showing up.
Code: Select all
; EnumDisplayDevices(Index [, ByRef Name, ByRef StateFlags ] )
;
; Index: One-based index of device to get info for.
; DeviceName: [out] The name of the device.
; StateFlags: [out] Any reasonable combination of the following flags:
; 0x00000001 DISPLAY_DEVICE_ATTACHED_TO_DESKTOP
; 0x00000004 DISPLAY_DEVICE_PRIMARY_DEVICE
; 0x00000008 DISPLAY_DEVICE_MIRRORING_DRIVER
; DeviceKey: [out] Path to the device's registry key relative to HKEY_LOCAL_MACHINE.
;
; Returns true if the display device exists, otherwise false.
;
/* Example 1 (requires EnableDisplayDevice()):
SecondaryDevice =
count = 0
Loop {
if ! EnumDisplayDevices(A_Index, DeviceName, StateFlags)
break
if !(StateFlags & 8) ; not a pseudo-device
if (++count = 2) ; second device
break
}
if DeviceName
EnableDisplayDevice(DeviceName, -1) ; toggle
*/
/* Example 2:
Loop {
if ! EnumDisplayDevices(A_Index, DeviceName, StateFlags)
break
if (StateFlags & 4)
text .= DeviceName " is the primary display device.`n"
else if (StateFlags & 1)
text .= "The desktop extends onto " DeviceName ".`n"
if (StateFlags & 8)
text .= DeviceName " is a pseudo-device.`n"
}
MsgBox %text%
*/
EnumDisplayDevices(Index, ByRef DeviceName, ByRef StateFlags="", ByRef DeviceKey="")
{
; DISPLAY_DEVICE DisplayDevice
VarSetCapacity(DisplayDevice, 424)
; lpDisplayDevice.cb := sizeof(DISPLAY_DEVICE)
NumPut(424, DisplayDevice, 0)
VarSetCapacity(DeviceName, 32, 0)
VarSetCapacity(DeviceKey, 128, 0)
; For consistency, clear StateFlags in case of failure.
StateFlags = 0
if ! DllCall("EnumDisplayDevices"
, "UInt", 0
, "UInt", Index-1
, "UInt", &DisplayDevice
, "UInt", 0)
return false
StateFlags := NumGet(DisplayDevice, 164)
DllCall("lstrcpynA", "Str", DeviceName, "UInt", &DisplayDevice+4, "int", 32)
DllCall("lstrcpynA", "Str", DeviceKey, "UInt", &DisplayDevice+296, "int", 128)
if (SubStr(DeviceKey,1,18)="\Registry\Machine")
DeviceKey := SubStr(DeviceKey,19)
return true
}
I am Win7 x64 and Nvidia graphic card user, if either of these matters. AHK runs in U32.