Windows – 从WinForms到Powershell-Console
发布时间:2020-12-14 01:50:24 所属栏目:Windows 来源:网络整理
导读:我试图将我的power shell控制台放在前面,即使它被最小化. 我找到了以下代码: function Show-Process($Process,[Switch]$Maximize){ $sig = ' [DllImport("user32.dll")] public static extern bool ShowWindowAsync(IntPtr hWnd,int nCmdShow); [DllImport(
我试图将我的power
shell控制台放在前面,即使它被最小化.
我找到了以下代码: 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事件调用该函数时,控制台不会显示. 以下是示例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()
感谢@ iRon的回答,我能够弄明白,我想要它.
对于任何好奇的人来说,问题是,只要没有调用ShowDialog,你就只能获得主窗口MainwindowHandle. 所以我将控制台Handle保存在一个变量中,我使用Form_Shown事件来获取Form WindowHandle,因为Form_Load仍然返回控制台句柄. $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-server-2008 – 默认域策略已损坏
- windows与linux中的mysql配置主从
- 08、组策略管理
- windows-server-2008-r2 – Windows RDC管理员用户可以免于
- 如何在Windows上使用Unix结尾的文件写入文件
- windows-server-2012 – Windows Server 2012 Branchcache与
- Microsoft Exchange Retention Policy
- 从.NET转向Win32开发
- Windows Server 2008 R2常规安全设置及基本安全策略
- windows-7 – 让Rspec自动测试在Windows上运行
推荐文章
站长推荐
- 如何使用selenium webdriver处理Windows文件浏览
- active-directory – (主要)Windows(AD)环境中的
- 【记录】解决windows中nginx命名退出了,为什么还
- 为什么Windows 10 Home上的计算机管理中缺少本地
- Windows Server 2016-管理站点复制(二)
- Windows应用商店应用 – XAML – C# – 在代码中
- windows – (MFC)如果控件是私有成员,父类如何接
- iis-7.5 – IIS 7.5:如何使用Windows身份验证配
- windows – 如何从命令行自动隐藏任务栏
- Windows – WinDbg中的“Break指令异常”是什么?
热点阅读