<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Huddled Masses &#187; Aero Peek</title>
	<atom:link href="http://joelbennett.net/tag/aero-peek/feed/" rel="self" type="application/rss+xml" />
	<link>http://joelbennett.net</link>
	<description>The internet home of Joel "Jaykul" Bennett...</description>
	<lastBuildDate>Fri, 27 Apr 2012 05:42:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
<cloud domain='joelbennett.net' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
		<item>
		<title>Fun with PInvoke and Aero Peek</title>
		<link>http://joelbennett.net/fun-with-pinvoke-and-aero-peek/</link>
		<comments>http://joelbennett.net/fun-with-pinvoke-and-aero-peek/#comments</comments>
		<pubDate>Sun, 23 Aug 2009 05:07:16 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Aero Peek]]></category>
		<category><![CDATA[API]]></category>
		<category><![CDATA[DwmSetWindowAttribute]]></category>
		<category><![CDATA[DWMWA_EXCLUDED_FROM_PEEK]]></category>
		<category><![CDATA[PInvoke]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[Win32]]></category>
		<category><![CDATA[Windows 7]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1247</guid>
		<description><![CDATA[There are so many fun things you can do in Windows when your scripting language allows you to make PInvoke calls to Win32 APIs &#8230; but I have to say it&#8217;s amazing how many things have been added to Windows recently and still left out of the .Net framework &#8230; Anyway, on to the Aero [...]]]></description>
			<content:encoded><![CDATA[	<p>There are so many fun things you can do in Windows when your scripting language allows you to make PInvoke calls to Win32 APIs &#8230; but I have to say it&#8217;s amazing how many things have been added to Windows recently and still left out of the .Net framework &#8230;</p>

	<p>Anyway, on to the <a href="http://windows.microsoft.com/en-us/windows7/products/features/aero-peek">Aero Peek</a> stuff.  If you haven&#8217;t seen it, Aero Peek is a feature of Windows 7, which lets you get a peek at your desktop, or at a single window for a moment.  Basically, you can press Win+Space (the Windows logo key and the space bar) and all of your open windows instantly turn transparent, revealing &#8230; whatever was on your desktop: wallpaper, icons, and gadgets.   You can also use it by hovering your mouse on the right corner of the taskbar, or you can peek at a single window by hovering over it&#8217;s taskbar button and then over it&#8217;s thumbnail.</p>

	<p>In any case, I have a couple of windows which I would like to have stay visible on the desktop when I hit the aero peek hotkey: Rainlendar and Miranda.  It turns out there&#8217;s a simple <span class="caps">API</span> call for this: <code>DwmSetWindowAttribute</code> which lets you set the <code>DWMWA_EXCLUDED_FROM_PEEK</code> attribute to <code>ENABLED</code> &#8230; causing a window to no longer hide when you press that hotkey. Of course, that <span class="caps">API</span> call should be made by those apps, in response to a user setting (so I&#8217;ve told their authors about it), but it doesn&#8217;t <em>have</em> to be (so I wrote a script to do it myself).</p>

	<p>In the old days, I would have written a little systray app which would give you a popup list of all windows, or perhaps added a menu item to a window&#8217;s right-click menu &#8230; and I would have had to deal with creating some way to persist which apps you wanted to apply this to, and then I could have applied the setting to them whenever you opened them. </p>

	<p>But now, I have PowerShell. I don&#8217;t need to give you menus and store settings, because I can just let you edit a little script instead.</p>

	<p>So here&#8217;s a script which will let you turn off Aero Peek transparency for windows by window title and/or process name &#8230; Once you have this function available, you can keep Rainlendar&#8217;s calendar, tasks, and event windows all visible by just running <code>Remove-AeroPeek -Process Rainlendar2</code> or you can keep your <a class="zem_slink" href="http://www.miranda-im.org/" title="Miranda IM" rel="homepage">Miranda</a> contact list visible by running <code>Remove-AeroPeek &#34;Miranda IM&#34;</code> (although you should not that depends on the window title matching just that one window &#8212; and Miranda lets you change what your title is, so you may have to adjust it).</p>

<script type="text/javascript" src="http://PoshCode.org/embed/1288"></script>

	<p>Of course, that script really deserves explanation, because it&#8217;s showing off quite a few advanced things&#8230;</p>

	<p>The first thing is that I&#8217;m using a Try/Catch block in the <span class="caps">BEGIN</span> block to make sure I only execute that code once. You can&#8217;t call Add-Type with the same code multiple times in a single PowerShell session, because the type will already exist when you call it the second time.  So the code in the <code>try</code> block will throw an exception if the type doesn&#8217;t already exist, and in the catch handler, we&#8217;ll create the type, and define the other function we need.</p>

	<p>Add-Type is a super-powerful cmdlet which compiles code on the fly (or imports types from pre-compiled assemblies). In this case we&#8217;re using it to import a little class called Dwm which I started writing myself from PInvoke.net and the <span class="caps">MSDN</span> documentation, but then eventually copied most of from a NeoWin forum thread&#8230;  All this class really does is define the <span class="caps">API</span> function and the flags we need to pass to it, and then provides a wrapper for the DwmSetWindowAttribute call. We could have written that call in PowerShell, but at the end of the day, once you start compiling C# code in PowerShell, it&#8217;s hard to know when to stop  <img src='http://joelbennett.net/wordpress/wp-includes/' alt=';)' class='wp-smiley' /> </p>

	<p>The Select-Window function is (yet another customized version of) a function I wrote awhile back on PoshCode as part of my (still in progress) rewrite of <a href="http://WASP.CodePlex.com">WASP</a> to use the <a href="http://msdn.microsoft.com/en-us/library/aa348551.aspx#Testing_with_UI_Automation">UIAutomationClient</a> &#8230; I&#8217;ve just modified it to add only the three properties of the window that I&#8217;m interested in: Title and ProcessId (for identifying the correct windows) and Handle (for passing to the <code>DwmSetWindowAttribute</code> call).  It uses the <code>RootElement</code> property of <a href="http://msdn.microsoft.com/en-us/library/system.windows.automation.automationelement.aspx">System.Windows.Automation.AutomationElement</a> to do a search, and then a series of GetCurrentPropertyValue calls to determine the Name, ProcessId, and NativeWindowHandle of the windows it finds.</p>

	<p>That&#8217;s pretty much all there is to it, other than filtering out the window(s) that we want and actually calling the <span class="caps">API</span>. I think I&#8217;m going to have to play a little bit more with this to see what else we can do &#8212; I&#8217;ve already realized that this means we can make little widgets with PowerBoots and set them to stick around just like regular desktop gadgets &#8230;</p>

<div class="zemanta-pixie"><a class="zemanta-pixie-a" href="http://reblog.zemanta.com/zemified/9d449ee5-040d-4fc6-8cf0-9791b23f0898/" title="Reblog this post [with Zemanta]"><img class="zemanta-pixie-img" src="http://img.zemanta.com/reblog_e.png?x-id=9d449ee5-040d-4fc6-8cf0-9791b23f0898" alt="Reblog this post [with Zemanta]" /></a><span class="zem-script more-related pretty-attribution"><script type="text/javascript" src="http://static.zemanta.com/readside/loader.js" defer="defer"></script></span></div>]]></content:encoded>
			<wfw:commentRss>http://joelbennett.net/fun-with-pinvoke-and-aero-peek/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

