how to maximise and return a form to windowed state?

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 6 years and 2 weeks 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
RARLINX1
Posts: 9
Last visit: Mon Feb 20, 2023 1:34 pm

how to maximise and return a form to windowed state?

Post by RARLINX1 »

HI,

I currently have a border-less form and have gone about recreating my own title bar controls.
So far i can drag the window around (that one took me a whole day to figure out), close and minimise. I can maximise to full screen (akin to F11 in a web browser) but cant seems to figure out how to do it with out overlapping the taskbar and then restore it to the previous size once its full screen.

What is the code needed to maximise and restore properly?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: how to maximise and return a form to windowed state?

Post by jvierra »

Simplest way:

Code: Select all

    # minimize form
    $winstate = $form1.WindowState
    $form1.WindowState = 'Minimized'
    
    # return to previous state
    $form1.WindowState = $winstate
RARLINX1
Posts: 9
Last visit: Mon Feb 20, 2023 1:34 pm

Re: how to maximise and return a form to windowed state?

Post by RARLINX1 »

Perfect thanks! I was getting lost with trying to use derivatives of
  1. Set-WindowStyle -Style MAXIMIZE -MainWindowHandle (Get-Process -Id $pid).MainWindowHandle
$MainForm.WindowState = 'Minimized'
$MainForm.WindowState = 'Normal'
This worked nicely for the job.

however $MainForm.WindowState = 'Maximized' will still obscure the taskbar. Do you know of a workaround for that?
RARLINX1
Posts: 9
Last visit: Mon Feb 20, 2023 1:34 pm

Re: how to maximise and return a form to windowed state?

Post by RARLINX1 »

OK I've got this far
  1. $buttonMaximize_Click={
  2.     $maxlocation = [System.Windows.Forms.SystemInformation]::WorkingArea.Location
  3.     $maxsize = [System.Windows.Forms.SystemInformation]::WorkingArea.Size
  4.     $form1.Location = $maxlocation
  5.     $form1.Size = $maxsize
  6. }
but that will breake

Code: Select all

$form1.WindowState = 'Normal' / $winstate
from returning it to a smaller window.

*edit: and it also wont work on any secondary screen.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: how to maximise and return a form to windowed state?

Post by jvierra »

You can have on or the other. If you want a programmable dynamic form then set the size and position. If you want the form to be returned to its' last position and size (minimize) then don't change the form.
You can also save the form sizing object and reset it after a minimize. Look for the event that tells you the form is being activated and reset the saved size.

To manage custom form actions you will need to learn the events of the forms. (size, resize, activated, shown ...) These event allow you to customize this behavior.

As a long time forms programmer - since Windows 0.9 - I can tell you that learning the events and how to use them will allow you to produce highly customized forms. It is a learning curve all programmers have to go through with Windows, Mac, X-Windows(Unix) or any other GUI operating system.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: how to maximise and return a form to windowed state?

Post by jvierra »

Here is an old blog post discussing some of the limitations of PowerShell forms.

http://tech-comments.blogspot.com/
This topic is 6 years and 2 weeks 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