Изменение размера формы с powershell - PullRequest
0 голосов
/ 16 января 2020

Я создал форму в powershell и установил значение borderstyle на none, я уже понял, как перетащить окно с помощью:

$MainForm_MouseDown=[System.Windows.Forms.MouseEventHandler]{
$global:dragging = $true
$global:mouseDragX = [System.Windows.Forms.Cursor]::Position.X - $MainForm.Left
$global:mouseDragY = [System.Windows.Forms.Cursor]::Position.Y - $MainForm.Top}

$MainForm_MouseMove=[System.Windows.Forms.MouseEventHandler]{

if ($global:dragging)
{
    $screen = [System.Windows.Forms.Screen]::PrimaryScreen.WorkingArea
    $currentX = [System.Windows.Forms.Cursor]::Position.X
    $currentY = [System.Windows.Forms.Cursor]::Position.Y
    [int]$newX = [Math]::Min($currentX - $global:mouseDragX, $screen.Right - $MainForm.Width)
    [int]$newY = [Math]::Min($currentY - $global:mouseDragY, $screen.Bottom - $MainForm.Height)
    $MainForm.Location = New-Object System.Drawing.Point($newX, $newY)
}
}

, но я не могу понять, как Чтобы изменить размер формы, я добавил панели по бокам, чтобы изменить размер формы слева направо и вверх и вниз. $ panelresizebottom, $ panelresizetop, $ panelresizeleft и $ panelresizeright

Спасибо!

РЕДАКТИРОВАТЬ:

Полный скрипт ниже:

#region Source: Startup.pss
#----------------------------------------------


function Main {
<#
    .SYNOPSIS
        The Main function starts the project application.

    .PARAMETER Commandline
        $Commandline contains the complete argument string passed to the script packager executable.

    .NOTES
        Use this function to initialize your script and to call GUI forms.

    .NOTES
        To get the console output in the Packager (Forms Engine) use: 
        $ConsoleOutput (Type: System.Collections.ArrayList)
#>
    Param ([String]$Commandline)

    if((Show-MainForm_psf) -eq 'OK')
    {

    }

    $script:ExitCode = 0 #Set the exit code for the Packager
}



#endregion Source: Startup.pss

#region Source: MainForm.psf
function Show-MainForm_psf
{
    #----------------------------------------------
    #region Import the Assemblies
    #----------------------------------------------
    [void][reflection.assembly]::Load('System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
    [void][reflection.assembly]::Load('System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
    #endregion Import Assemblies

    #----------------------------------------------
    #region Generated Form Objects
    #----------------------------------------------
    [System.Windows.Forms.Application]::EnableVisualStyles()
    $MainForm = New-Object 'System.Windows.Forms.Form'
    $ResizePanelRight = New-Object 'System.Windows.Forms.Panel'
    $ResizePanelBottom = New-Object 'System.Windows.Forms.Panel'
    $panel2 = New-Object 'System.Windows.Forms.Panel'
    $buttonButton1 = New-Object 'System.Windows.Forms.Button'
    $button1 = New-Object 'System.Windows.Forms.Button'
    $ResizePanelLeft = New-Object 'System.Windows.Forms.Panel'
    $panel1 = New-Object 'System.Windows.Forms.Panel'
    $picturebox3 = New-Object 'System.Windows.Forms.PictureBox'
    $picturebox2 = New-Object 'System.Windows.Forms.PictureBox'
    $picturebox1 = New-Object 'System.Windows.Forms.PictureBox'
    $picturebox4 = New-Object 'System.Windows.Forms.PictureBox'
    $ResizePanelTop = New-Object 'System.Windows.Forms.Panel'
    $InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
    #endregion Generated Form Objects

    #----------------------------------------------
    # User Generated Script
    #----------------------------------------------

    $MainForm_Load={
    #TODO: Initialize Form Controls here

    }

    $buttonCallChildForm_Click={
        #TODO: Place custom script here
        if((Show-ChildForm_psf) -eq 'OK')
        {

        }
    }

    $panel1_Paint=[System.Windows.Forms.PaintEventHandler]{
    #Event Argument: $_ = [System.Windows.Forms.PaintEventArgs]
        #TODO: Place custom script here

    }

    $picturebox1_Click={
        $MainForm.close()

    }

    $MainForm_MouseDown=[System.Windows.Forms.MouseEventHandler]{
    #Event Argument: $_ = [System.Windows.Forms.MouseEventArgs]
        $global:dragging = $true
        $global:mouseDragX = [System.Windows.Forms.Cursor]::Position.X - $MainForm.Left
        $global:mouseDragY = [System.Windows.Forms.Cursor]::Position.Y - $MainForm.Top
    }

    $MainForm_MouseMove=[System.Windows.Forms.MouseEventHandler]{
    #Event Argument: $_ = [System.Windows.Forms.MouseEventArgs]
        if ($global:dragging)
        {
            $screen = [System.Windows.Forms.Screen]::PrimaryScreen.WorkingArea
            $currentX = [System.Windows.Forms.Cursor]::Position.X
            $currentY = [System.Windows.Forms.Cursor]::Position.Y
            [int]$newX = [Math]::Min($currentX - $global:mouseDragX, $screen.Right - $MainForm.Width)
            [int]$newY = [Math]::Min($currentY - $global:mouseDragY, $screen.Bottom - $MainForm.Height)
            $MainForm.Location = New-Object System.Drawing.Point($newX, $newY)

        }
    }

    $MainForm_MouseUp=[System.Windows.Forms.MouseEventHandler]{
    #Event Argument: $_ = [System.Windows.Forms.MouseEventArgs]
        #TODO: Place custom script here
        $global:dragging = $false

    }



    $ResizePanelBottom_MouseDown=[System.Windows.Forms.MouseEventHandler]{
    #Event Argument: $_ = [System.Windows.Forms.MouseEventArgs]
    }

    $ResizePanelBottom_MouseMove=[System.Windows.Forms.MouseEventHandler]{
    #Event Argument: $_ = [System.Windows.Forms.MouseEventArgs]
    }

    $ResizePanelBottom_MouseUp=[System.Windows.Forms.MouseEventHandler]{
    #Event Argument: $_ = [System.Windows.Forms.MouseEventArgs]

    }

    $ResizePanelRight_MouseDown=[System.Windows.Forms.MouseEventHandler]{
    #Event Argument: $_ = [System.Windows.Forms.MouseEventArgs]
        #TODO: Place custom script here

    }

    $ResizePanelRight_MouseMove=[System.Windows.Forms.MouseEventHandler]{
    #Event Argument: $_ = [System.Windows.Forms.MouseEventArgs]
        #TODO: Place custom script here

    }

    $ResizePanelRight_MouseUp=[System.Windows.Forms.MouseEventHandler]{
    #Event Argument: $_ = [System.Windows.Forms.MouseEventArgs]
        #TODO: Place custom script here

    }

    $ResizePanelTop_MouseDown=[System.Windows.Forms.MouseEventHandler]{
    #Event Argument: $_ = [System.Windows.Forms.MouseEventArgs]
        #TODO: Place custom script here

    }

    $ResizePanelTop_MouseMove=[System.Windows.Forms.MouseEventHandler]{
    #Event Argument: $_ = [System.Windows.Forms.MouseEventArgs]
        #TODO: Place custom script here

    }

    $ResizePanelTop_MouseUp=[System.Windows.Forms.MouseEventHandler]{
    #Event Argument: $_ = [System.Windows.Forms.MouseEventArgs]
        #TODO: Place custom script here

    }

    $ResizePanelLeft_MouseDown=[System.Windows.Forms.MouseEventHandler]{
    #Event Argument: $_ = [System.Windows.Forms.MouseEventArgs]
        #TODO: Place custom script here

    }

    $ResizePanelLeft_MouseMove=[System.Windows.Forms.MouseEventHandler]{
    #Event Argument: $_ = [System.Windows.Forms.MouseEventArgs]
        #TODO: Place custom script here

    }

    $ResizePanelLeft_MouseUp=[System.Windows.Forms.MouseEventHandler]{
    #Event Argument: $_ = [System.Windows.Forms.MouseEventArgs]
        #TODO: Place custom script here

    }

    # --End User Generated Script--
    #----------------------------------------------
    #region Generated Events
    #----------------------------------------------

    $Form_StateCorrection_Load=
    {
        #Correct the initial state of the form to prevent the .Net maximized form issue
        $MainForm.WindowState = $InitialFormWindowState
    }

    $Form_StoreValues_Closing=
    {
        #Store the control values
    }


    $Form_Cleanup_FormClosed=
    {
        #Remove all event handlers from the controls
        try
        {
            $ResizePanelRight.remove_MouseDown($ResizePanelRight_MouseDown)
            $ResizePanelRight.remove_MouseMove($ResizePanelRight_MouseMove)
            $ResizePanelRight.remove_MouseUp($ResizePanelRight_MouseUp)
            $ResizePanelBottom.remove_MouseDown($ResizePanelBottom_MouseDown)
            $ResizePanelBottom.remove_MouseMove($ResizePanelBottom_MouseMove)
            $ResizePanelBottom.remove_MouseUp($ResizePanelBottom_MouseUp)
            $ResizePanelLeft.remove_MouseDown($ResizePanelLeft_MouseDown)
            $ResizePanelLeft.remove_MouseMove($ResizePanelLeft_MouseMove)
            $ResizePanelLeft.remove_MouseUp($ResizePanelLeft_MouseUp)
            $picturebox1.remove_Click($picturebox1_Click)
            $ResizePanelTop.remove_MouseDown($ResizePanelTop_MouseDown)
            $ResizePanelTop.remove_MouseMove($ResizePanelTop_MouseMove)
            $ResizePanelTop.remove_MouseUp($ResizePanelTop_MouseUp)
            $panel1.remove_Paint($panel1_Paint)
            $MainForm.remove_Load($MainForm_Load)
            $MainForm.remove_MouseDown($MainForm_MouseDown)
            $MainForm.remove_MouseMove($MainForm_MouseMove)
            $MainForm.remove_MouseUp($MainForm_MouseUp)
            $MainForm.remove_Load($Form_StateCorrection_Load)
            $MainForm.remove_Closing($Form_StoreValues_Closing)
            $MainForm.remove_FormClosed($Form_Cleanup_FormClosed)
        }
        catch { Out-Null <# Prevent PSScriptAnalyzer warning #> }
    }
    #endregion Generated Events

    #----------------------------------------------
    #region Generated Form Code
    #----------------------------------------------
    $MainForm.SuspendLayout()
    $panel1.SuspendLayout()
    $panel2.SuspendLayout()
    #
    # MainForm
    #
    $MainForm.Controls.Add($ResizePanelRight)
    $MainForm.Controls.Add($ResizePanelBottom)
    $MainForm.Controls.Add($panel2)
    $MainForm.Controls.Add($panel1)
    $MainForm.AutoScaleDimensions = '6, 13'
    $MainForm.AutoScaleMode = 'Font'
    $MainForm.ClientSize = '1264, 653'
    $MainForm.FormBorderStyle = 'None'
    $MainForm.Margin = '4, 4, 4, 4'
    $MainForm.Name = 'MainForm'
    $MainForm.StartPosition = 'CenterScreen'
    $MainForm.Text = 'Main Form'
    $MainForm.add_Load($MainForm_Load)
    $MainForm.add_MouseDown($MainForm_MouseDown)
    $MainForm.add_MouseMove($MainForm_MouseMove)
    $MainForm.add_MouseUp($MainForm_MouseUp)
    #
    # ResizePanelRight
    #
    $ResizePanelRight.Cursor = 'SizeWE'
    $ResizePanelRight.Dock = 'Right'
    $ResizePanelRight.Location = '1259, 51'
    $ResizePanelRight.Name = 'ResizePanelRight'
    $ResizePanelRight.Size = '5, 597'
    $ResizePanelRight.TabIndex = 7
    $ResizePanelRight.add_MouseDown($ResizePanelRight_MouseDown)
    $ResizePanelRight.add_MouseMove($ResizePanelRight_MouseMove)
    $ResizePanelRight.add_MouseUp($ResizePanelRight_MouseUp)
    #
    # ResizePanelBottom
    #
    $ResizePanelBottom.Cursor = 'SizeNS'
    $ResizePanelBottom.Dock = 'Bottom'
    $ResizePanelBottom.Location = '182, 648'
    $ResizePanelBottom.Name = 'ResizePanelBottom'
    $ResizePanelBottom.Size = '1082, 5'
    $ResizePanelBottom.TabIndex = 6
    $ResizePanelBottom.add_MouseDown($ResizePanelBottom_MouseDown)
    $ResizePanelBottom.add_MouseMove($ResizePanelBottom_MouseMove)
    $ResizePanelBottom.add_MouseUp($ResizePanelBottom_MouseUp)
    #
    # panel2
    #
    $panel2.Controls.Add($buttonButton1)
    $panel2.Controls.Add($button1)
    $panel2.Controls.Add($ResizePanelLeft)
    $panel2.BackColor = 'Desktop'
    $panel2.Dock = 'Left'
    $panel2.Location = '0, 51'
    $panel2.Name = 'panel2'
    $panel2.Size = '182, 602'
    $panel2.TabIndex = 1
    #
    # buttonButton1
    #
    $buttonButton1.BackColor = 'Navy'
    $buttonButton1.FlatAppearance.BorderSize = 0
    $buttonButton1.FlatAppearance.MouseOverBackColor = '0, 0, 64'
    $buttonButton1.FlatStyle = 'Flat'
    $buttonButton1.Location = '3, 40'
    $buttonButton1.Margin = '0, 0, 0, 0'
    $buttonButton1.Name = 'buttonButton1'
    $buttonButton1.Size = '179, 40'
    $buttonButton1.TabIndex = 9
    $buttonButton1.Text = 'button1'
    $buttonButton1.UseCompatibleTextRendering = $True
    $buttonButton1.UseVisualStyleBackColor = $False
    #
    # button1
    #
    $button1.BackColor = 'Navy'
    $button1.FlatAppearance.BorderSize = 0
    $button1.FlatAppearance.MouseOverBackColor = '0, 0, 64'
    $button1.FlatStyle = 'Flat'
    $button1.Location = '3, 0'
    $button1.Name = 'button1'
    $button1.Size = '179, 40'
    $button1.TabIndex = 8
    $button1.Text = 'button1'
    $button1.UseCompatibleTextRendering = $True
    $button1.UseVisualStyleBackColor = $False
    #
    # ResizePanelLeft
    #
    $ResizePanelLeft.Cursor = 'SizeWE'
    $ResizePanelLeft.Dock = 'Left'
    $ResizePanelLeft.Location = '0, 0'
    $ResizePanelLeft.Name = 'ResizePanelLeft'
    $ResizePanelLeft.Size = '5, 602'
    $ResizePanelLeft.TabIndex = 6
    $ResizePanelLeft.add_MouseDown($ResizePanelLeft_MouseDown)
    $ResizePanelLeft.add_MouseMove($ResizePanelLeft_MouseMove)
    $ResizePanelLeft.add_MouseUp($ResizePanelLeft_MouseUp)
    #
    # panel1
    #
    $panel1.Controls.Add($picturebox3)
    $panel1.Controls.Add($picturebox2)
    $panel1.Controls.Add($picturebox1)
    $panel1.Controls.Add($picturebox4)
    $panel1.Controls.Add($ResizePanelTop)
    $panel1.BackColor = '255, 128, 0'
    $panel1.Dock = 'Top'
    $panel1.Location = '0, 0'
    $panel1.Name = 'panel1'
    $panel1.Size = '1264, 51'
    $panel1.TabIndex = 0
    $panel1.add_Paint($panel1_Paint)
    #
    # picturebox3
    #
    $picturebox3.Cursor = 'Hand'
    $picturebox3.Location = '1106, 12'
    $picturebox3.Name = 'picturebox3'
    $picturebox3.Size = '30, 30'
    $picturebox3.SizeMode = 'StretchImage'
    $picturebox3.TabIndex = 4
    $picturebox3.TabStop = $False
    #
    # picturebox2
    #
    $picturebox2.Cursor = 'Hand'
    $picturebox2.Location = '1160, 12'
    $picturebox2.Name = 'picturebox2'
    $picturebox2.Size = '30, 30'
    $picturebox2.SizeMode = 'StretchImage'
    $picturebox2.TabIndex = 3
    $picturebox2.TabStop = $False
    #
    # picturebox1
    #
    $picturebox1.Cursor = 'Hand'
    $picturebox1.Location = '1215, 12'
    $picturebox1.Name = 'picturebox1'
    $picturebox1.Size = '30, 30'
    $picturebox1.SizeMode = 'StretchImage'
    $picturebox1.TabIndex = 2
    $picturebox1.TabStop = $False
    $picturebox1.add_Click($picturebox1_Click)
    #
    # picturebox4
    #
    $picturebox4.Location = '0, 0'
    $picturebox4.Name = 'picturebox4'
    $picturebox4.Size = '182, 51'
    $picturebox4.SizeMode = 'StretchImage'
    $picturebox4.TabIndex = 0
    $picturebox4.TabStop = $False
    #
    # ResizePanelTop
    #
    $ResizePanelTop.Cursor = 'SizeNS'
    $ResizePanelTop.Dock = 'Top'
    $ResizePanelTop.Location = '0, 0'
    $ResizePanelTop.Name = 'ResizePanelTop'
    $ResizePanelTop.Size = '1264, 5'
    $ResizePanelTop.TabIndex = 5
    $ResizePanelTop.add_MouseDown($ResizePanelTop_MouseDown)
    $ResizePanelTop.add_MouseMove($ResizePanelTop_MouseMove)
    $ResizePanelTop.add_MouseUp($ResizePanelTop_MouseUp)
    $panel2.ResumeLayout()
    $panel1.ResumeLayout()
    $MainForm.ResumeLayout()
    #endregion Generated Form Code

    #----------------------------------------------

    #Save the initial state of the form
    $InitialFormWindowState = $MainForm.WindowState
    #Init the OnLoad event to correct the initial state of the form
    $MainForm.add_Load($Form_StateCorrection_Load)
    #Clean up the control events
    $MainForm.add_FormClosed($Form_Cleanup_FormClosed)
    #Store the control values when form is closing
    $MainForm.add_Closing($Form_StoreValues_Closing)
    #Show the Form
    return $MainForm.ShowDialog()

}
#endregion Source: MainForm.psf

#region Source: Globals.ps1
    #--------------------------------------------
    # Declare Global Variables and Functions here
    #--------------------------------------------

    $global:dragging = $false
    $global:mouseDragX = 0
    $global:mouseDragY = 0

#endregion Source: Globals.ps1

#Start the application
Main ($CommandLine)
...