Custom resize of borderless window in a PowerShell Windows Forms script
<p>I have a border-less Windows Forms window in my PowerShell script:</p>

<ul>
<li>I want the user to be able to resize it, but it should only <strong>horizontally grow to the left</strong>.</li>
<li>The right border of the window needs to stay in its original place at all times.</li>
</ul>

<p>The resizing itself works, but the window moves horizontally during the resizing. Also, every time I start resizing, the window jumps to the left or right once.</p>

<p><em>As the original script is quite large, I made an example that shows the problem:</em></p>

<pre><code># 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()
</code></pre>