问题描述
我想创建一个基于 PowerShell 的 GUI 和 Windows 窗体,以显示不同容器中的项目,并使用户能够将项目(控制对象)从一个容器拖到另一个容器。由于一些不好的闪烁,我复制了这个问题的答案并将代码转换为 PowerShell: How to double buffer .NET controls on a form?
问题是,当我开始拖动对象时,它们会在表单上“涂抹”,但我找不到解决方法。
class Dictionary : System.Collections.DictionaryBase
{
Dictionary() {}
[Bool]Exists([System.Object] $Key) {return ($Key -in $this.Keys) }
}
function SetDoubleBuffered()
{
param([System.Windows.Forms.Control] $TargetControl)
[System.Reflection.PropertyInfo] $DoubleBufferedProp = [System.Windows.Forms.Control].GetProperty("DoubleBuffered",[System.Reflection.BindingFlags]::NonPublic -bor [System.Reflection.BindingFlags]::Instance)
$DoubleBufferedProp.SetValue($TargetControl,$True,$Null)
}
function Button_MouseDown()
{
[CmdletBinding()]
param(
[parameter(Mandatory=$True)][System.Object] $Sender,[parameter(Mandatory=$True)][System.EventArgs] $EventArguments
)
$Sender.Tag.DragStart = $False
$Sender.Tag.StartX = $EventArguments.X
$Sender.Tag.StartY = $EventArguments.Y
}
function Button_MouseUp()
{
[CmdletBinding()]
param(
[parameter(Mandatory=$True)][System.Object] $Sender,[parameter(Mandatory=$True)][System.EventArgs] $EventArguments
)
$Sender.Tag.DragStart = $False
$Sender.Tag.StartX = 0
$Sender.Tag.StartY = 0
}
function Button_MouseMove()
{
[CmdletBinding()]
param(
[parameter(Mandatory=$True)][System.Object] $Sender,[parameter(Mandatory=$True)][System.EventArgs] $EventArguments
)
if ($EventArguments.Button.value__ -gt 0)
{
if (-not $Sender.Tag.DragStart)
{
if ([System.Math]::sqrt([System.Math]::Pow($Sender.Tag.StartX - $EventArguments.X,2) + [System.Math]::Pow($Sender.Tag.StartY - $EventArguments.Y,2)) -ge 10)
{
$Sender.Tag.DragStart = $true
}
}
else
{
$Sender.Left = $Sender.Left + ($EventArguments.X - $Sender.Tag.StartX)
$Sender.Top = $Sender.Top + ($EventArguments.Y - $Sender.Tag.StartY)
}
}
else
{
$Sender.Tag.DragStart = $False
$Sender.Tag.StartX = 0
$Sender.Tag.StartY = 0
}
}
function OpenForm()
{
$Form = [System.Windows.Forms.Form]::new()
$Form.Text = "DragTest"
$Form.Width = 900
$Form.Height = 900
$Button = [System.Windows.Forms.Button]::new()
$Button.Left = 10
$Button.Top = 30
$Button.Height = 20
$Button.Width = 100
$Button.Text = "MyButton"
$Button.Name = "SomeButton"
$Button.Tag = [Dictionary]::new()
$Button.Add_MouseDown({Button_MouseDown -Sender $Button -EventArguments $_})
$Button.Add_MouseUp({Button_MouseUp -Sender $Button -EventArguments $_})
$Button.Add_MouseMove({Button_MouseMove -Sender $Button -EventArguments $_})
$Form.Controls.Add($Button)
SetDoubleBuffered -TargetControl $Form
SetDoubleBuffered -TargetControl $Button
$Form.ShowDialog() | Out-Null
}
cls
OpenForm
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)