2015-06-01 16 views
0

這是一個基本的窗體窗體,其中包含一些按鈕,應該將它們複製到Clickboard,無論何時單擊該變量,它都會工作一次,然後按鈕停止響應,任何想法爲什麼?使用clip.exe的Windows窗體按鈕一次性工作並停止

的代碼是:

Add-Type -AssemblyName System.Windows.Forms 
$Form = New-Object System.Windows.Forms.Form 
$Form.Text = "Click2Copy :)" 
$Font = New-Object System.Drawing.Font ("Roman", 11, [System.Drawing.FontStyle]::Regular) 
$Form.Font = $Font 
$Form.AutoSize = $false 
$Form.AutoSizeMode = "GrowAndShrink" 
$Form.MinimizeBox = $false 
$Form.MaximizeBox = $false 
$Form.WindowState = "Normal" 
$Form.SizeGripStyle = "Hide" 
$Form.ShowInTaskbar = $false 
$Form.Opacity = 0.8 
$form.StartPosition = "CenterScreen" 
$form.BackColor = "black" 

#$Label = New-Object System.Windows.Forms.Label 
#$label.Location = New-Object Drawing.point 30,100 
#$Label.Text = "Click a button to copy its content" 
#$Label.AutoSize = $true 
#$Label.ForeColor = "White" 

$button = New-Object Windows.Forms.Button 
$button.text = "Server-One-Example" 
$button.ForeColor = "White" 
$button.AutoSize = $true 
$button.Location = New-Object Drawing.Point 65,10 

$copy1 = "Server-One-Example" | clip.exe 
$button.Add_Click($copy1) 

$button2 = New-Object Windows.Forms.Button 
$button2.text = "Server-Two-Example" 
$button2.ForeColor = "White" 
$button2.AutoSize = $true 
$button2.Location = New-Object Drawing.Point 65,40 

$copy2 = "Server-Two-Example" | clip.exe 
$button2.Add_Click($copy2) 

#$Form.controls.add($label) 
$form.controls.add($button) 
$form.controls.add($button2) 
$Form.ShowDialog() 
+0

如果你會做一點嘗試,你會發現,與代碼,因爲它是在這裏,點擊按鈕也絕對沒有什麼。把「asdf」放在你的剪貼板上,然後運行你的程序,不要點擊任何東西。現在從剪貼板粘貼到記事本中,您會看到它上面有「Server-Two-Example」。運行管道輸出到clip.exe的行在程序運行時運行 - 它們沒有「連線」到按鈕單擊事件。 –

回答

1

線條$copy1 = "Server-One-Example" | clip.exe和``$ COPY2 =「服務器兩實施例」」執行與所述數據發送到剪貼板時Powershell的執行腳本。因此,當程序運行時,它將「Server-One-Example」發送到剪貼板,然後在用戶甚至有機會單擊該按鈕之前將「Server-Two-Example」發送到剪貼板。

當按鈕被點擊時,您想要運行的代碼需要傳遞給Add_Click。你有它的方式$ copy1和$ copy2不會最終包含任何東西,所以點擊事件不會做任何事情。爲了證明這一點,請在PowerShell命令提示符處運行$copy1 = "asdf" | clip.exe。然後探索$ copy1變量,看看你是否能從中獲得任何東西。 $copy1.ToString()$copy1.GetType()返回錯誤。

Add-Type -AssemblyName System.Windows.Forms 
$Form = New-Object System.Windows.Forms.Form 
$Form.Text = "Click2Copy :)" 
$Font = New-Object System.Drawing.Font ("Roman", 11, [System.Drawing.FontStyle]::Regular) 
$Form.Font = $Font 
$Form.AutoSize = $false 
$Form.AutoSizeMode = "GrowAndShrink" 
$Form.MinimizeBox = $false 
$Form.MaximizeBox = $false 
$Form.WindowState = "Normal" 
$Form.SizeGripStyle = "Hide" 
$Form.ShowInTaskbar = $false 
$Form.Opacity = 0.8 
$form.StartPosition = "CenterScreen" 
$form.BackColor = "black" 

#$Label = New-Object System.Windows.Forms.Label 
#$label.Location = New-Object Drawing.point 30,100 
#$Label.Text = "Click a button to copy its content" 
#$Label.AutoSize = $true 
#$Label.ForeColor = "White" 

$button = New-Object Windows.Forms.Button 
$button.text = "Server-One-Example" 
$button.ForeColor = "White" 
$button.AutoSize = $true 
$button.Location = New-Object Drawing.Point 65,10 


$button.Add_Click({"Server-One-Example" | clip.exe}) 

$button2 = New-Object Windows.Forms.Button 
$button2.text = "Server-Two-Example" 
$button2.ForeColor = "White" 
$button2.AutoSize = $true 
$button2.Location = New-Object Drawing.Point 65,40 


$button2.Add_Click({"Server-Two-Example" | clip.exe}) 

#$Form.controls.add($label) 
$form.controls.add($button) 
$form.controls.add($button2) 
$Form.ShowDialog() 
+0

謝謝!所以如果我理解正確,你刪除了變量,改用set字符串並在{)之前添加{}?關於同一個腳本的一個簡單問題,當從一個批處理文件中調用它時,窗體窗體會打開,但是在後臺使用powershell窗口,是否有一種方法可以使窗體可見?我在蝙蝠或任何東西中都沒有-noexit。 –

+0

我剛剛刪除了(嘗試)設置'$ copy1'和'$ copy2'的行,並且更改了Add_Click方法以包含要運行的代碼。 –

+0

'powershell -WindowStyle隱藏-f c:\ temp \ test.ps1' –