gpt4 book ai didi

powershell - 为什么拖放只能在 ISE 中工作,我会错过装配吗? (PowerShell 中的 Windows 窗体)

转载 作者:行者123 更新时间:2023-12-03 01:00:43 24 4
gpt4 key购买 nike

我有一个更大的 powershell 应用程序,它使用拖放来处理引号。一切正常,但仅在 ISE 中。当我通过桌面上的快捷方式使用脚本时 (C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -NoProfile -file "D:\Powershell\test\dragndrop.ps1") 拖放是不工作。

错误信息是:

找不到类型 [System.Windows.DragDropEffects]

我的PowerShell版本:

Name                           Value                                                                                                                                                                                                
---- -----
PSVersion 5.1.18362.145
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.18362.145
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1

在 Windows 10 64 位、1903 上运行

这是我在应用程序中使用的表格。它已修改,因此您可以复制并粘贴脚本以进行测试而无需任何修改。
Add-Type -AssemblyName System.Collections
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName Microsoft.Office.Interop.Excel


[System.Windows.Forms.Application]::EnableVisualStyles();

$form = New-Object System.Windows.Forms.Form
$form.Visible = $false
[void]$form.SuspendLayout()
$form.ClientSize = New-Object System.Drawing.Size(320,380)
$form.StartPosition = 'CenterScreen'
$form.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle
$form.MaximizeBox = $false
$form.MinimizeBox = $false
$form.HelpButton = $true

$CTRL_TabCtrl = New-Object System.Windows.Forms.TabControl
$CTRL_TabCtrl.Location = New-Object System.Drawing.Point(5,5)
$CTRL_TabCtrl.Size = New-Object System.Drawing.Size(310,370)
[void]$form.Controls.Add($CTRL_TabCtrl)

$CTRL_Tab1 = New-Object System.Windows.Forms.TabPage
$CTRL_Tab1.AutoSize = $true
$CTRL_Tab1.Text = 'Chose Quote'
$CTRL_Tab1.TabIndex = 1
[void]$CTRL_TabCtrl.Controls.Add($CTRL_Tab1)

$CTRL_Tab2 = New-Object System.Windows.Forms.TabPage
$CTRL_Tab2.AutoSize = $true
$CTRL_Tab2.Text = 'Settings'
$CTRL_Tab2.TabIndex = 2
[void]$CTRL_TabCtrl.Controls.Add($CTRL_Tab2)

$CTRL_label1 = New-Object System.Windows.Forms.Label
$CTRL_label1.Location = New-Object System.Drawing.Point(10,10)
$CTRL_label1.Size = New-Object System.Drawing.Size(280,20)
$CTRL_label1.Text = 'Please select quote (PDF or Excel):'
$CTRL_label1.Name = 'Label1'
$CTRL_label1.Add_MouseHover( $showHelp )
[void]$CTRL_Tab1.Controls.Add($CTRL_label1)

$CTRL_PathQuote = New-Object System.Windows.Forms.TextBox
$CTRL_PathQuote.Location = New-Object System.Drawing.Point(10,30)
$CTRL_PathQuote.Size = New-Object System.Drawing.Size(275,20)
$CTRL_PathQuote.MaxLength = 250
$CTRL_PathQuote.Name = 'PathQuote'
$CTRL_PathQuote.Add_MouseHover( $showHelp )
[void]$CTRL_Tab1.Controls.Add($CTRL_PathQuote)

$CTRL_UploadButton = New-Object System.Windows.Forms.Button
$CTRL_UploadButton.Location = New-Object System.Drawing.Point(10,60)
$CTRL_UploadButton.Size = New-Object System.Drawing.Size(120,23)
$CTRL_UploadButton.Text = 'Select File...'
$CTRL_UploadButton.Name = 'UploadButton'
$CTRL_UploadButton.Add_MouseHover( $showHelp )
#$CTRL_UploadButton.Add_Click( { Get-FileName-Dialog -textBox $CTRL_PathQuote } )
[void]$CTRL_Tab1.Controls.Add($CTRL_UploadButton)

$CTRL_label3 = New-Object System.Windows.Forms.Label
$CTRL_label3.Location = New-Object System.Drawing.Point(10,100)
$CTRL_label3.Size = New-Object System.Drawing.Size(280,20)
$CTRL_label3.Text = 'Or drop quote here: (drag && drop):'
$CTRL_label3.Name = 'Label3'
$CTRL_label3.Add_MouseHover( $showHelp )
[void]$CTRL_Tab1.Controls.Add($CTRL_label3)

$CTRL_PictureBox = New-Object Windows.Forms.PictureBox
$CTRL_PictureBox.Location = New-Object System.Drawing.Point(10,120)
$CTRL_PictureBox.Size = New-Object System.Drawing.Size(280,174)
$CTRL_PictureBox.BorderStyle = [System.Windows.Forms.BorderStyle]::FixedSingle
$CTRL_PictureBox.AllowDrop = $true

$CTRL_PictureBox.Add_DragOver({
if ($_.Data.GetDataPresent([System.Windows.Forms.DataFormats]::FileDrop)) {
foreach ($filename in $_.Data.GetData([System.Windows.Forms.DataFormats]::FileDrop)) {
if( @( '.pdf', '.xlsx').Contains( [System.IO.Path]::GetExtension($filename).ToLower() ) ) {
$_.Effect = [System.Windows.DragDropEffects]::Copy
$CTRL_PictureBox.BackColor = [System.Drawing.Color]::DarkGray
}
else {
$_.Effect = [System.Windows.DragDropEffects]::None
$CTRL_PictureBox.BackColor = [System.Drawing.Color]::Transparent
}
break #only first file
}
}
})

$CTRL_PictureBox.Add_DragDrop({
if ($_.Data.GetDataPresent([System.Windows.Forms.DataFormats]::FileDrop)) {
foreach ($filename in $_.Data.GetData([System.Windows.Forms.DataFormats]::FileDrop)) {
if( @( '.pdf', '.xlsx').Contains( [System.IO.Path]::GetExtension($filename).ToLower() ) ) {
$_.Effect = [System.Windows.DragDropEffects]::All
$CTRL_PathQuote.Text = $filename
}
else {
$_.Effect = [System.Windows.DragDropEffects]::None
}
break #only first file
}
}
})

[void]$CTRL_Tab1.Controls.Add($CTRL_PictureBox)

$CTRL_OKButton1 = New-Object System.Windows.Forms.Button
$CTRL_OKButton1.Location = New-Object System.Drawing.Point(70,310)
$CTRL_OKButton1.Size = New-Object System.Drawing.Size(75,23)
$CTRL_OKButton1.Text = 'Start'
$CTRL_OKButton1.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $CTRL_OKButton1
[void]$CTRL_Tab1.Controls.Add($CTRL_OKButton1)
$CTRL_OKButton1.Enabled = $false

$CTRL_CancelButton1 = New-Object System.Windows.Forms.Button
$CTRL_CancelButton1.Location = New-Object System.Drawing.Point(150,310)
$CTRL_CancelButton1.Size = New-Object System.Drawing.Size(75,23)
$CTRL_CancelButton1.Text = 'Abbrechen'
$CTRL_CancelButton1.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $CTRL_CancelButton1
[void]$CTRL_Tab1.Controls.Add($CTRL_CancelButton1)



$scriptBlock_OkButton1 = { $CTRL_PathQuote.Text.Trim().Length -gt 0 -and (Test-Path -Path $CTRL_PathQuote.Text -PathType leaf) }

$scriptBlock_PathQuoteChanged = {
$CTRL_OKButton1.Enabled = & $scriptBlock_OkButton1;
$CTRL_PictureBox.BackColor = [System.Drawing.Color]::Transparent
if( !$CTRL_OKButton1.Enabled ) {
$CTRL_PictureBox.Image = $dragnDropImg
}
else {
$CTRL_PictureBox.Image = $dragnDropOkImg
}
}


$CTRL_OKButton1.Enabled = & $scriptBlock_OkButton1

[void]$CTRL_PathQuote.Add_TextChanged( { & $scriptBlock_PathQuoteChanged } )
[void]$CTRL_PictureBox.Add_DragLeave( { & $scriptBlock_PathQuoteChanged } )

[void]$form.ResumeLayout()

$userInput = $form.ShowDialog()

最佳答案

使用这个[System.Windows.Forms.DragDropEffects]而不是 [System.Windows.DragDropEffects]在你使用它的所有地方..

关于powershell - 为什么拖放只能在 ISE 中工作,我会错过装配吗? (PowerShell 中的 Windows 窗体),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56747543/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com