Last active
December 12, 2015 03:18
-
-
Save breezhang/4705369 to your computer and use it in GitHub Desktop.
hide console2 + powershell http://stackoverflow.com/questions/11942032/unhiding-a-powershell-window-thats-been-run-with-windowstyle-hidden
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Runtime.InteropServices; | |
namespace ConsoleApplication1 | |
{ | |
class Program | |
{ | |
[DllImport("kernel32.dll")] | |
static extern bool SetConsoleCtrlHandler(ConsoleCtrlDelegate HandlerRoutine, bool Add); | |
delegate Boolean ConsoleCtrlDelegate(CtrlTypes CtrlType); | |
enum CtrlTypes : uint | |
{ | |
CTRL_C_EVENT = 0, | |
CTRL_BREAK_EVENT, | |
CTRL_CLOSE_EVENT, | |
CTRL_LOGOFF_EVENT = 5, | |
CTRL_SHUTDOWN_EVENT | |
} | |
static void Main(string[] args) | |
{ | |
if (SetConsoleCtrlHandler(type => | |
{ | |
switch (type) | |
{ | |
case CtrlTypes.CTRL_C_EVENT: | |
Console.WriteLine("CTRL_C_EVENT"); | |
break; | |
case CtrlTypes.CTRL_BREAK_EVENT: | |
Console.WriteLine("CTRL+BREAK received!"); | |
break; | |
case CtrlTypes.CTRL_CLOSE_EVENT: | |
Console.WriteLine("Program being closed!"); | |
break; | |
case CtrlTypes.CTRL_LOGOFF_EVENT: | |
Console.WriteLine("User is logging off!"); | |
break; | |
case CtrlTypes.CTRL_SHUTDOWN_EVENT: | |
Console.WriteLine("User is logging off!"); | |
break; | |
} | |
return true; | |
}, true)) | |
{ | |
Console.WriteLine("Setup Ok!"); | |
while (true) | |
{ | |
if ((Console.ReadKey()).KeyChar == 'q') | |
{ | |
break; | |
} | |
} | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function hide-powershell { | |
param($shost ="console") | |
<# | |
try{ | |
[Foo.ConsoleUtils]::ShowWindow((ps $shost).MainWindowHandle,0) | |
} | |
catch [System.Management.Automation.RuntimeException] | |
{ | |
if ($_.FullyQualifiedErrorId -eq 'TypeNotFound'){ | |
$src = @' | |
[DllImport("Kernel32.dll")] | |
public static extern IntPtr GetConsoleWindow(); | |
[DllImport("User32.dll")] | |
public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow); | |
'@ | |
Add-Type -Name ConsoleUtils -Namespace Foo -MemberDefinition $src | |
[Foo.ConsoleUtils]::ShowWindow((ps $shost).MainWindowHandle,0) | |
} | |
} | |
catch{ | |
Write-Warning $_.Exception | |
} | |
#> | |
if (!("Foo.ConsoleUtils" -as [type])){ | |
$src = @' | |
[DllImport("Kernel32.dll")] | |
public static extern IntPtr GetConsoleWindow(); | |
[DllImport("User32.dll")] | |
public static extern bool ShowWindow(IntPtr hWnd, Int32 nCmdShow); | |
'@ | |
Add-Type -Name ConsoleUtils -Namespace Foo -MemberDefinition $src | |
} | |
if(@(ps console -ea 0)){ | |
[Foo.ConsoleUtils]::ShowWindow((ps $shost).MainWindowHandle,0) | |
}else{ | |
#$hWnd = [Foo.ConsoleUtils]::GetConsoleWindow() | |
#[Foo.ConsoleUtils]::ShowWindow($hWnd, 0) | |
} | |
} | |
#add your profile file | |
#activate console2 can use console2 hotkeys |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment