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.
Locked
User avatar
luk4mu5
Posts: 9
Joined: Wed May 17, 2017 4:17 pm
Location: England

how to maximise and return a form to windowed state?

Post by luk4mu5 » Mon Mar 12, 2018 2:34 pm

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?

User avatar
jvierra
Posts: 13486
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » Mon Mar 12, 2018 3:09 pm

Simplest way:

Code: Select all

    # minimize form
    $winstate = $form1.WindowState
    $form1.WindowState = 'Minimized'
    
    # return to previous state
    $form1.WindowState = $winstate

User avatar
luk4mu5
Posts: 9
Joined: Wed May 17, 2017 4:17 pm
Location: England

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

Post by luk4mu5 » Mon Mar 12, 2018 4:09 pm

Perfect thanks! I was getting lost with trying to use derivatives of

Code: Select all

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?

User avatar
luk4mu5
Posts: 9
Joined: Wed May 17, 2017 4:17 pm
Location: England

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

Post by luk4mu5 » Mon Mar 12, 2018 4:52 pm

OK I've got this far

Code: Select all

$buttonMaximize_Click={
$maxlocation = [System.Windows.Forms.SystemInformation]::WorkingArea.Location
$maxsize = [System.Windows.Forms.SystemInformation]::WorkingArea.Size
$form1.Location = $maxlocation
$form1.Size = $maxsize
}
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.

User avatar
jvierra
Posts: 13486
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » Mon Mar 12, 2018 5:14 pm

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.

User avatar
jvierra
Posts: 13486
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra » Mon Mar 12, 2018 5:39 pm

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

http://tech-comments.blogspot.com/

Locked