Page 1 of 1

Dynamic form location based on monitor size?

Posted: Mon May 03, 2021 2:13 pm
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.  

Re: Dynamic form location based on monitor size?

Posted: Mon May 03, 2021 5:17 pm
by jvierra
Just set the location to the right upper maximum of the screen after subtracting the form width plus any extra.

Re: Dynamic form location based on monitor size?

Posted: Tue May 04, 2021 5:30 am
by austindodig
Thanks for the tip! I'm still getting my feet wet with PS Studio. How exactly would I do this?

Re: Dynamic form location based on monitor size?

Posted: Tue May 04, 2021 5:33 am
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.

Re: Dynamic form location based on monitor size?

Posted: Tue May 04, 2021 7:26 am
by austindodig
Thanks!