“Extend my Windows desktop onto this monitor” programmatically

后端 未结 8 1080
不思量自难忘°
不思量自难忘° 2020-12-24 03:02

I would like to be able to set \"Extend my Windows desktop onto this monitor\" via code. A PowerShell script would be ideal. WMI seems the way forward but I have zero knowle

8条回答
  •  北海茫月
    2020-12-24 03:38

    This sort of operation is not directly accessible from PowerShell in the sense that there is not a .NET interface to these settings. A lot of core OS stuff is unmanaged code which can only be manipulated via win32 API calls. While you may be on to something with WMI, I searched for a while and wasn't able to find a satisfactory WMI class which is able to manipulate this setting.

    The next step would be to modify the registry directly. It looks like the setting lies under HKLM:\system\CurrentControlSet\control\video--somewhere. I believe it's the one called "Attach.ToDesktop".

    This is a partial solution, so I'm marking as community wiki answer.

    I'm not certain this is the right registry key, and I don't have a system on which I can test multi-monitor at the moment. The purpose of this is to determine which is the primary controller, and then it outputs the value of the Attach.ToDesktop key.

    param ( 
        $ControllerName = "$( throw 'ControllerName is a mandatory parameter' )"
    )
    $regPath = "HKLM:\system\CurrentControlSet\control\video"
    $devDescStr = "Device Description"
    
    Set-Location -path $regPath
    $regSubKey = Get-ChildItem -recurse -include 0000
    $devDescProperty = $regSubKey | Get-ItemProperty -name $devDescStr -erroraction SilentlyContinue 
    $priDescProperty = $devDescProperty | Where-Object { $_.$devDescStr -match $ControllerName }
    Set-Location -path $priDescProperty.PSPath
    Get-ItemProperty -path . -name "Attach.ToDesktop"
    

提交回复
热议问题