Jump to content

Disabling a 3rd monitor while running a function and then powering back on


Go to solution Solved by 20Ice18,

Recommended Posts

Hi everyone, I'm relatively new to AutoIt and I am working on a feature for a code that was written a while ago.

The code basically creates a Rdp file to a Linux server (using xrdp). I added an option to use multimon. 

It works great with 2 screens but when I use 3 screens there is a problem.

The code creates flags for the .Rdp file. I tried using the appropriate flag - selectedmonitors:s:0,1

And for some reason it stretches over the screen *only* when attempting to connect to a Linux server. It works fine with a windows machine.

I disconnected the third screen to test if it would work, and then plugged it back in and I saw it works fine.

My idea was to disable the third screen while opening the file (also done in my script) and then powering it back on after 10-20 seconds.

I couldn't find any way to do this.

I'd love to have some help or references!

Thanks in advance!

Link to comment
Share on other sites

  • 5 weeks later...
  • Solution

To disable or enable a monitor  you can use the DllCall the function I suppose, to call Windows API functions. Here's an example of how you can disable and enable a monitor using the EnumDisplayMonitors and ChangeDisplaySettingsEx functions:

 

#include <WinAPI.au3>
#include <Constants.au3>

Global Const $DISPLAY_DEVICE_ATTACHED_TO_DESKTOP = 0x1
Global Const $DMDO_DEFAULT = 0x0
Global Const $DMDO_90 = 0x1
Global Const $DMDO_180 = 0x2
Global Const $DMDO_270 = 0x3
Global Const $ENUM_CURRENT_SETTINGS = -1

Func DisableMonitor($iIndex)
    Local $hMonitor = _WinAPI_EnumDisplayMonitors(0, 0)
    Local $iCount = @extended
    If $iIndex < 0 Or $iIndex >= $iCount Then Return False

    Local $aMonitorInfo = _WinAPI_GetMonitorInfo($hMonitor[$iIndex])
    Local $hDeviceName = $aMonitorInfo[3]

    Local $tDevMode = DllStructCreate($tagDEVMODE)
    DllCall("user32.dll", "int", "EnumDisplaySettings", "ptr", 0, "int", $ENUM_CURRENT_SETTINGS, "ptr", DllStructGetPtr($tDevMode))

    DllStructSetData($tDevMode, "PelsWidth", 1)
    DllStructSetData($tDevMode, "PelsHeight", 1)
    DllStructSetData($tDevMode, "DisplayFlags", BitOR(DllStructGetData($tDevMode, "DisplayFlags"), $DM_INTERLACED))
    DllStructSetData($tDevMode, "Fields", BitOR(DllStructGetData($tDevMode, "Fields"), $DM_DISPLAYFLAGS))

    Local $aResult = DllCall("user32.dll", "int", "ChangeDisplaySettingsEx", "str", $hDeviceName, "ptr", DllStructGetPtr($tDevMode), "hwnd", 0, "int", $CDS_UPDATEREGISTRY, "ptr", 0)

    Return $aResult[0] = $DISP_CHANGE_SUCCESSFUL
EndFunc

Func EnableMonitor($iIndex)
    Local $hMonitor = _WinAPI_EnumDisplayMonitors(0, 0)
    Local $iCount = @extended
    If $iIndex < 0 Or $iIndex >= $iCount Then Return False

    Local $aMonitorInfo = _WinAPI_GetMonitorInfo($hMonitor[$iIndex])
    Local $hDeviceName = $aMonitorInfo[3]

    Local $aResult = DllCall("user32.dll", "int", "ChangeDisplaySettingsEx", "str", $hDeviceName, "ptr", 0, "hwnd", 0, "int", $CDS_UPDATEREGISTRY, "ptr", 0)

    Return $aResult[0] = $DISP_CHANGE_SUCCESSFUL
EndFunc

; Example usage:
DisableMonitor(2) ; Disable the third monitor
Sleep(10000) ; Wait for 10 seconds
EnableMonitor(2) ; Enable the third monitor

You can adjust the index passed to the DisableMonitor and EnableMonitor functions to disable or enable a specific monitor. Make sure to call DisableMonitor before opening the .rdp file and EnableMonitor after a delay to re-enable the monitor.

❤️

Link to comment
Share on other sites

11 hours ago, 20Ice18 said:

To disable or enable a monitor  you can use the DllCall the function I suppose, to call Windows API functions. Here's an example of how you can disable and enable a monitor using the EnumDisplayMonitors and ChangeDisplaySettingsEx functions:

 

#include <WinAPI.au3>
#include <Constants.au3>

Global Const $DISPLAY_DEVICE_ATTACHED_TO_DESKTOP = 0x1
Global Const $DMDO_DEFAULT = 0x0
Global Const $DMDO_90 = 0x1
Global Const $DMDO_180 = 0x2
Global Const $DMDO_270 = 0x3
Global Const $ENUM_CURRENT_SETTINGS = -1

Func DisableMonitor($iIndex)
    Local $hMonitor = _WinAPI_EnumDisplayMonitors(0, 0)
    Local $iCount = @extended
    If $iIndex < 0 Or $iIndex >= $iCount Then Return False

    Local $aMonitorInfo = _WinAPI_GetMonitorInfo($hMonitor[$iIndex])
    Local $hDeviceName = $aMonitorInfo[3]

    Local $tDevMode = DllStructCreate($tagDEVMODE)
    DllCall("user32.dll", "int", "EnumDisplaySettings", "ptr", 0, "int", $ENUM_CURRENT_SETTINGS, "ptr", DllStructGetPtr($tDevMode))

    DllStructSetData($tDevMode, "PelsWidth", 1)
    DllStructSetData($tDevMode, "PelsHeight", 1)
    DllStructSetData($tDevMode, "DisplayFlags", BitOR(DllStructGetData($tDevMode, "DisplayFlags"), $DM_INTERLACED))
    DllStructSetData($tDevMode, "Fields", BitOR(DllStructGetData($tDevMode, "Fields"), $DM_DISPLAYFLAGS))

    Local $aResult = DllCall("user32.dll", "int", "ChangeDisplaySettingsEx", "str", $hDeviceName, "ptr", DllStructGetPtr($tDevMode), "hwnd", 0, "int", $CDS_UPDATEREGISTRY, "ptr", 0)

    Return $aResult[0] = $DISP_CHANGE_SUCCESSFUL
EndFunc

Func EnableMonitor($iIndex)
    Local $hMonitor = _WinAPI_EnumDisplayMonitors(0, 0)
    Local $iCount = @extended
    If $iIndex < 0 Or $iIndex >= $iCount Then Return False

    Local $aMonitorInfo = _WinAPI_GetMonitorInfo($hMonitor[$iIndex])
    Local $hDeviceName = $aMonitorInfo[3]

    Local $aResult = DllCall("user32.dll", "int", "ChangeDisplaySettingsEx", "str", $hDeviceName, "ptr", 0, "hwnd", 0, "int", $CDS_UPDATEREGISTRY, "ptr", 0)

    Return $aResult[0] = $DISP_CHANGE_SUCCESSFUL
EndFunc

; Example usage:
DisableMonitor(2) ; Disable the third monitor
Sleep(10000) ; Wait for 10 seconds
EnableMonitor(2) ; Enable the third monitor

You can adjust the index passed to the DisableMonitor and EnableMonitor functions to disable or enable a specific monitor. Make sure to call DisableMonitor before opening the .rdp file and EnableMonitor after a delay to re-enable the monitor.

Interesting.. I will try this, thanks!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...