Reputation: 164
During some coding and testing, I realised, that my code related to a Form, suddenly is messed up, using a different computer with a different resolution.
To get around the issue, my idea is, to take a common screen resolution size 1920 x 1080 and to start to compare to the actual screen size, where the code is opened. (I am already happy with the Primary screen, do not need all screens).
Now, it is more or less easy to compare the common screen size towards the size used on the computer, where you are running code.
Trying to calculate the resolution and adjust the frame according to it, I am failing to use either round or [int], to get it into a one liner.
When you have a look at the following code:
CLS
Add-Type -assembly System.Windows.Forms
$monitor = [System.Windows.Forms.Screen]::PrimaryScreen
[void]::$monitor.WorkingArea.Width
[void]::$monitor.WorkingArea.Height
$ResolutionStartPointWidth = '1920'
$ResolutionStartPointHeight = '1080'
#Calculating the difference of Width and Height towards actual screen resolution
$ResolutiondifferenceWidth =
($monitor.WorkingArea.Width/$ResolutionStartPointWidth)
$ResolutiondifferenceHeight =
($monitor.WorkingArea.Height/$ResolutionStartPointHeight)
# MainForm
$MainForm = New-Object System.Windows.Forms.Form
$MainForm.Text ='Adjusting Form Size'
$MainForm.Size = '1200,800'
$MainForm.StartPosition = "CenterScreen"
$MainForm.AutoSize = $true
$MainForm.BringToFront()
$MainForm.BackgroundImageLayout = "Stretch"
$Mainform.ShowDialog()
I tried to adjust the ScreenSize in the following line:
$MainForm.Size = '1200,800'
And tried for example:
$MainForm.Size = [int](1200*$ResolutiondifferenceWidth,800)
I also tried other constructs but as more, I tried, as more ridiculous everything got.
Target is, that the calculation of "1200*$ResolutiondifferenceWidth" is calculated and then rounded or put into a number without any decimal and the result is given as the width to $MainForm.Size.
The result also will be helpful, when it comes to "Form" location and also to button size and maybe even more.
Thank you for any suggestions,
Mike
Upvotes: 0
Views: 5079
Reputation: 61013
For one thing, you are defining $ResolutionStartPointWidth
and $ResolutionStartPointHeight
as strings by quoting them.
Also, I believe you don't even need these variables.
Try:
Add-Type -assembly System.Windows.Forms
$monitor = [System.Windows.Forms.Screen]::PrimaryScreen
# Calculating the factors to multiply the Width and Height
$widthFactor = 1200 / $monitor.WorkingArea.Width
$heightFactor = 800 / $monitor.WorkingArea.Height
# MainForm
$MainForm = New-Object System.Windows.Forms.Form
$MainForm.Text ='Adjusting Form Size'
# using System.Drawing.Size will automatically round the width and height to integer numbers like with:
# [Math]::Round(1920 * $widthFactor)
# [Math]::Round(1080 * $heightFactor)
$MainForm.Size = New-Object System.Drawing.Size (1920 * $widthFactor), (1080 * $heightFactor)
$MainForm.StartPosition = "CenterScreen"
$MainForm.AutoSize = $true
$MainForm.BringToFront()
$MainForm.BackgroundImageLayout = "Stretch"
$Mainform.ShowDialog()
# always clean up when done with the form
$MainForm.Dispose()
Upvotes: 3