Winforms PowerShell Windows窗体脚本中无边框窗口的自定义大小

Winforms PowerShell Windows窗体脚本中无边框窗口的自定义大小,winforms,powershell,resize,Winforms,Powershell,Resize,我的PowerShell脚本中有一个无边框的Windows窗体窗口: 我希望用户能够调整它的大小,但它只能水平地向左扩展 窗口的右边框需要始终保持在其原始位置 调整大小本身可以工作,但窗口在调整大小期间水平移动。而且,每次我开始调整大小时,窗口都会向左或向右跳一次 由于原始脚本相当大,我制作了一个示例来说明问题: 有几个小问题: 将$script:lastmx变量初始化为0 始终将其称为$script:lastmx,而不是$lastmx 行$Form1.Left=$Form1\u Left

我的PowerShell脚本中有一个无边框的Windows窗体窗口:

  • 我希望用户能够调整它的大小,但它只能水平地向左扩展
  • 窗口的右边框需要始终保持在其原始位置
调整大小本身可以工作,但窗口在调整大小期间水平移动。而且,每次我开始调整大小时,窗口都会向左或向右跳一次

由于原始脚本相当大,我制作了一个示例来说明问题:


有几个小问题:

  • $script:lastmx
    变量初始化为0

  • 始终将其称为
    $script:lastmx
    ,而不是
    $lastmx

  • $Form1.Left=$Form1\u LeftPos-$diffX
    应实际读取
    $Form1.Left=$Form1\u LeftPos+$diffX

这对我来说很有用:

$script:lastmx=0
$ResizeTimer.add\u勾选({
$MousePosX=([System.Windows.Forms.Cursor]::Position).x
$Form1_LeftPos=$Form1.Left
如果($Script:lastmx-eq 0){
$script:lastmx=$MousePosX
}
$diffX=$MousePosX-$Script:lastmx
$Form1.width=($Form1.width-$diffX)
$Form1.Left=$Form1\u LeftPos+$diffX
$script:lastmx=$MousePosX
})
# Custom resize example
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

$Form1 = New-Object System.Windows.Forms.Form
$Form1.size = "500,500"
$Form1.FormBorderStyle = "None"
$Form1.TopMost = $true
$Form1.StartPosition = "CenterScreen"
$Form1.add_MouseClick({
    if ($_.Button -eq "Right") {
        $form1.close()
    }
})

$resizeBar = New-Object System.Windows.Forms.Label
$resizeBar.BackColor = "150,150,150"
$resizeBar.Size = "8,500"
$resizeBar.Cursor = "SizeWE"
$Form1.controls.add($resizeBar)

$resizeBar.add_MouseDown({ $ResizeTimer.start()})
$resizeBar.add_MouseUp({ $ResizeTimer.stop()})

$comp = new-object System.ComponentModel.Container
$ResizeTimer = New-Object System.Windows.Forms.Timer($comp)
$ResizeTimer.Interval = 10

$ResizeTimer.add_Tick({
    $MousePosX =  ([System.Windows.Forms.Cursor]::Position).x
    $Form1_LeftPos = $Form1.Left

    if ($lastmx -eq 0) {
        $script:lastmx = $MousePosX
    }
    $diffX =  $MousePosX - $lastmx

    $Form1.width = ($Form1.width - $diffX)
    $Form1.Left = $Form1_LeftPos - $diffX
    $script:lastmx = $MousePosX
})

$Form1.showdialog()