# encoding: ascii
# api: csharp
# title: Remove-AeroPeek
# description: Exclude a window from Aero Peek so it stays visible when you press Win+Space in Windows 7 (of course, there’s lots of other possibilities here, but I’ll let others explore them).  
# version: 0.1
# type: function
# author: Joel Bennett
# license: CC0
# function: Remove-AeroPeek
# x-poshcode-id: 1288
# x-archived: 2009-10-17T19:39:26
#
# I wrote this because Rainlendar doesn’t set it’s windows correctly to not hide when you use Aero Peek or Focus, and if you uncomment the last line, just running this script fixes that.
#
function Remove-AeroPeek {
PARAM(
   [string]$Title="*", 
   [string]$Process="*"
)
BEGIN {
try { 
   $null = [Huddled.DWM]
} catch { 
Add-Type -Type @"
using System;
using System.Runtime.InteropServices;

namespace Huddled {
   public static class Dwm {
      [DllImport("dwmapi.dll", PreserveSig = false)]
      public static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, ref int attrValue, int attrSize);

      [Flags]
      public enum DwmWindowAttribute
      {
         NCRenderingEnabled = 1,
         NCRenderingPolicy,
         TransitionsForceDisabled,
         AllowNCPaint,
         CaptionButtonBounds,
         NonClientRtlLayout,
         ForceIconicRepresentation,
         Flip3DPolicy,
         ExtendedFrameBounds,
         HasIconicBitmap,
         DisallowPeek,
         ExcludedFromPeek,
         Last
      }

      [Flags]
      public enum DwmNCRenderingPolicy
      {
         UseWindowStyle,
         Disabled,
         Enabled,
         Last
      }

      public static void RemoveFromAeroPeek(IntPtr Hwnd) //Hwnd is the handle to your window
      {
         int renderPolicy = (int)DwmNCRenderingPolicy.Enabled;
         DwmSetWindowAttribute(Hwnd, (int)DwmWindowAttribute.ExcludedFromPeek, ref renderPolicy, sizeof(int));
      }
   }
}
"@

[Reflection.Assembly]::Load("UIAutomationClient, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")
function global:Select-Window {
PARAM( [string]$Title="*", 
       [string]$Process="*", 
       [switch]$Recurse,
       [System.Windows.Automation.AutomationElement]$Parent = [System.Windows.Automation.AutomationElement]::RootElement ) 
PROCESS {
   $search = "Children"
   if($Recurse) { $search = "Descendants" }
   
   $Parent.FindAll( $search, [System.Windows.Automation.Condition]::TrueCondition ) | 
   Add-Member -Type ScriptProperty -Name Title     -Value {
               $this.GetCurrentPropertyValue([System.Windows.Automation.AutomationElement]::NameProperty)} -Passthru |
   Add-Member -Type ScriptProperty -Name Handle    -Value {
               $this.GetCurrentPropertyValue([System.Windows.Automation.AutomationElement]::NativeWindowHandleProperty)} -Passthru |
   Add-Member -Type ScriptProperty -Name ProcessId -Value {
               $this.GetCurrentPropertyValue([System.Windows.Automation.AutomationElement]::ProcessIdProperty)} -Passthru |

   Where-Object {
      ((Get-Process -Id $_.ProcessId).ProcessName -like $Process) -and ($_.Title -like $Title)
   }
}}
}
}
PROCESS {
   Foreach($window in Select-Window -Process $Process -Title $Title) { 
      [Huddled.Dwm]::RemoveFromAeroPeek( $window.Handle ) 
   }
}
}

# Remove-AeroPeek "Miranda IM"
# Remove-AeroPeek -Process Rainlendar2
