我的 PowerShell 脚本中有一个无边框 winforms 窗口:
调整大小本身有效,但调整大小时窗口水平移动。此外,每次我开始调整大小时,窗口向左或向右跳一次。
由于原始脚本非常大,我做了一个显示问题的示例:
# 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()
谢谢!
答案 0 :(得分:1)
一些小问题。
将circle1 = mycanvas.create_oval(x-r, y-r, x+r, y+r, outline='#000000',
fill=next(color_iteration))
变量初始化为0。
始终将其称为$script:lastmx
而不是$script:lastmx
。
第$lastmx
行实际上应该是$Form1.Left = $Form1_LeftPos - $diffX
这对我有用:
$Form1.Left = $Form1_LeftPos + $diffX