PoshCode Archive  Artifact Content

Artifact 98844b5ef68d19765595ccc992f4408101856a61202c477a45e4d3e63dc79a3b:

  • File Set-Opacity.ps1 — part of check-in [66f8e18f55] at 2018-06-10 13:02:11 on branch trunk — Like Set-Blur, except it runs on Windows XP … you know … without the blur (user: unknown size: 4216)

# encoding: ascii
# api: csharp
# title: Set-Opacity
# description: Like Set-Blur, except it runs on Windows XP … you know … without the blur
# version: 0.1
# type: class
# license: CC0
# x-poshcode-id: 1865
# x-archived: 2010-05-24T16:40:58
#
#
   #.Synopsis
   # Mess with the transparency and blur of the PowerShell window
   #.Parameter color
   # The color to treat as transparent (you can set this to your foreground or background for best effect, but you must pass an actual [Drawing.Color]
   #.Parameter opacity
   # On a scale of 0 to 255, how opaque to make the window (255 is no transparency, 0 is invisible)
   #.Parameter Off
   # If set, turns off the color and opacity settings
   #.Parameter Handle
   # The handle of the window to act on. Defaults to the current PowerShell window
   #.Example 
   # Set-Opacity -Color ([System.Drawing.Color]::FromArgb( 0xff, 1, 36, 86 ))
   #
   # Makes the PowerShell Blue color completely transparent
   #.Example 
   # Set-Opacity -Opacity 200
   #
   # Sets the window partly transparent
   param([byte]$opacity, [System.Drawing.Color]$color, [switch]$Off, [IntPtr]$handle = (ps -Id $pid).MainWindowHandle)
   

Add-Type -Type @"
using System;
using System.Runtime.InteropServices;
namespace Huddled {
   public class Opacity {
   
      [DllImport("user32.dll")]
      public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
      
      [DllImport("user32.dll", SetLastError=true)]
      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);
   }
   
   [StructLayout(LayoutKind.Sequential)]
   public struct ColorRef
   {
      public uint ColorDWORD;

      public static implicit operator uint(ColorRef cr) {
         return cr.ColorDWORD;
      }
      public static implicit operator ColorRef(uint crKey) {
         ColorRef cr = new ColorRef();
         cr.ColorDWORD = crKey;
         return cr;
      }
      
      public static implicit operator System.Drawing.Color(ColorRef cr){
         return System.Drawing.Color.FromArgb((int)(0x000000FFU & cr.ColorDWORD), (int)(0x0000FF00U & cr.ColorDWORD) >> 8, (int)(0x00FF0000U & cr.ColorDWORD) >> 16);
      }
      
      public static implicit operator ColorRef(System.Drawing.Color color){
         ColorRef cr = new ColorRef();
         cr.ColorDWORD = (uint)color.R + (((uint)color.G) << 8) + (((uint)color.B) << 16);
         return cr;
      }
   }
}
"@ -Ref System.Drawing

function global:Set-Opacity {
   #.Synopsis
   # Mess with the transparency and blur of the PowerShell window
   #.Parameter color
   # The color to treat as transparent (you can set this to your foreground or background for best effect, but you must pass an actual [Drawing.Color]
   #.Parameter opacity
   # On a scale of 0 to 255, how opaque to make the window (255 is no transparency, 0 is invisible)
   #.Parameter Off
   # If set, turns off the color and opacity settings
   #.Parameter Handle
   # The handle of the window to act on. Defaults to the current PowerShell window
   #.Example 
   # Set-Opacity -Color ([System.Drawing.Color]::FromArgb( 0xff, 1, 36, 86 ))
   #
   # Makes the PowerShell Blue color completely transparent
   #.Example 
   # Set-Opacity -Opacity 200
   #
   # Sets the window partly transparent
   param([byte]$opacity, [System.Drawing.Color]$color, [switch]$Off, [IntPtr]$handle = (ps -Id $pid).MainWindowHandle)
   
   if($Off) { 
      $style = [Huddled.Opacity]::GetWindowLong($handle, -20) -bor 0x80000 -bxor 0x80000
   } else {
      $style = [Huddled.Opacity]::GetWindowLong($handle, -20) -bor 0x80000      
   }
   $style = [Huddled.Opacity]::SetWindowLong($Handle, -20, $style);

   $flag = 0
   if($color) { $flag += 1 } else { $color = [System.Drawing.Color]::Black }
   if($opacity) { $flag += 2 } else { $opacity = 255 }
   
   $null = [Huddled.Opacity]::SetLayeredWindowAttributes($handle, ([uInt32][Huddled.ColorRef]$color), $opacity, $flag)
}

Set-Opacity @PSBoundParameters