Oh well, I guess it just doesnt like my display driver.
Return value is always 0. It can be turned on normally by windows. Although enumdisplaydevices cannot pick it up unless it is on. So I'm guessing the video driver does disable it for this
I can turn it off, but I just can't turn it on.
EDIT:::: Ive finally cracked it, I noticed this quote on MSDN
Quote:
When adding a display monitor to a multiple-monitor system programmatically, set DEVMODE.dmFields to DM_POSITION and specify a position (in DEVMODE.dmPosition) for the monitor you are adding that is adjacent to at least one pixel of the display area of an existing monitor. To detach the monitor, set DEVMODE.dmFields to DM_POSITION but set DEVMODE.dmPelsWidth and DEVMODE.dmPelsHeight to zero. For more information, see Multiple Display Monitors.
Then I realised your code had the comment
Quote:
; Enable by setting position = {0,0}
So I added some extra info to the structure positioning it to the right of my monitor.
Specifically the lines,
Code:
VarSetCapacity(Point,8,0)
Numput(A_ScreenWidth + 1,Point)
Numput(&point,devmode,44)
just after the line with the above comment.
This is now working spot on for me and I can enable/disable my second monitor on the fly.
The full very slightly adjust code, is as follows.
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}
VarSetCapacity(Point,8,0)
Numput(A_ScreenWidth + 1,Point)
Numput(&point,devmode,44)
}
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
}
I'm sure it could be enhanced more to allow better positioning.