大佬教程收集整理的这篇文章主要介绍了Windows – 从WinForms到Powershell-Console,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
function Show-Process($Process,[Switch]$MaximizE) { $sig = ' [DllImport("user32.dll")] public static extern bool ShowWindowAsync(IntPtr hWnd,int nCmdShow); [DllImport("user32.dll")] public static extern int SetForegroundWindow(IntPtr hwnd); ' if ($MaximizE) { $Mode = 3 } else { $Mode = 4 } $type = Add-Type -MemberDeFinition $sig -Name WindowAPI -PassThru $hwnd = $process.MainWindowHandle $null = $type::ShowWindowAsync($hwnd,$ModE) $null = $type::SetForegroundWindow($hwnd) } Show-Process -Process (Get-Process -Id $pid)
它工作正常,但当我从Button Click事件调用该函数时,控制台不会显示.
问题是什么?有没有办法在使用WinForms GUI时将powersHell控制台置于前面?
以下是示例GUI代码:
function Show-Process($Process,$ModE) $null = $type::SetForegroundWindow($hwnd) } Add-Type -AssemblyName System.Windows.Forms [System.Windows.Forms.Application]::EnableVisualStyles() $Form = New-Object system.Windows.Forms.Form $Form.ClientSize = '446,266' $Form.text = "Form" $Form.TopMost = $false $Button1 = New-Object system.Windows.Forms.button $Button1.text = "button" $Button1.width = 60 $Button1.height = 30 $Button1.LOCATIOn = New-Object System.Drawing.Point(75,29) $Button1.Font = 'Microsoft Sans Serif,10' $Button1.Add_Click({ Show-Process -Process (Get-Process -Id $pid) }) $Form.controls.AddRange(@($Button1)) [void]$Form.ShowDialog()
$sig = ' [DllImport("user32.dll")] public static extern bool ShowWindowAsync(IntPtr hWnd,int nCmdShow); [DllImport("user32.dll")] public static extern int SetForegroundWindow(IntPtr hwnd);' $type = Add-Type -MemberDeFinition $sig -Name WindowAPI -PassThru [IntPtr]$handleConsole = (Get-Process -Id $pid).MainWindowHandle [void]$type::ShowWindowAsync($handleConsole,4);[void]$type::SetForegroundWindow($handleConsolE) Add-Type -AssemblyName System.Windows.Forms [System.Windows.Forms.Application]::EnableVisualStyles() $Form = New-Object system.Windows.Forms.Form $Form.ClientSize = '446,266' $Form.text = "Form" $Form.TopMost = $false $Form.Add_Shown({ $global:handleForm = (Get-Process -Id $pid).MainWindowHandle }) $Button1 = New-Object system.Windows.Forms.button $Button1.text = "Clone ad-USer" $Button1.width = 60 $Button1.height = 30 $Button1.LOCATIOn = New-Object System.Drawing.Point(75,10' $Button1.Add_Click({ [void]$type::ShowWindowAsync($handleConsole,4);[void]$type::SetForegroundWindow($handleConsolE) Read-Host -Prompt "Please Enter a Value" [void]$type::ShowWindowAsync($global:handleForm,4);[void]$type::SetForegroundWindow($global:handleForm) }) $Form.controls.AddRange(@($Button1)) [void]$Form.ShowDialog()
以上是大佬教程为你收集整理的Windows – 从WinForms到Powershell-Console全部内容,希望文章能够帮你解决Windows – 从WinForms到Powershell-Console所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。