2016-10-10 91 views
1

Powerhell & .NET的新功能。是否可以通过命令行设置powershell opacity

这是可能的configure PowerShell控制台的颜色。

但是,我怎样才能设置窗口的不透明度。看来InternalHostRawUserInterface类的属性需要枚举ConsoleColor

是否可以设置窗口的透明度?

+1

是的。如果需要使用P/Invoke的一些自定义C#代码。你的问题是“我该怎么做?”? – Joey

回答

5

由于Joey mentioned in his comment,您必须与低级API通信以修改窗口的透明度。

使用this example,我们可以像这样它适应的PowerShell:

function Set-ConsoleOpacity 
{ 
    param(
     [ValidateRange(10,100)] 
     [int]$Opacity 
    ) 

    # Check if pinvoke type already exists, if not import the relevant functions 
    try { 
     $Win32Type = [Win32.WindowLayer] 
    } catch { 
     $Win32Type = Add-Type -MemberDefinition @' 
      [DllImport("user32.dll")] 
      public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong); 

      [DllImport("user32.dll")] 
      public static extern int GetWindowLong(IntPtr hWnd, int nIndex); 

      [DllImport("user32.dll")] 
      public static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey, byte bAlpha, uint dwFlags); 
'@ -Name WindowLayer -Namespace Win32 -PassThru 
    } 

    # Calculate opacity value (0-255) 
    $OpacityValue = [int]($Opacity * 2.56) - 1 

    # Grab the host windows handle 
    $ThisProcess = Get-Process -Id $PID 
    $WindowHandle = $ThisProcess.MainWindowHandle 

    # "Constants" 
    $GwlExStyle = -20; 
    $WsExLayered = 0x80000; 
    $LwaAlpha = 0x2; 

    if($Win32Type::GetWindowLong($WindowHandle,-20) -band $WsExLayered -ne $WsExLayered){ 
     # If Window isn't already marked "Layered", make it so 
     [void]$Win32Type::SetWindowLong($WindowHandle,$GwlExStyle,$Win32Type::GetWindowLong($WindowHandle,$GwlExStyle) -bxor $WsExLayered) 
    } 

    # Set transparency 
    [void]$Win32Type::SetLayeredWindowAttributes($WindowHandle,0,$OpacityValue,$LwaAlpha) 
} 

,然后用它喜欢:

Set-ConsoleOpacity -Opacity 50 

然后将窗口的不透明度设置为50%

相关问题