Inconsistent screen.bounds

Welcome Forums General PowerShell Q&A Inconsistent screen.bounds

This topic contains 1 reply, has 1 voice, and was last updated by

 
Participant
7 months, 4 weeks ago.

  • Author
    Posts
  • #96392

    Participant
    Points: 13
    Rank: Member

    Using the below code provided by Will on here when I run it from the ISE I get one set of valus for the screen.bounds but when running from the console or via scheduled task I get a different set of values. The one I get from the ISE are correct and the form fills the second monitor.. Any ideas why I get different values ?

    #region screenblocker
    Add-Type -AssemblyName System.Windows.Forms
    $screens = [System.Windows.Forms.Screen]::Allscreens | Where-Object Primary -eq $false
    $synchash.screens = New-Object System.Collections.Generic.List[System.Object]
    foreach($screen in $screens){
    $synchash.screens.Add($screen.devicename.replace('\',").replace('.',")[-1])
    }
    foreach ($screen in $screens){
    $synchash."$($screen.devicename.replace('\',").replace('.',"))" = $screen
    $newRunspace =[runspacefactory]::CreateRunspace()
    $newRunspace.ApartmentState = "STA"
    $newRunspace.ThreadOptions = "ReuseThread"
    $newRunspace.Open()
    $newRunspace.SessionStateProxy.SetVariable("syncHash",$syncHash)
    $PowerShell = [PowerShell]::Create().AddScript({
    function LoadXaml ($filename){
    $XamlLoader=(New-Object System.Xml.XmlDocument)
    $XamlLoader.Load($filename)
    return $XamlLoader
    }
    $screenNum = $synchash.screens[0]
    $display = "Display" + $screenNum
    $synchash.screens.RemoveAt(0)

    $XamlMainWindow = LoadXaml("\blankScreen.xaml")
    $reader = (New-Object System.Xml.XmlNodeReader $XamlMainWindow)
    $syncHash."Window$screenNum" = [Windows.Markup.XamlReader]::Load($reader)

    [xml]$XAML = $XamlMainWindow
    $XamlMainWindow.SelectNodes("//*[@*[contains(translate(name(.),'n','N'),'Name')]]") | ForEach-Object{
    #Find all of the form types and add them as members to the synchash
    $syncHash.Add($("" + $_.Name + $screenNum),$syncHash."Window$screenNum".FindName($_.Name) )
    }

    $syncHash."Window$screenNum".Top = $synchash.$display.bounds.Top;
    $syncHash."Window$screenNum".Left = $synchash.$display.bounds.Left;
    $syncHash."Window$screenNum".Width = $synchash.$display.bounds.Width;
    $syncHash."Window$screenNum".Height = $synchash.$display.bounds.Height;
    $synchash."Window$screenNum".Show()

    $synchash."error$screenNum" = $error
    })
    $PowerShell.Runspace = $newRunspace
    [void]$Jobs.Add((
    [pscustomobject]@{
    PowerShell = $PowerShell
    Runspace = $PowerShell.BeginInvoke()
    }
    ))
    }
    #endregion screenblocker

  • #96407

    Participant
    Points: 13
    Rank: Member

    A further bit of investigating

    If I run [System.Windows.Forms.Screen]::Allscreens in the ISE it returns details on both my primary and secondary monitors, however if I run it in the console it only returns details of my primary monitor. How can I get details of the secondary monitor in the console ?

The topic ‘Inconsistent screen.bounds’ is closed to new replies.