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.
Locked
austindodig
Posts: 3
Last visit: Wed May 26, 2021 7:36 am
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: 14825
Last visit: Thu Jun 10, 2021 11:20 pm
Answers: 10
Has voted: 3 times
Been upvoted: 10 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: Wed May 26, 2021 7:36 am
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: 14825
Last visit: Thu Jun 10, 2021 11:20 pm
Answers: 10
Has voted: 3 times
Been upvoted: 10 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: Wed May 26, 2021 7:36 am
Has voted: 1 time

Re: Dynamic form location based on monitor size?

Post by austindodig »

Thanks!

Locked