Dynamic form location based on monitor size?

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 2 years and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
austindodig
Posts: 3
Last visit: Mon Oct 16, 2023 12:04 pm
Has voted: 1 time

Dynamic form location based on monitor size?

Post by austindodig »

Hello,

Currently I'm working on a project in PowerShell Studio 2021 where the form must open in the upper right corner of the screen. This project will be deployed to many users with different monitor sizes so I don't want to statically code the coordinates for the $form1.location. My work around is to detect the primary monitor working location and multiply that by the factor in the $form1.Location variable. Unfortunately, the location in the properties of the form within PS Studio will not accept anything other than integers and that is part of the region generated form code which, to my knowledge, I cannot edit within the .psf file so I must export to a ps1 file and make the changes there.

My question is two part:

A) Is there a way that I can code this directly into PS Studios PSF file without having to export first?
B) Is there a better way to accomplish this?

I really appreciate any insight.

  1. $monitor = [System.Windows.Forms.Screen]::PrimaryScreen
  2.  
  3. $widthFactor = 0.78125
  4. $heightFactor = 0.0333333333333333
  5.  
  6. $form1.Location = New-Object System.Drawing.Point(($monitor.WorkingArea.Width * $widthFactor), ($monitor.WorkingArea.Height * $heightFactor))
  7.  
  8.  
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Dynamic form location based on monitor size?

Post by jvierra »

Just set the location to the right upper maximum of the screen after subtracting the form width plus any extra.
austindodig
Posts: 3
Last visit: Mon Oct 16, 2023 12:04 pm
Has voted: 1 time

Re: Dynamic form location based on monitor size?

Post by austindodig »

Thanks for the tip! I'm still getting my feet wet with PS Studio. How exactly would I do this?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Dynamic form location based on monitor size?

Post by jvierra »

[System.Windows.Forms.Screen]::AllScreens[0].bounds.width - ($f.bounds.Width +50)

This gets the left upper target position for the new location.
austindodig
Posts: 3
Last visit: Mon Oct 16, 2023 12:04 pm
Has voted: 1 time

Re: Dynamic form location based on monitor size?

Post by austindodig »

Thanks!
This topic is 2 years and 10 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked