2016-03-04 47 views
0

我正在尋找一種方法來獲得雙顯示器的計算機上的總分辨率。我已經找到了通過this stackoverflow page(這是我的問題)做到這一點的方法,但我在下面提出的解決方案超級笨重。找到雙屏幕分辨率

$screen = [System.Windows.Forms.Screen]::AllScreens | select -ExpandProperty bounds 
foreach ($item in $screen) 
{ 
    $item = $item -replace ".*Y=0,","" -replace "{", "" -replace "}", "" -replace "Width=", "" -replace "Height=", "" -replace ",", ";" 
    $pos = $item.IndexOf(";") 
    $leftPart = $item.Substring(0, $pos) 
    $rightPart = $item.Substring($pos + 1) 
    [int]$SCREENWIDTH = $SCREENWIDTH + $leftPart 
    [int]$SCREENHEIGHT = $rightPart 
} 
$richtextbox1.Text = ([string]$SCREENWIDTH + " " + [string]$SCREENHEIGHT) 

輸出從:

$screen = [System.Windows.Forms.Screen]::AllScreens | select -ExpandProperty bounds 

{X=1920,Y=0,Width=1920,Height=1200} {X=0,Y=0,Width=1920,Height=1200} 

所需的輸出是具有總寬度和高度存儲到變量。

我正在使用powershell studio。

回答

0

只要確保你初始化$屏幕寬度

$screen = [System.Windows.Forms.Screen]::AllScreens | select -ExpandProperty bounds 
$SCREENWIDTH = 0 
foreach ($item in $screen) 
{ 
    $item = $item -replace ".*Y=0,","" -replace "{", "" -replace "}", "" -replace "Width=", "" -replace "Height=", "" -replace ",", ";" 
    $pos = $item.IndexOf(";") 
    $leftPart = $item.Substring(0, $pos) 
    $rightPart = $item.Substring($pos + 1) 

    [int]$SCREENWIDTH = $SCREENWIDTH + $leftPart 
    [int]$SCREENHEIGHT = $rightPart 
} 

([string]$SCREENWIDTH + " " + [string]$SCREENHEIGHT)