<?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; UserInterface</title>
	<atom:link href="http://huddledmasses.org/tag/userinterface/feed/" rel="self" type="application/rss+xml" />
	<link>http://huddledmasses.org</link>
	<description>You can do more than breathe for free...</description>
	<lastBuildDate>Sat, 28 Jan 2012 21:37:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<cloud domain='huddledmasses.org' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
		<item>
		<title>Disabling Events in ShowUI</title>
		<link>http://huddledmasses.org/disabling-events-in-showui/</link>
		<comments>http://huddledmasses.org/disabling-events-in-showui/#comments</comments>
		<pubDate>Mon, 27 Jun 2011 18:35:20 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[HowTo]]></category>
		<category><![CDATA[PowerBoots]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[ShowUI]]></category>
		<category><![CDATA[UserInterface]]></category>
		<category><![CDATA[WinForms]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1701</guid>
		<description><![CDATA[Because some things just work better in WPF. Let&#8217;s say you had a form that collected a bunch of user input, and then had a button that would fire off some work. We&#8217;ll assume that you wanted to prevent people from firing off the work again before they know the results of the first time, [...]]]></description>
			<content:encoded><![CDATA[	<h1>Because some things just work better in <span class="caps">WPF</span>.</h1>

	<p>Let&#8217;s say you had a form that collected a bunch of user input, and then had a button that would fire off some work.  We&#8217;ll assume that you wanted to prevent people from firing off the work again before they know the results of the first time, so you&#8217;ll disable the button while you&#8217;re working. Something like this (leaving out the boring stuff about collecting the user&#8217;s input and displaying the results):</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #0066cc; font-style: italic;">Import-<span style="font-style: normal;">Module</span></span> ShowUI<br />
<br />
StackPanel <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;TextBlock <span style="color: #000066;">-Name</span> Output<br />
&nbsp; &nbsp;Button <span style="color: #009900;">&quot;Click Me&quot;</span> <span style="color: #000066;">-Margin</span> <span style="color: #cc66cc;">3</span> <span style="color: #000066;">-On_Click</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$this</span>.<span style="color: #003366;">IsEnabled</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$False</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># Update the user about what we're doing:</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Output</span>.<span style="color: #003366;">Text</span> <span style="color: #66cc66;">+=</span> <span style="color: #009900;">&quot;We are doing some hard work...<span style="color: #000099; font-weight: bold;">`n</span>&quot;</span><br />
&nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># Simulate doing some hard work ...</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$times</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$times</span> <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">Start-<span style="font-style: normal;">Sleep</span></span> <span style="color: #000066;">-Seconds</span> <span style="color: #cc66cc;">3</span><br />
&nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># Let them know about all our hard work</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Output</span>.<span style="color: #003366;">Text</span> <span style="color: #66cc66;">+=</span> <span style="color: #009900;">&quot;Work completed $times time(s).<span style="color: #000099; font-weight: bold;">`n</span>&quot;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$this</span>.<span style="color: #003366;">IsEnabled</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$True</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span> <span style="color: #000066;">-Show</span> <br />
&nbsp;</div>

	<p>It&#8217;s pretty simple. In fact, I think there&#8217;s only three things worth pointing out:</p>

	<ol>
		<li>There are variables for named controls.</li>
		<li>If you want to make sure a control has time to update it&#8217;s display while you&#8217;re executing your event handler, call UpdateLayout().</li>
	</ol>
	<ol>
		<li>You can disable and enable buttons.</li>
	</ol>

	<h3>Now, try doing the same thing with Windows Forms.</h3>

	<p>This was <em>my</em> first attempt:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>void<span style="color: #333;">&#93;</span><span style="color: #333;">&#91;</span>reflection.<span style="color: #003366;">assembly</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">LoadWithPartialName</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;System.Windows.Forms&quot;</span><span style="color: #333;">&#41;</span><br />
<br />
<span style="color: #666666; font-style: italic;"># Create the form</span><br />
<span style="color: #660033; font-weight: bold;">$form</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">new-<span style="font-style: normal;">object</span></span> System.<span style="color: #003366;">Windows</span>.<span style="color: #003366;">Forms</span>.<span style="color: #003366;">Form</span><br />
<span style="color: #660033; font-weight: bold;">$form</span>.<span style="color: #003366;">Size</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;250,250&quot;</span><br />
<br />
<span style="color: #666666; font-style: italic;">## Create text</span><br />
<span style="color: #660033; font-weight: bold;">$output</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">new-<span style="font-style: normal;">object</span></span> system.<span style="color: #003366;">windows</span>.<span style="color: #003366;">forms</span>.<span style="color: #003366;">label</span><br />
<span style="color: #660033; font-weight: bold;">$output</span>.<span style="color: #003366;">Anchor</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Top,Left,Right&quot;</span><br />
<span style="color: #660033; font-weight: bold;">$output</span>.<span style="color: #003366;">AutoSize</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$True</span><br />
<br />
<span style="color: #666666; font-style: italic;">## Create button</span><br />
<span style="color: #660033; font-weight: bold;">$button</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">new-<span style="font-style: normal;">object</span></span> system.<span style="color: #003366;">windows</span>.<span style="color: #003366;">forms</span>.<span style="color: #003366;">button</span> <br />
<span style="color: #660033; font-weight: bold;">$button</span>.<span style="color: #003366;">Anchor</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Bottom,Right&quot;</span><br />
<span style="color: #660033; font-weight: bold;">$button</span>.<span style="color: #003366;">Text</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Click Me&quot;</span><br />
<span style="color: #660033; font-weight: bold;">$button</span>.<span style="color: #003366;">Location</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;150,180&quot;</span><br />
<br />
<span style="color: #660033; font-weight: bold;">$button</span>.<span style="color: #003366;">Add_Click</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$button</span>.<span style="color: #003366;">Enabled</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$False</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Output</span>.<span style="color: #003366;">Text</span> <span style="color: #66cc66;">+=</span> <span style="color: #009900;">&quot;We are doing some hard work...<span style="color: #000099; font-weight: bold;">`n</span>&quot;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">## Simulate doing work</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$times</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$times</span> <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span><br />
&nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">Start-<span style="font-style: normal;">Sleep</span></span> <span style="color: #000066;">-Seconds</span> <span style="color: #cc66cc;">5</span><br />
&nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># Let them know about all our hard work</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Output</span>.<span style="color: #003366;">Text</span> <span style="color: #66cc66;">+=</span> <span style="color: #009900;">&quot;Work completed $times time(s).<span style="color: #000099; font-weight: bold;">`n</span>&quot;</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$button</span>.<span style="color: #003366;">Enabled</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$True</span><br />
<span style="color: #333;">&#125;</span><span style="color: #333;">&#41;</span><br />
<br />
<span style="color: #660033; font-weight: bold;">$form</span>.<span style="color: #003366;">Controls</span>.<span style="color: #003366;">AddRange</span><span style="color: #333;">&#40;</span>@<span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$output</span>, <span style="color: #660033; font-weight: bold;">$button</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span><br />
<br />
<span style="color: #666666; font-style: italic;">## show the form</span><br />
<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>void<span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$form</span>.<span style="color: #003366;">showdialog</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
&nbsp;</div>

	<p>Now, I bet you noticed that took a bit more code than the <span class="caps">WPF</span> version in <a href="http://ShowUI.CodePlex.com">ShowUI</a>, but the thing about it is that I also had to manually specify the positions of the controls (and I had to set the size of the form to make that work). And after all that, it still doesn&#8217;t actually work, and it has a few ugly behaviors:</p>

	<p>First of all, disabling the control doesn&#8217;t work, because Windows posts all click events as messages to the button&#8217;s message queue, and it doesn&#8217;t know that it should ignore them, because it only processes one at a time &#8212; when it&#8217;s done running the Click handler the first time, it notices there&#8217;s another event &#8230; and since it&#8217;s enabled, it processes it.  If you click it five times while it&#8217;s disabled, you&#8217;re going to get the work done five more times&#8230; eventually.</p>

	<p>On top of that, although the label updates without being told to, it didn&#8217;t push the button down the screen or resize the form, so eventually it overlaps the button, passes through the bottom of the form and disappears.</p>

	<p>The next thing I tried was to actually unhook the event handler before I disable the button &#8230; but that had no effect either, because (as I wrote earlier) the button doesn&#8217;t actually ignore the clicks while it&#8217;s doing the work &#8212; it just queues them up for processing later.</p>

	<p>Of course, I could do the work in a background job so that the button would return immediately, but that doesn&#8217;t meet the requirements I have for not queuing up extra work before the first work is done, and in fact, then I&#8217;d have extra work to do to create a time to check and see if the remote job was finished or not.</p>

	<p>[New] Thanks to <a href="http://huddledmasses.org/disabling-events-in-showui/comment-page-1/#comment-215538">SAPIENDavid</a>, I&#8217;ve realized the simple fix for the disabling problem, we just have to call <code>DoEvents</code> to empty the event queue <em>before</em> we re-enable the button.</p>

	<p>Here&#8217;s what <strong>did</strong> work:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #0066cc; font-style: italic;">Add-<span style="font-style: normal;">Type</span></span> <span style="color: #000066;">-Assembly</span> System.<span style="color: #003366;">Windows</span>.<span style="color: #003366;">Forms</span><br />
<br />
<span style="color: #666666; font-style: italic;"># Create the form</span><br />
<span style="color: #660033; font-weight: bold;">$form</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Object</span></span> System.<span style="color: #003366;">Windows</span>.<span style="color: #003366;">Forms</span>.<span style="color: #003366;">Form</span> <span style="color: #000066;">-Property</span> @<span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; Size <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;200,70&quot;</span><br />
&nbsp; &nbsp; AutoSize <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$true</span> <br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;"># Create a FlowLayout</span><br />
<span style="color: #660033; font-weight: bold;">$panel</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Object</span></span> System.<span style="color: #003366;">Windows</span>.<span style="color: #003366;">Forms</span>.<span style="color: #003366;">FlowLayoutPanel</span> <span style="color: #000066;">-Property</span> @<span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; Dock <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Fill&quot;</span><br />
&nbsp; &nbsp; FlowDirection <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;TopDown&quot;</span><br />
&nbsp; &nbsp; AutoSize <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$true</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;">## Create text</span><br />
<span style="color: #660033; font-weight: bold;">$output</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Object</span></span> system.<span style="color: #003366;">windows</span>.<span style="color: #003366;">forms</span>.<span style="color: #003366;">label</span> <span style="color: #000066;">-Property</span> @<span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; Dock <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Fill&quot;</span><br />
&nbsp; &nbsp; Text <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Click the button when you're ready to work.<span style="color: #000099; font-weight: bold;">`n</span>&quot;</span><br />
&nbsp; &nbsp; AutoSize <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$true</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;">## Create button</span><br />
<span style="color: #660033; font-weight: bold;">$button</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Object</span></span> system.<span style="color: #003366;">windows</span>.<span style="color: #003366;">forms</span>.<span style="color: #003366;">button</span> <span style="color: #000066;">-Property</span> @<span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; Anchor <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Bottom,Right&quot;</span><br />
&nbsp; &nbsp; Text <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Click Me&quot;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #660033; font-weight: bold;">$lastButtonClick</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">get-<span style="font-style: normal;">date</span></span><br />
<span style="color: #660033; font-weight: bold;">$button</span>.<span style="color: #003366;">Add_Click</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$button</span>.<span style="color: #003366;">Enabled</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$False</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Output</span>.<span style="color: #003366;">Text</span> <span style="color: #66cc66;">+=</span> <span style="color: #009900;">&quot;We are doing some hard work...<span style="color: #000099; font-weight: bold;">`n</span>&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">## Simulate doing work</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$times</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$times</span> <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span><br />
&nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">Start-<span style="font-style: normal;">Sleep</span></span> <span style="color: #000066;">-Seconds</span> <span style="color: #cc66cc;">5</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># Let them know about all our hard work</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Output</span>.<span style="color: #003366;">Text</span> <span style="color: #66cc66;">+=</span> <span style="color: #009900;">&quot;Work completed $times time(s).<span style="color: #000099; font-weight: bold;">`n</span>&quot;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">#Process the pending messages before enabling the button</span><br />
&nbsp; &nbsp; <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>System.<span style="color: #003366;">Windows</span>.<span style="color: #003366;">Forms</span>.<span style="color: #003366;">Application</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">DoEvents</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$button</span>.<span style="color: #003366;">Enabled</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$True</span><br />
<span style="color: #333;">&#125;</span><span style="color: #333;">&#41;</span><br />
<br />
<span style="color: #660033; font-weight: bold;">$panel</span>.<span style="color: #003366;">Controls</span>.<span style="color: #003366;">AddRange</span><span style="color: #333;">&#40;</span>@<span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$output</span>, <span style="color: #660033; font-weight: bold;">$button</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span><br />
<span style="color: #660033; font-weight: bold;">$form</span>.<span style="color: #003366;">Controls</span>.<span style="color: #003366;">AddRange</span><span style="color: #333;">&#40;</span>@<span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$panel</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span><br />
<br />
<span style="color: #666666; font-style: italic;">## show the form</span><br />
<span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>void<span style="color: #333;">&#93;</span></span><span style="color: #660033; font-weight: bold;">$form</span>.<span style="color: #003366;">showdialog</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
&nbsp;</div>

	<p>To fix the other problems, I added a FlowLayoutPanel (which is very different from the StackPanel in <span class="caps">WPF</span>, but still serves the same purpose), and made all the relevant bits autosize (it&#8217;s always surprising to me when I need to drop back to WinForms, how everything has to be <strong>told</strong> to autosize and fill empty space). </p>

	<p>That&#8217;s enough to take care of the output problem, and make the two solutions roughly equivalent (there&#8217;s still some differences, as you can see if you run them).</p>

	<p>For what it&#8217;s worth, this was a real world question from a user at our <a href="http://PowerShellGroup.org/virtual/live">Virtual User Group</a> this morning, and I just couldn&#8217;t help sharing how much easier user interfaces are to write in ShowUI. Clearly a designer like PrimalForms makes laying out the controls easier &#8212; but when it comes to the little things, you still have to figure out to make them work, and actually implement your event handlers correctly.</p>

	<p>Just for the record, here&#8217;s what it would take to implement the <span class="caps">WPF</span> solution without ShowUI:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #0066cc; font-style: italic;">Add-<span style="font-style: normal;">Type</span></span> <span style="color: #000066;">-Assembly</span> PresentationFramework<br />
<span style="color: #660033; font-weight: bold;">$window</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Object</span></span> System.<span style="color: #003366;">Windows</span>.<span style="color: #003366;">Window</span> <span style="color: #000066;">-Property</span> @<span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; SizeToContent <span style="color: #66cc66;">=</span> <span style="color: #009900;">'WidthAndHeight'</span><br />
&nbsp; &nbsp; Content <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Object</span></span> System.<span style="color: #003366;">Windows</span>.<span style="color: #003366;">Controls</span>.<span style="color: #003366;">StackPanel</span><br />
<span style="color: #333;">&#125;</span><br />
<span style="color: #660033; font-weight: bold;">$output</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Object</span></span> System.<span style="color: #003366;">Windows</span>.<span style="color: #003366;">Controls</span>.<span style="color: #003366;">TextBlock</span><br />
<span style="color: #660033; font-weight: bold;">$button</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Object</span></span> System.<span style="color: #003366;">Windows</span>.<span style="color: #003366;">Controls</span>.<span style="color: #003366;">Button</span> <span style="color: #000066;">-Property</span> @<span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; Content <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Click Me&quot;</span><br />
&nbsp; &nbsp; Margin <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">3</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #660033; font-weight: bold;">$button</span>.<span style="color: #003366;">Add_Click</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$this</span>.<span style="color: #003366;">IsEnabled</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$False</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># Update the user about what we're doing:</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Output</span>.<span style="color: #003366;">Text</span> <span style="color: #66cc66;">+=</span> <span style="color: #009900;">&quot;We are doing some hard work...<span style="color: #000099; font-weight: bold;">`n</span>&quot;</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Window</span>.<span style="color: #003366;">UpdateLayout</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># Simulate doing some hard work ...</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$times</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$times</span> <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span><br />
&nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">Start-<span style="font-style: normal;">Sleep</span></span> <span style="color: #000066;">-Seconds</span> <span style="color: #cc66cc;">3</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># Let them know about all our hard work</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Output</span>.<span style="color: #003366;">Text</span> <span style="color: #66cc66;">+=</span> <span style="color: #009900;">&quot;Work completed $times time(s).<span style="color: #000099; font-weight: bold;">`n</span>&quot;</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$this</span>.<span style="color: #003366;">IsEnabled</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$True</span><br />
<span style="color: #333;">&#125;</span><span style="color: #333;">&#41;</span><br />
<br />
<span style="color: #660033; font-weight: bold;">$window</span>.<span style="color: #003366;">Content</span>.<span style="color: #003366;">Children</span>.<span style="color: #003366;">Add</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$output</span><span style="color: #333;">&#41;</span><br />
<span style="color: #660033; font-weight: bold;">$window</span>.<span style="color: #003366;">Content</span>.<span style="color: #003366;">Children</span>.<span style="color: #003366;">Add</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$button</span><span style="color: #333;">&#41;</span><br />
<span style="color: #660033; font-weight: bold;">$window</span>.<span style="color: #003366;">ShowDialog</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
&nbsp;</div>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/disabling-events-in-showui/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>ShowUI: Handling Events and Producing Output</title>
		<link>http://huddledmasses.org/showui-handling-events-and-producing-output/</link>
		<comments>http://huddledmasses.org/showui-handling-events-and-producing-output/#comments</comments>
		<pubDate>Mon, 20 Jun 2011 05:22:10 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[HowTo]]></category>
		<category><![CDATA[PowerBoots]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[ShowUI]]></category>
		<category><![CDATA[UserInterface]]></category>
		<category><![CDATA[WinForms]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1694</guid>
		<description><![CDATA[Once you get past the basics of WPF and ShowUI, learning to use nested panels or grids to achieve the layouts you want, and start getting a grip on what controls are available by default, the next step to building useful user interfaces is going to be handling user interactions. In programming, we call those [...]]]></description>
			<content:encoded><![CDATA[	<p>Once you get past the basics of <span class="caps">WPF</span> and <a href="http://showui.codeplex.com/">ShowUI</a>, learning to use nested panels or grids to achieve the layouts you want, and start getting a grip on what controls are available by default, the next step to building useful user interfaces is going to be handling user interactions.</p>

	<p>In programming, we call those interactions &#8220;events.&#8221; An event in <span class="caps">WPF</span> covers all forms of user interactions like clicking a button, type in a textbox, or select items from a treeview or listbox, and even less obvious things like when the right or left mouse button get&#8217;s pressed down, or let up &#8230; or when the mouse moves, of when the control gets focus or looses it, or &#8220;touch&#8221; and &#8220;stylus&#8221; events, drag-and-drop events, etc.  There are also events that aren&#8217;t caused by users, like events for when databinding is updated, when the control is initialized, hidden, made visible, etc.</p>

	<p>In ShowUI, all events are handled by assigning scriptblocks to a parameter who&#8217;s name starts with &#8220;On_&#8221; like -On_Click or -On_GotFocus or -On_MouseLeftButtonDown or -On_TextInput &#8230; and so on.</p>

	<p>Let&#8217;s say that you want a quick dialog like this:</p>

	<p><img src="http://huddledmasses.org/images/ShowUI/ShowUI-10.png" class="float-right-block" alt="" width="225" height="118" /></p>

	<p>You&#8217;re going to need to handle the OK button click, of course, but in that scriptblock, you&#8217;re going to want to get the text from the textbox, and make sure that it gets returned when the window is closed &#8230; and you&#8217;re going to want to close the Window! </p>

	<p>We&#8217;re here to help! Within the event handler script blocks, ShowUI defines a bunch of variables for you to help you handle the event: <code>$this</code> is the source of the event, <code>$_</code> is the event arguments, and <code>$window</code> is the top-level window that contains your UI. Any named controls in your script are also exposed as variables, so if you started with this script:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
StackPanel <span style="color: #000066;">-ControlName</span> <span style="color: #009900;">&quot;Prompt&quot;</span> <span style="color: #000066;">-Margin</span> <span style="color: #009900;">&quot;8,0,8,8&quot;</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; Label <span style="color: #009900;">&quot;Please Enter Your Full Name:&quot;</span><br />
&nbsp; &nbsp; StackPanel <span style="color: #000066;">-Orientation</span> Horizontal <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; TextBox <span style="color: #000066;">-Name</span> FullName <span style="color: #000066;">-Width</span> <span style="color: #cc66cc;">100</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Button <span style="color: #009900;">&quot;OK&quot;</span> <span style="color: #000066;">-IsDefault</span> <span style="color: #000066;">-Width</span> <span style="color: #cc66cc;">50</span> <span style="color: #000066;">-Margin</span> <span style="color: #009900;">&quot;8,0,0,0&quot;</span> <span style="color: #000066;">-On_Click</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># Do something to output the name!</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span> <span style="color: #000066;">-Show</span><br />
&nbsp;</div>

	<p>You&#8217;re going to be able to get the text from the TextBox using <code>$FullName.Text</code> because you know the TextBox has a property named &#8220;Text&#8221; (since it&#8217;s exposed as a parameter for you in the TextBox command), and because now you know that ShowUI creates a variable for all the named controls.</p>

	<p>In order to write output from a script like that one, you have to set the <code>Tag</code> property on the top level control (in this case, the StackPanel, which is obviously named &#8220;Prompt&#8221;). You can do that easily by hand, or you can use the Set-UIValue function.</p>

	<p>In order to close the window, you&#8217;re going to have to do one of two things: first, you can use the handy <code>Close-Control</code> function, or you can call the Close method on the window. The Close-Control function will look at the &#8220;parent&#8221; (and it&#8217;s parent) to try and find the window that needs to be closed &#8212; but if it can&#8217;t find one, it will just <strong>hide</strong> the parent, so if your button were several layers deep (unlike ours), you&#8217;d have to specify the top level control as a parameter.</p>

	<p>Here&#8217;s two versions of what it could have looked like when I was finished:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
StackPanel <span style="color: #000066;">-ControlName</span> <span style="color: #009900;">&quot;Prompt&quot;</span> <span style="color: #000066;">-Margin</span> <span style="color: #009900;">&quot;8,0,8,8&quot;</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; Label <span style="color: #009900;">&quot;Please Enter Your Full Name:&quot;</span><br />
&nbsp; &nbsp; StackPanel <span style="color: #000066;">-Orientation</span> Horizontal <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; TextBox <span style="color: #000066;">-Name</span> FullName <span style="color: #000066;">-Width</span> <span style="color: #cc66cc;">100</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Button <span style="color: #009900;">&quot;OK&quot;</span> <span style="color: #000066;">-IsDefault</span> <span style="color: #000066;">-Width</span> <span style="color: #cc66cc;">50</span> <span style="color: #000066;">-Margin</span> <span style="color: #009900;">&quot;8,0,0,0&quot;</span> <span style="color: #000066;">-On_Click</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Prompt</span>.<span style="color: #003366;">Tag</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$FullName</span>.<span style="color: #003366;">Text</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Window</span>.<span style="color: #003366;">Close</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span> <span style="color: #000066;">-On_Loaded</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$FullName</span>.<span style="color: #003366;">Focus</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#125;</span> <span style="color: #000066;">-Show</span><br />
&nbsp;</div>

	<div class="posh code posh" style="font-family:monospace;"><br />
StackPanel <span style="color: #000066;">-ControlName</span> <span style="color: #009900;">&quot;Prompt&quot;</span> <span style="color: #000066;">-Margin</span> <span style="color: #009900;">&quot;8,0,8,8&quot;</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; Label <span style="color: #009900;">&quot;Please Enter Your Full Name:&quot;</span><br />
&nbsp; &nbsp; StackPanel <span style="color: #000066;">-Orientation</span> Horizontal <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; TextBox <span style="color: #000066;">-Name</span> FullName <span style="color: #000066;">-Width</span> <span style="color: #cc66cc;">100</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Button <span style="color: #009900;">&quot;OK&quot;</span> <span style="color: #000066;">-IsDefault</span> <span style="color: #000066;">-Width</span> <span style="color: #cc66cc;">50</span> <span style="color: #000066;">-Margin</span> <span style="color: #009900;">&quot;8,0,0,0&quot;</span> <span style="color: #000066;">-On_Click</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">Set-<span style="font-style: normal;">UIValue</span></span> <span style="color: #660033; font-weight: bold;">$Prompt</span> <span style="color: #000066;">-Passthru</span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Close-<span style="font-style: normal;">Control</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span> <span style="color: #000066;">-On_Loaded</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$FullName</span>.<span style="color: #003366;">Focus</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#125;</span> <span style="color: #000066;">-Show</span><br />
&nbsp;</div>

	<p>One thing you&#8217;ll notice right away is that I cheated and actually added another event handler too: For the &#8220;Loaded&#8221; event on the StackPanel.  This event handler is called during the initialization of the user interface, and gives you a chance to do things like what I did here: set the initial keyboard focus where you want it (so the user can start typing as soon as the window pops up).</p>

	<p>However, if you run them both, you&#8217;ll notice another thing: the output is different.  In the second example I took advantage of the fact that Set-UIValue will actually call Get-UIValue if you don&#8217;t pass it a parameter!  The cool thing about Get-UIValue is that if it doesn&#8217;t find a &#8220;Tag&#8221; on the specified control, it will look through the children to find one, and create a hashtable out of the values it finds. So in this case, rather than write the code to get the value from the right textbox and set it myself, I just let the built-in features of ShowUI do their thing.</p>

	<h3>A bigger example</h3>

	<p>Of course, in neither example did I need to do anything with the button or with the actual parameters that are passed in to the button&#8217;s &#8220;Click&#8221; event &#8230; so perhaps one last (more complicated) example would be useful:</p>

	<p><img src="http://huddledmasses.org/images/ShowUI/ShowUI-11.png" class="float-right-block" alt="" width="185" height="167" /></p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Grid</span></span> <span style="color: #000066;">-ControlName</span> SelectUserGroups <span style="color: #000066;">-Columns</span> Auto,<span style="color: #66cc66;">*</span> <span style="color: #000066;">-Rows</span> <span style="color: #cc66cc;">4</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$GetGroups</span> <span style="color: #66cc66;">=</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$user</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">QADUuser</span></span> <span style="color: #660033; font-weight: bold;">$this</span>.<span style="color: #003366;">Text</span> <span style="color: #000066;">-SizeLimit</span> <span style="color: #cc66cc;">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$User</span>.<span style="color: #003366;">LogonName</span> <span style="color: #000066;">-eq</span> <span style="color: #660033; font-weight: bold;">$this</span>.<span style="color: #003366;">Text</span> <span style="color: #000066;">-or</span> <span style="color: #660033; font-weight: bold;">$User</span>.<span style="color: #003366;">Email</span> <span style="color: #000066;">-eq</span> <span style="color: #660033; font-weight: bold;">$this</span>.<span style="color: #003366;">Text</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$this</span>.<span style="color: #003366;">Foreground</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Black&quot;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Group</span>.<span style="color: #003366;">ItemsSource</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">QADGroup</span></span> <span style="color: #000066;">-ContainsMember</span> <span style="color: #660033; font-weight: bold;">$user</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$UserName</span>.<span style="color: #003366;">Text</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$user</span>.<span style="color: #003366;">LogonName</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$EmailAddress</span>.<span style="color: #003366;">Text</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$user</span>.<span style="color: #003366;">Email</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span> <span style="color: #666699; font-weight: bold;">else</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$this</span>.<span style="color: #003366;">Foreground</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Red&quot;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Group</span>.<span style="color: #003366;">ItemsSource</span> <span style="color: #66cc66;">=</span> @<span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Label</span></span> <span style="color: #009900;">&quot;Name&quot;</span><br />
&nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Textbox</span></span> <span style="color: #000066;">-name</span> UserName <span style="color: #000066;">-minwidth</span> <span style="color: #cc66cc;">100</span> <span style="color: #000066;">-Column</span> <span style="color: #cc66cc;">1</span> <span style="color: #000066;">-On_LostFocus</span> <span style="color: #660033; font-weight: bold;">$GetGroups</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Label</span></span> <span style="color: #009900;">&quot;Email&quot;</span> <span style="color: #000066;">-Row</span> <span style="color: #cc66cc;">1</span><br />
&nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Textbox</span></span> <span style="color: #000066;">-name</span> EmailAddress <span style="color: #000066;">-minwidth</span> <span style="color: #cc66cc;">100</span> &nbsp;<span style="color: #000066;">-Column</span> <span style="color: #cc66cc;">1</span> <span style="color: #000066;">-Row</span> <span style="color: #cc66cc;">1</span> &nbsp;<span style="color: #000066;">-On_LostFocus</span> <span style="color: #660033; font-weight: bold;">$GetGroups</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Label</span></span> <span style="color: #009900;">&quot;Group&quot;</span> <span style="color: #000066;">-Row</span> <span style="color: #cc66cc;">2</span><br />
&nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Listbox</span></span> <span style="color: #000066;">-name</span> <span style="color: #660033;">Group</span> <span style="color: #000066;">-Column</span> <span style="color: #cc66cc;">1</span> <span style="color: #000066;">-Row</span> <span style="color: #cc66cc;">2</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Button</span></span> <span style="color: #009900;">&quot;OK&quot;</span> <span style="color: #000066;">-Row</span> <span style="color: #cc66cc;">3</span> <span style="color: #000066;">-Column</span> <span style="color: #cc66cc;">1</span> <span style="color: #000066;">-On_Click</span> <span style="color: #333;">&#123;</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">ParentControl</span></span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Set-<span style="font-style: normal;">UIValue</span></span> <span style="color: #000066;">-Passthru</span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Close-<span style="font-style: normal;">Control</span></span> <span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span> <span style="color: #000066;">-Show</span><br />
&nbsp;</div>

	<p>Hopefully, by now this doesn&#8217;t need a whole lot of explanation, but let&#8217;s walk through it anyway.  First of all, if you&#8217;re not familiar with them, this script uses the excellent <a href="http://www.quest.com/powershell/activeroles-server.aspx">PowerShell Commands for Active Directory</a> from Quest software.  This doesn&#8217;t represent a full, complete, useful user interface &#8212; it&#8217;s more of an example that you can hopefully work from (and please, contribute ammendments <a href="http://poshcode.org/2737">on PoshCode</a>).</p>

	<p>You can see that I used a Grid with four rows and two Columns: with one column &#8220;Auto&#8220;sized, and one using up all the rest of the UI (you can resize the window to make the fields bigger).  The first thing in the grid is the definition of a scripblock which I assigned to a variable <code>$GetGroups</code>.  The reason I did that is because I wanted to reuse the scriptblock as the event handler for both the UserName and EmailAddress fields.</p>

	<p>Without dwelling a whole lot on it, you can see the last line is the &#8220;OK&#8221; button which get&#8217;s the parent grid and calls Set-UIValue to invoke, as before, the hashtable collection of all the textbox values.</p>

	<p>The interesting stuff is in that GetGroup event handler:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
&nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$user</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">QADUuser</span></span> <span style="color: #660033; font-weight: bold;">$this</span>.<span style="color: #003366;">Text</span> <span style="color: #000066;">-SizeLimit</span> <span style="color: #cc66cc;">1</span><br />
&nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$User</span>.<span style="color: #003366;">LogonName</span> <span style="color: #000066;">-eq</span> <span style="color: #660033; font-weight: bold;">$this</span>.<span style="color: #003366;">Text</span> <span style="color: #000066;">-or</span> <span style="color: #660033; font-weight: bold;">$User</span>.<span style="color: #003366;">Email</span> <span style="color: #000066;">-eq</span> <span style="color: #660033; font-weight: bold;">$this</span>.<span style="color: #003366;">Text</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$this</span>.<span style="color: #003366;">Foreground</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Black&quot;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Group</span>.<span style="color: #003366;">ItemsSource</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">QADGroup</span></span> <span style="color: #000066;">-ContainsMember</span> <span style="color: #660033; font-weight: bold;">$user</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$UserName</span>.<span style="color: #003366;">Text</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$user</span>.<span style="color: #003366;">LogonName</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$EmailAddress</span>.<span style="color: #003366;">Text</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$user</span>.<span style="color: #003366;">Email</span><br />
&nbsp; &nbsp; <span style="color: #333;">&#125;</span> <span style="color: #666699; font-weight: bold;">else</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$this</span>.<span style="color: #003366;">Foreground</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;Red&quot;</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Group</span>.<span style="color: #003366;">ItemsSource</span> <span style="color: #66cc66;">=</span> @<span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<p>You can see that first we call <code>Get-QADUser</code> with the Text of <code>$this</code> field.  By using <code>$this</code>, we make it so the event handler will work on both Textboxes, since it will get the text of whatever triggered the event handler.  Get-QADUser doesn&#8217;t return anything unless it finds a user, and setting the SizeLimit ensures that we won&#8217;t end up waiting for it to retrieve &#8220;all&#8221; the users just because the Textbox was left empty.  In fact, the point of that line is to make sure that we matched a user.</p>

	<p>On the next line, I&#8217;m making sure that either the LogonName or Email of the user that we found matches fully the text that the user typed. This makes sure that the user that we matched is a full match, so we know we&#8217;ve gotten the person typing into the form to type a full, complete username or email address.</p>

	<p>When it does match, we set the color to Black (in case it was an error and set to Red before), and we call Get-QADGroup to get all the groups that the user is a member of.  We set those groups as the source for the <code>$Group</code> listbox, and they&#8217;ll immediately show up for the user.  And finally, we update the few fields we&#8217;re showing from the user object we retrieved earlier. </p>

	<p>Of course, when it doesn&#8217;t match, we set the text red to indicate an error, and then we zero out the data on the group listbox.</p>

	<p>I hope this has helped some of you figure out event handlers in ShowUI &#8212; please feel free to ask questions in the comments below or on the <a href="http://showui.codeplex.com/discussions">ShowUI discussion boards</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/showui-handling-events-and-producing-output/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>ShowUI: the tutorial walkthrough</title>
		<link>http://huddledmasses.org/showui-tutorial-walkthrough/</link>
		<comments>http://huddledmasses.org/showui-tutorial-walkthrough/#comments</comments>
		<pubDate>Wed, 15 Jun 2011 04:44:07 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[PowerBoots]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[ShowUI]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[UserInterface]]></category>
		<category><![CDATA[WalkThrough]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1679</guid>
		<description><![CDATA[Ok, let&#8217;s be clear: you&#8217;ve seen this before, and this isn&#8217;t going to be the last you&#8217;ll see of it, because we&#8217;ve got much more in store in ShowUI, but for the first release of ShowUI, it&#8217;s obviously time to update this simple walkthrough of building simple user interfaces in PowerShell! An introduction to ShowUI [...]]]></description>
			<content:encoded><![CDATA[	<p>Ok, let&#8217;s be clear: you&#8217;ve seen this <a href="/powerboots-tutorial-walkthrough">before</a>, and this isn&#8217;t going to be the last you&#8217;ll see of it, because we&#8217;ve got much more in store in <a href="http://showui.codeplex.com">ShowUI</a>, but for the first release of ShowUI, it&#8217;s obviously time to update this simple walkthrough of building simple user interfaces in PowerShell!</p>

	<h2>An introduction to ShowUI</h2>

	<p>ShowUI is the next generation PowerShell module for building user interfaces in script. It&#8217;s the merger of my previous PowerShell UI project (called PowerBoots) with one by James Brundage, former Microsoft PowerShell team member and founder of <a href="http://start-automating.com">Start-Automating</a> (called <span class="caps">WPK</span>) which shipped in the Windows 7 resource kit.</p>

	<h4>If you want to follow along, you need to:</h4>

	<p>1. Get a copy of the <a href="http://showui.codeplex.com/releases/">ShowUI Module</a> from CodePlex <br />
2. Install it by putting the &#8220;ShowUI&#8221; folder in one of your &#8220;Modules&#8221; folders (list them by typing <code>$Env:PSMODULEPATH</code> in PowerShell v2, and feel free to create the one you want if it doesn&#8217;t exist). You should end up with something like <code>C:\Users\Jaykul\Documents\WindowsPowerShell\Modules\ShowUI\ShowUI.psm1</code><br />
3. Run PowerShell <span class="caps">ISE</span>, or use PowerShell.exe with the -<span class="caps">STA</span> switch (the best way to do this is to add it to the shortcut you use to launch PowerShell: open the properties dialog, and on the Shortcut tab, add &#8220; -STA&#8221; to the Target)<sup class="footnote"><a href="#fn1">1</a></sup>.</p>

	<p>Did I hear someone ask <strong>what <em>is</em> WPF?</strong> It was introduced as part of .Net 3.0 (and vastly improved in .Net 3.5, and again in 4.0), so you can expect to find it preinstalled on computers from Vista on, and of course you can download and install it on XP if it&#8217;s not already installed.  The only thing you really need to know about <span class="caps">WPF</span> for the purposes of this tutorial is that it is <strong>the</strong> new <span class="caps">GUI</span> toolkit for .Net, and that it is container based &#8212; you put elements into other elements to control the layout, rather like <span class="caps">HTML</span> and Java Swing&#8230; you can <strong>pick up the rest as we go along</strong>.</p>

	<h2>A simple ShowUI program</h2>

	<p><img src="http://huddledmasses.org/images/ShowUI/ShowUI-01.png" class="float-right-block" alt="" width="167" height="85" /></p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Button</span></span> <span style="color: #000066;">-Content</span> <span style="color: #009900;">&quot;Hello World&quot;</span> <span style="color: #000066;">-Show</span><br />
&nbsp;</div>

	<p>This first example is a little bit more verbose than it needs to be, because the <code>-Content</code> parameter is positional, so the first non-named argument you pass will be used for that. The same is true for the -Children parameter of panels, and in fact, each of the other similar content parameters: Items, Blocks, and Inlines.</p>

	<p>Additionally, each control is aliased without the New- verb, so you could just call <code>Button</code> instead of <code>New-Button</code> &#8230; and of course, since our button doesn&#8217;t do anything, we could just as easily have used a Label, and written:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
Label <span style="color: #009900;">&quot;Hello World&quot;</span> <span style="color: #000066;">-Show</span><br />
&nbsp;</div>

	<p>Note: &#8220;Label&#8221; is also the name of an executable for labelling drives in Windows, make sure ShowUI is imported before you run that command.</p>

	<p>One last point: ShowUI brings a lot of features from <span class="caps">WPK</span> to the table, and one you&#8217;ll use a lot is the ability to skip specifying the window and put the -Show parameter on almost any <span class="caps">WPF</span> element. This is partly because we&#8217;re back to running PowerShell 2.0 with the -<span class="caps">STA</span> switch, but in any case, we can now skip the &#8220;New-BootsWindow&#8221; command, and our examples can be that much simpler.</p>

	<h2>We can put controls in a stack</h2>

	<p><img src="http://huddledmasses.org/images/ShowUI/ShowUI-02.png" class="float-right-block" alt="" width="165" height="132" /></p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #0066cc; font-style: italic;">Show-<span style="font-style: normal;">UI</span></span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; StackPanel <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Button <span style="color: #009900;">&quot;A bed of clams&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Button <span style="color: #009900;">&quot;A coalition of cheetas&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Button <span style="color: #009900;">&quot;A gulp of swallows&quot;</span><br />
&nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<p>StackPanels are awesome. So are WrapPanels. Try that code with a WrapPanel instead of a StackPanel and see what the difference is (hint: try resizing the window). Actually, try it with a UniformGrid too. <span class="caps">WPF</span> has several other panel containers too: Grid, ToolBarPanel, TabPanel, DockPanel, Canvas &#8230; we&#8217;ll talk about some more of those later.</p>

	<h2>Ok, lets see some formatting</h2>

	<p><img src="http://huddledmasses.org/images/ShowUI/ShowUI-03.png" class="float-right-block" alt="" width="256" height="179" /></p>

	<p>To scoot the buttons out from the edge we can use margins or padding: margins go on the outside of containers, padding goes on the inside. We can also specify the FontFamily, FontSize, FontWeight, and FontStyle, as well as Foreground and Background colors &#8230; </p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #0066cc; font-style: italic;">Show-<span style="font-style: normal;">UI</span></span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; StackPanel <span style="color: #000066;">-Margin</span> <span style="color: #cc66cc;">5</span> <span style="color: #000066;">-Background</span> Pink <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; Button <span style="color: #000066;">-Margin</span> <span style="color: #cc66cc;">2</span> <span style="color: #009900;">&quot;A bed of clams&quot;</span> <span style="color: #000066;">-FontFamily</span> Consolas <span style="color: #000066;">-FontSize</span> <span style="color: #cc66cc;">24</span> <span style="color: #000066;">-FontWeight</span> Bold<br />
&nbsp; &nbsp; &nbsp; &nbsp; Button <span style="color: #000066;">-Margin</span> <span style="color: #cc66cc;">2</span> <span style="color: #009900;">&quot;A coalition of cheetas&quot;</span> <span style="color: #000066;">-FontFamily</span> Arial <span style="color: #000066;">-FontSize</span> <span style="color: #cc66cc;">20</span> <span style="color: #000066;">-FontStyle</span> Italic<br />
&nbsp; &nbsp; &nbsp; &nbsp; Button <span style="color: #000066;">-Margin</span> <span style="color: #cc66cc;">2</span> <span style="color: #009900;">&quot;A gulp of swallows&quot;</span> <span style="color: #000066;">-FontFamily</span> <span style="color: #009900;">'Segoe UI'</span> <span style="color: #000066;">-FontSize</span> <span style="color: #cc66cc;">18</span> <span style="color: #000066;">-Foreground</span> Crimson<br />
&nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<p>So you see, the pink background is on the StackPanel, which has a (default, white) margin around it.  If you wanted the whole background of the window to be pink, you would need to set the background of the Window instead of the StackPanel.</p>

<h3 style="clear: both;">An aside on Typography</h3>

	<p>ShowUI doesn&#8217;t need to create a full set of typography-specific top-level elements the way Shoes does, because we are based on <span class="caps">WPF</span>, which has a far more powerful typography system available than any we&#8217;ve used previously. So, with <span class="caps">WPF</span> we start by selecting a control based on how much text you want to put in it, and how much formatting you want to apply: Label and TextBox are the simplest, TextBlock supports limited text formattings, and FlowDocumentViewer or RichTextBox support full rich content. And of course, Hyperlink supports clicking  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=';)' class='wp-smiley' />  </p>

	<p><img src="http://huddledmasses.org/images/ShowUI/ShowUI-07.png" class="float-right-block" alt="" width="507" height="186" /></p>

	<p>For the typography elements, the content model changes a little bit.  There are basically two types of typographical elements: Inline and Block elements (where inline elements can&#8217;t contain block elements). In fact, he <a href="http://msdn.microsoft.com/en-us/library/bb613554.aspx">TextBlock Content Model</a> is similar to that of an inline element. It is actually a type-restricted &#8220;Items&#8221; container.  Instead of being able to have <em>anything</em> as content, it can only contain <a href="http://msdn.microsoft.com/en-us/library/system.windows.documents.inline.aspx">Inline</a> flow content elements such as <a href="http://msdn.microsoft.com/en-us/library/system.windows.documents.anchoredblock.aspx">AnchoredBlock</a>, <a href="http://msdn.microsoft.com/en-us/library/system.windows.documents.bold.aspx">Bold</a>, <a href="http://msdn.microsoft.com/en-us/library/system.windows.documents.hyperlink.aspx">Hyperlink</a>, <a href="http://msdn.microsoft.com/en-us/library/system.windows.documents.inlineuicontainer.aspx">InlineUIContainer</a>, <a href="http://msdn.microsoft.com/en-us/library/system.windows.documents.italic.aspx">Italic</a>, <a href="http://msdn.microsoft.com/en-us/library/system.windows.documents.linebreak.aspx">LineBreak</a>, <a href="http://msdn.microsoft.com/en-us/library/system.windows.documents.run.aspx">Run</a>, <a href="http://msdn.microsoft.com/en-us/library/system.windows.documents.span.aspx">Span</a>, and <a href="http://msdn.microsoft.com/en-us/library/system.windows.documents.underline.aspx">Underline</a>, and it will create a run automatically if you just put a text string in it. </p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #0066cc; font-style: italic;">Show-<span style="font-style: normal;">UI</span></span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;StackPanel <span style="color: #000066;">-Margin</span> <span style="color: #cc66cc;">10</span> <span style="color: #000066;">-Children</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; TextBlock <span style="color: #009900;">&quot;A Question&quot;</span> <span style="color: #000066;">-FontSize</span> <span style="color: #cc66cc;">42</span> <span style="color: #000066;">-FontWeight</span> Bold <span style="color: #000066;">-Foreground</span> <span style="color: #009900;">&quot;#FF0088&quot;</span> <br />
&nbsp; &nbsp; &nbsp; TextBlock <span style="color: #000066;">-FontSize</span> <span style="color: #cc66cc;">24</span> <span style="color: #000066;">-Inlines</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Bold <span style="color: #009900;">&quot;Q. &quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&quot;Are you starting to dig &quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Hyperlink <span style="color: #009900;">&quot;ShowUI?&quot;</span> <span style="color: #000066;">-NavigateUri</span> http:<span style="color: #66cc66;">//</span>huddledmasses.<span style="color: #003366;">org</span><span style="color: #66cc66;">/</span>tag<span style="color: #66cc66;">/</span>showui<span style="color: #66cc66;">/</span> `<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">-On_RequestNavigate</span> <span style="color: #333;">&#123;</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>Diagnostics.<span style="color: #666699; font-weight: bold;">Process</span><span style="color: #333;">&#93;</span></span>::<span style="color: #660033;">Start</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$this</span>.<span style="color: #003366;">NavigateUri</span>.<span style="color: #003366;">ToString</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#41;</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; TextBlock <span style="color: #000066;">-FontSize</span> <span style="color: #cc66cc;">16</span> <span style="color: #000066;">-Inlines</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Span <span style="color: #000066;">-FontSize</span> <span style="color: #cc66cc;">24</span> <span style="color: #000066;">-FontWeight</span> Bold <span style="color: #000066;">-Inlines</span> <span style="color: #009900;">&quot;A. &quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&quot;Leave me alone, I'm hacking here!&quot;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<p>Note: If you want support for the full <a href="http://msdn.microsoft.com/en-us/library/aa970909.aspx">document model</a> (which allows Paragraphs and Lists), you need to use a <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.flowdocumentreader.aspx">FlowDocumentReader</a>, <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.flowdocumentpageviewer.aspx">FlowDocumentPageViewer</a>, <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.richtextbox.aspx">RichTextBox</a>, or a <a href="http://msdn.microsoft.com/en-us/library/system.windows.controls.flowdocumentscrollviewer.aspx">FlowDocumentScrollViewer</a> ... there&#8217;s lots more information about those <a href="http://msdn.microsoft.com/en-us/library/aa970909.aspx">on msdn</a>.</p>

	<h2>Time for some artwork</h2>

	<p><img src="http://huddledmasses.org/images/ShowUI/ShowUI-04.png" class="float-right-block" alt="" width="179" height="171" /></p>

	<div class="posh code posh" style="font-family:monospace;"><br />
Ellipse <span style="color: #000066;">-Width</span> <span style="color: #cc66cc;">60</span> <span style="color: #000066;">-Height</span> <span style="color: #cc66cc;">80</span> <span style="color: #000066;">-Margin</span> <span style="color: #009900;">&quot;20,10,60,20&quot;</span> <span style="color: #000066;">-Fill</span> Black <span style="color: #000066;">-Show</span><br />
&nbsp;</div>

	<p>In <span class="caps">WPF</span>, everything always starts out white, and you must position things based on the container. You can see that the Margin can be specified as a single value as in the previous example, or as separate values for Left, Top, Right, Bottom.  Oddly, to satisfy PowerShell&#8217;s type-casting, you have to quote them so they&#8217;re a single comma-separated string, instead of four separate values.</p>

	<h2>Some more advanced drawing</h2>

	<p><img src="http://huddledmasses.org/images/ShowUI/ShowUI-05.png" class="float-right-block" alt="" width="155" height="160" /></p>

	<div class="posh code posh" style="font-family:monospace;"><br />
Canvas <span style="color: #000066;">-Height</span> <span style="color: #cc66cc;">100</span> <span style="color: #000066;">-Width</span> <span style="color: #cc66cc;">100</span> <span style="color: #000066;">-Children</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;Rectangle <span style="color: #000066;">-Margin</span> <span style="color: #009900;">&quot;10,10,0,0&quot;</span> <span style="color: #000066;">-Width</span> <span style="color: #cc66cc;">45</span> <span style="color: #000066;">-Height</span> <span style="color: #cc66cc;">45</span> <span style="color: #000066;">-Stroke</span> <span style="color: #009900;">&quot;#689945&quot;</span> <span style="color: #000066;">-StrokeThickness</span> <span style="color: #cc66cc;">2</span> <span style="color: #000066;">-Fill</span> <span style="color: #009900;">&quot;#336699&quot;</span><br />
&nbsp; &nbsp;Polygon <span style="color: #000066;">-Stroke</span> Pink <span style="color: #000066;">-StrokeThickness</span> <span style="color: #cc66cc;">2</span> <span style="color: #000066;">-Fill</span> DarkRed <span style="color: #000066;">-Points</span> <span style="color: #009900;">&quot;10,60&quot;</span>, <span style="color: #009900;">&quot;50,60&quot;</span>, <span style="color: #009900;">&quot;50,50&quot;</span>, <span style="color: #009900;">&quot;65,65&quot;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #009900;">&quot;50,80&quot;</span>, <span style="color: #009900;">&quot;50,70&quot;</span>, <span style="color: #009900;">&quot;10,70&quot;</span>, <span style="color: #009900;">&quot;10,60&quot;</span> <br />
<span style="color: #333;">&#125;</span> <span style="color: #000066;">-Show</span><br />
&nbsp;</div>

	<p>When you want to start getting clever and overlapping things, you need to use a Canvas container. The Canvas can contain multiple items which are all absolutely positioned, but unlike most other containers, it doesn&#8217;t automatically expand to contain it&#8217;s children, so you typically have to set it&#8217;s size.</p>

	<p>We also have to set the Stroke and Fill.  These are the two colors that make up every object, and again, if we don&#8217;t set them, they default to white. Note that you can use named colors, or you can specify a hex value using &#8220;#RRGGBB&#8221; or &#8220;#AARRGGBB&#8221; to set the alpha channel. The StrokeThickness controls the line thickness.  </p>

	<p>One other thing to notice is that we positioned the Rectangle by using the <code>Margin</code>, but we positioned the arrow, which we built using a Polygon, based purely on the x,y coordinates of the points.  The available shapes are Ellipse, Line, Path, Polygon, Polyline, and Rectangle.  You can, of course, make nearly any shape you want with the Polygon.</p>

	<p>There are other more advanced shapes available in external libraries, and we can even do 3D, use gradient or image fills&#8230;</p>

	<h3>We can even get images straight off the web</h3>

	<div class="posh code posh" style="font-family:monospace;"><br />
&nbsp; &nbsp;Image <span style="color: #000066;">-Source</span> http:<span style="color: #66cc66;">//</span>huddledmasses.<span style="color: #003366;">org</span><span style="color: #66cc66;">/</span>images<span style="color: #66cc66;">/</span>PowerBoots<span style="color: #66cc66;">/</span>IMG_3298.<span style="color: #003366;">jpg</span> <span style="color: #000066;">-MaxWidth</span> <span style="color: #cc66cc;">400</span> <span style="color: #000066;">-AsJob</span><br />
&nbsp;</div>

	<p><img src="http://huddledmasses.org/images/ShowUI/ShowUI-06.png" class="float-right-block" alt="" width="437" height="729" /></p>

	<p><span class="caps">WPF</span> loads the image on a background thread, and caches it in memory, so the window will show up and be responsive while you&#8217;re waiting for the image, and because we&#8217;ve specified <code>-AsJob</code>, you can actually continue using PowerShell while the image loads. Note: it will load much faster the second time you run that script.  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=';)' class='wp-smiley' /> </p>

	<h3>Events</h3>

	<p>If you were paying attention to the typography example, you&#8217;ll have noticed that we introduced event handling without making a big fuss about it. Event handlers in PowerBoots are specified in much the same way that Properties are, except that their parameter names always start with &#8220;On_&#8221; and they take a script block.  The Hyperlink element in a <span class="caps">WPF</span> window doesn&#8217;t automatically open a browser (because you can use it to change &#8220;pages&#8221; in a <span class="caps">WPF</span> application), so to make simple web links work, you have to handle the &#8220;RequestNavigate&#8221; event as shown above.</p>

	<p><img src="http://huddledmasses.org/images/ShowUI/ShowUI-08.png" class="float-right-block" alt="" width="259" height="86" /></p>

	<div class="posh code posh" style="font-family:monospace;"><br />
WrapPanel <span style="color: #000066;">-ControlName</span> <span style="color: #009900;">'Click Counter'</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; Button <span style="color: #009900;">&quot;Push Me&quot;</span> <span style="color: #000066;">-On_Click</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; $<span style="color: #333;">&#123;</span>Click Counter<span style="color: #333;">&#125;</span>.<span style="color: #003366;">Tag</span> <span style="color: #66cc66;">=</span> $<span style="color: #333;">&#123;</span>Click Counter<span style="color: #333;">&#125;</span>.<span style="color: #003366;">Tag</span> <span style="color: #66cc66;">+</span> <span style="color: #cc66cc;">1</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$CountLabel</span>.<span style="color: #003366;">Content</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;You clicked the button $(${Click Counter}.Tag) times!&quot;</span><br />
&nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; Label <span style="color: #009900;">&quot;Nothing pushed so far&quot;</span> <span style="color: #000066;">-Name</span> CountLabel<br />
<span style="color: #333;">&#125;</span> <span style="color: #000066;">-Show</span><br />
&nbsp;</div>

	<p>In order to update your user interface when an event triggers, you&#8217;ll need to have variables that point at the control(s) you want to affect.  In ShowUI event handlers, you get a <code>$this</code> variable which points at the object that caused the event, a <code>$_</code> variable which is the event arguments, and a <code>$window</code> variable for the top-level window &#8230; you also get variables for each named control in your window.  </p>

	<p><span class="caps">WPF</span> Elements all have a <code>Tag</code> property which can be used to store any object, so as you can see in the previous example, you can use that to keep track of things &#8230; but more importantly, the value of the <code>Tag</code> on the top-level control will be output. In other words, when the window is closed, this example actually outputs to the PowerShell pipeline how many times you clicked the button.</p>

	<h3>Working with User Input</h3>

	<p>There are helper functions in ShowUI for working with that output value: <code>Set-UIValue</code> and <code>Get-UIValue</code>, and Get-UIValue is particularly helpful because if the control it&#8217;s called on has no value, it collects the values of all the child controls, so you can use it to output a whole form at once:</p>

	<p><img src="http://huddledmasses.org/images/ShowUI/ShowUI-09.png" class="float-right-block" alt="" width="289" height="260" /></p>

	<p>I&#8217;ve made this example more complicated than it needed to be to demonstrate some best practices.  We could have made it much simpler by using a UniformGrid control instead of a GridControl, thus avoiding needing to set the -Row and -Column special parameters, but I wanted to show those to you anyway, and the form looks a lot better when the two columns can have different sizes:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
Grid <span style="color: #000066;">-ControlName</span> <span style="color: #009900;">'Your Information'</span> <span style="color: #000066;">-Columns</span> Auto,<span style="color: #66cc66;">*</span> <span style="color: #000066;">-Rows</span> <span style="color: #cc66cc;">7</span> <span style="color: #000066;">-MinHeight</span> <span style="color: #cc66cc;">200</span> <span style="color: #000066;">-MinWidth</span> <span style="color: #cc66cc;">250</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; Label <span style="color: #009900;">&quot;First Name&quot;</span><br />
&nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">TextBox</span></span> <span style="color: #000066;">-Name</span> First <span style="color: #000066;">-Column</span> <span style="color: #cc66cc;">1</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; Label <span style="color: #009900;">&quot;Last Name&quot;</span> <span style="color: #000066;">-Row</span> <span style="color: #cc66cc;">1</span> <br />
&nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">TextBox</span></span> <span style="color: #000066;">-Name</span> Last <span style="color: #000066;">-Row</span> <span style="color: #cc66cc;">1</span> <span style="color: #000066;">-Column</span> <span style="color: #cc66cc;">1</span><br />
<br />
&nbsp; &nbsp; Label <span style="color: #009900;">&quot;Address&quot;</span> <span style="color: #000066;">-Row</span> <span style="color: #cc66cc;">2</span><br />
&nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">TextBox</span></span> <span style="color: #000066;">-Name</span> Address <span style="color: #000066;">-Row</span> <span style="color: #cc66cc;">2</span> <span style="color: #000066;">-Column</span> <span style="color: #cc66cc;">1</span><br />
<br />
&nbsp; &nbsp; Label <span style="color: #009900;">&quot;City&quot;</span> <span style="color: #000066;">-Row</span> <span style="color: #cc66cc;">3</span> <br />
&nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">TextBox</span></span> <span style="color: #000066;">-Name</span> City <span style="color: #000066;">-Row</span> <span style="color: #cc66cc;">3</span> <span style="color: #000066;">-Column</span> <span style="color: #cc66cc;">1</span><br />
<br />
&nbsp; &nbsp; Label <span style="color: #009900;">&quot;State&quot;</span> <span style="color: #000066;">-Row</span> <span style="color: #cc66cc;">4</span><br />
&nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">TextBox</span></span> <span style="color: #000066;">-Name</span> State <span style="color: #000066;">-Row</span> <span style="color: #cc66cc;">4</span> <span style="color: #000066;">-Column</span> <span style="color: #cc66cc;">1</span><br />
<br />
&nbsp; &nbsp; Label <span style="color: #009900;">&quot;Zip&quot;</span> <span style="color: #000066;">-Row</span> <span style="color: #cc66cc;">5</span> <br />
&nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">TextBox</span></span> <span style="color: #000066;">-Name</span> Zip <span style="color: #000066;">-Row</span> <span style="color: #cc66cc;">5</span> <span style="color: #000066;">-Column</span> <span style="color: #cc66cc;">1</span><br />
<br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">Button</span></span> <span style="color: #009900;">&quot;OK&quot;</span> <span style="color: #000066;">-IsDefault</span> <span style="color: #000066;">-On_Click</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">Get-<span style="font-style: normal;">ParentControl</span></span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Set-<span style="font-style: normal;">UIValue</span></span> <span style="color: #000066;">-PassThru</span> <span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">Close-<span style="font-style: normal;">Control</span></span><br />
&nbsp; &nbsp; <span style="color: #333;">&#125;</span> <span style="color: #000066;">-Row</span> <span style="color: #cc66cc;">6</span> <span style="color: #000066;">-Column</span> <span style="color: #cc66cc;">1</span><br />
<span style="color: #333;">&#125;</span> <span style="color: #000066;">-Show</span> <span style="color: #000066;">-On_Loaded</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$First</span>.<span style="color: #003366;">Focus</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<p>You&#8217;ll notice how easily I specified the width of the columns, but the <span class="caps">COUNT</span> of the rows in the Grid panel. If you provide an array of values, they&#8217;re converted to GridLengths, but if you provide just one, it&#8217;s treated as the count. You can make grid columnds and rows AUTOmatically size to their contents, and you can make them * width to make them take up any extra space.  You can even split the extra space by setting * on more than one column (and specify the proportion by using numbers, like: 1*, 2*).</p>

	<p>I should also point out that if you use MinHeight and MinWidth instead of the Height and Width values, your controls will be able to size up to fill space when the window is resized! Try that script with Width and Height instead and resize the window to see the difference.</p>

	<p>We used the Set-UIValue shortcut in this form, which brings up another point: when creating data forms, you only need to name the controls you want output from. Then you&#8217;ll be able to just call Set-UIValue on the parent to collect all the values from the controls and output them as a hashtable!</p>

	<p>Finally, remember your defaults: set the focus to something so the user doesn&#8217;t <span class="caps">HAVE</span> to click to get started, and set a button to -IsDefault with a On_Click handler so that when the user hits enter they can submit the form.</p>

	<h3>Further directions</h3>

	<p>There&#8217;s a lot more possible with ShowUI: we can use gradients for colors, create data templates and styles, and even make chromeless windows, but you have the basics for getting started already.</p>

	<p>This first release of ShowUI is still missing some features from both <span class="caps">WPK</span> and PowerBoots, but we&#8217;ll get to them as we progress.  For now we&#8217;d like to invite you to come <a href="http://showui.codeplex.com/releases/">download the latest release</a>, and <a href="http://showui.codeplex.com/workitem/list/basic">write up or vote up the features</a> that you want the most in the next version.  Keep an eye on the release page and on the discussions and we&#8217;ll be cranking out new releases on a monthly basis for now.</p>

	<p>I hope you&#8217;ve enjoyed this tour through ShowUI, and will be able to start applying it soon for fun and profit.</p>

	<p id="fn1" class="footnote"><sup>1</sup> The -<span class="caps">STA</span> switch is necessary because the .Net framework requires the <acronym title="Single Threaded Apartment">STA</acronym> threading model in order to do graphical user interfaces. PowerShell <span class="caps">ISE</span> doesn&#8217;t require it because unlike the PowerShell.exe command console (which is a Win32 native &#8220;console&#8221; application that hosts the .Net framework), PowerShell <span class="caps">ISE</span> is a .Net framework &#8220;graphical&#8221; application, and is therefore running in the <span class="caps">STA</span> model already.</p>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/showui-tutorial-walkthrough/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>PowerBoots (Next Generation UI) WPF for PowerShell slides</title>
		<link>http://huddledmasses.org/powerboots-presentation-2011-02/</link>
		<comments>http://huddledmasses.org/powerboots-presentation-2011-02/#comments</comments>
		<pubDate>Mon, 14 Feb 2011 04:29:41 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[PowerBoots]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[ShowUI]]></category>
		<category><![CDATA[UserInterface]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1657</guid>
		<description><![CDATA[I gave a LiveMeeting presentation on PowerBoots last week, and these are the slides. I&#8217;m sorry to say that the recording did not work out, so you&#8217;ll have to wait for the next time I present it to see the demos. Luckily, the demo scripts I used are mostly the samples which are available in [...]]]></description>
			<content:encoded><![CDATA[	<p>I gave a LiveMeeting presentation on PowerBoots last week, and these are the slides. I&#8217;m sorry to say that the recording did not work out, so you&#8217;ll have to wait for the next time I present it to see the demos. Luckily, the demo scripts I used are mostly the samples which <em>are</em> available in the PowerBoots distributions.</p>

	<ul>
		<li><a href="/downloads/PowerBoots/PowerBoots-2011.pptx" title="2.13Mb">PowerPoint Deck</a></li>
	</ul>
	<ul>
		<li><a href="/downloads/PowerBoots/PowerBoots-2011.pdf" title="973Kb"><span class="caps">PDF</span> Deck with notes</a></li>
	</ul>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/powerboots-presentation-2011-02/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PowerBoots: Loading XAML Windows in PowerShell 1.0 or 2.0</title>
		<link>http://huddledmasses.org/powerboots-loading-xaml-windows-in-powershell-10-or-20/</link>
		<comments>http://huddledmasses.org/powerboots-loading-xaml-windows-in-powershell-10-or-20/#comments</comments>
		<pubDate>Fri, 13 Feb 2009 07:20:07 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[PowerBoots]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[UserInterface]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[XAML]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1059</guid>
		<description><![CDATA[Awhile back I wrote a series of posts about WPF From PowerShell From PowerShell&#8221; which were about how you could load XAML in previous PowerShell 2 CTPs to create WPF user interfaces &#8230; a few people have mentioned loading XAML in PowerBoots, and a couple of people have posted other samples showing XAML even since [...]]]></description>
			<content:encoded><![CDATA[	<p>Awhile back I wrote a series of posts about <a href="http://huddledmasses.org/?s=&#34;WPF"><span class="caps">WPF</span> From PowerShell</a> From PowerShell&#8221; which were about how you could load <span class="caps">XAML</span> in previous PowerShell 2 CTPs to create <span class="caps">WPF</span> user interfaces &#8230; a few people have mentioned loading <span class="caps">XAML</span> in <a href="http://boots.CodePlex.com">PowerBoots</a>, and a couple of people have posted other samples showing <span class="caps">XAML</span> even since I published the most recent release, so I figure it&#8217;s time to point out that you really can load that <span class="caps">XAML</span> into Boots, and get all the threading and other support.  </p>

	<p>Just for fun, I&#8217;m going to rehash an earlier post about <a href="http://huddledmasses.org/wpf-from-powershell-updating-windows/">updating windows</a> to show how you can go about this using PowerBoots, and hopefully show that it&#8217;s a little easier (and a lot more async).  Compare and contrast the code in this article with that one, just for fun.</p>

	<h2>This works with any version of PowerShell</h2>

	<p>Unlike the original article, most this code (except where explicitly mentioned) works on either PowerShell v2 with PowerBoots, or PowerShell 1.0 with <strong>PoshWPF</strong>, a snapin that is part of the PowerBoots module but is also released separately&#8230; Also, unlike those previous posts, this does <em>not</em> require you to be running PowerShell.exe with the -<span class="caps">STA</span> switch, since the New-BootsWindow cmdlet takes care of threading for us.</p>

<span id="more-1059"></span>

	<h3>The simple splash screen</h3>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #660033; font-weight: bold;">$Splash</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">BootsWindow</span></span> <span style="color: #000066;">-Async</span> <span style="color: #000066;">-Passthru</span> <span style="color: #000066;">-SourceTemplate</span> @<span style="color: #009900;">&quot;<br />
&lt;window xmlns=&quot;</span>http:<span style="color: #66cc66;">//</span>schemas.<span style="color: #003366;">microsoft</span>.<span style="color: #003366;">com</span><span style="color: #66cc66;">/</span>winfx<span style="color: #66cc66;">/</span><span style="color: #cc66cc;">2006</span><span style="color: #66cc66;">/</span>xaml<span style="color: #66cc66;">/</span>presentation<span style="color: #009900;">&quot; <br />
&nbsp; &nbsp; &nbsp; &nbsp; windowstyle=&quot;</span>None<span style="color: #009900;">&quot; allowstransparency=&quot;</span>True<span style="color: #009900;">&quot; opacity=&quot;</span><span style="color: #cc66cc;">0.8</span><span style="color: #009900;">&quot; <br />
&nbsp; &nbsp; &nbsp; &nbsp; topmost=&quot;</span>True<span style="color: #009900;">&quot; sizetocontent=&quot;</span>WidthAndHeight<span style="color: #009900;">&quot; <br />
&nbsp; &nbsp; &nbsp; &nbsp; windowstartuplocation=&quot;</span>CenterOwner<span style="color: #009900;">&quot; showintaskbar=&quot;</span>False<span style="color: #009900;">&quot;&gt;<br />
&nbsp; &nbsp;&lt;img source=&quot;</span>http:<span style="color: #66cc66;">//</span>dilbert.<span style="color: #003366;">com</span><span style="color: #66cc66;">/</span>dyn<span style="color: #66cc66;">/</span>str_strip<span style="color: #66cc66;">/</span>000000000<span style="color: #66cc66;">/</span>00000000<span style="color: #66cc66;">/</span>0000000<span style="color: #66cc66;">/</span>000000<span style="color: #66cc66;">/</span><span style="color: #cc66cc;">40000</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">1000</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">200</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">41215</span><span style="color: #66cc66;">/</span>41215.<span style="color: #003366;">strip</span>.<span style="color: #003366;">print</span>.<span style="color: #003366;">gif</span><span style="color: #009900;">&quot; <br />
&nbsp; &nbsp; &nbsp; &nbsp; height=&quot;</span><span style="color: #cc66cc;">177</span><span style="color: #009900;">&quot;&gt;<br />
&lt;/window&gt;<br />
&quot;</span>@ <span style="color: #000066;">-Content</span> <span style="color: #333;">&#123;</span><span style="color: #660033; font-weight: bold;">$Args</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#93;</span>.<span style="color: #003366;">Content</span><span style="color: #333;">&#125;</span> <span style="color: #000066;">-On_MouseDown</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$this</span>.<span style="color: #003366;">DragMove</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#125;</span><br />
<span style="color: #666666; font-style: italic;"># Imagine this is your script, working ...</span><br />
<span style="color: #0066cc; font-style: italic;">Start-<span style="font-style: normal;">Sleep</span></span> <span style="color: #cc66cc;">3</span><br />
<span style="color: #666666; font-style: italic;"># And now you're done, and want to close it</span><br />
<span style="color: #0066cc; font-style: italic;">Invoke-<span style="font-style: normal;">BootsWindow</span></span> <span style="color: #660033; font-weight: bold;">$Splash</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$Splash</span>.<span style="color: #003366;">Close</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#125;</span></div>

	<p>This is, of course, not a great example of why you would want to use <span class="caps">XAML</span>, since it reads virtually the same as it would if you wrote it in PowerBoots (if you were on PowerShell v2):</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #666666; font-style: italic;">## This requires PowerBoots which is (as of this writing) is still v2 only...</span><br />
<span style="color: #660033; font-weight: bold;">$window</span> <span style="color: #66cc66;">=</span> Boots <span style="color: #000066;">-Async</span> <span style="color: #000066;">-Passthru</span> &nbsp;<span style="color: #000066;">-Content</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp;Image <span style="color: #000066;">-Height</span> <span style="color: #cc66cc;">177</span> <span style="color: #000066;">-Source</span> http:<span style="color: #66cc66;">//</span>dilbert.<span style="color: #003366;">com</span><span style="color: #66cc66;">/</span>dyn<span style="color: #66cc66;">/</span>str_strip<span style="color: #66cc66;">/</span>000000000<span style="color: #66cc66;">/</span>00000000<span style="color: #66cc66;">/</span>0000000<span style="color: #66cc66;">/</span>000000<span style="color: #66cc66;">/</span><span style="color: #cc66cc;">40000</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">1000</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">200</span><span style="color: #66cc66;">/</span><span style="color: #cc66cc;">41215</span><span style="color: #66cc66;">/</span>41215.<span style="color: #003366;">strip</span>.<span style="color: #003366;">print</span>.<span style="color: #003366;">gif</span><br />
<span style="color: #333;">&#125;</span> <span style="color: #000066;">-WindowStyle</span> None <span style="color: #000066;">-AllowsTransparency</span> <span style="color: #660033; font-weight: bold;">$True</span> <span style="color: #000066;">-Opacity</span> <span style="color: #cc66cc;">0.8</span> <span style="color: #000066;">-Topmost</span> <span style="color: #660033; font-weight: bold;">$True</span> <span style="color: #000066;">-WindowStartupLocation</span> CenterOwner <span style="color: #000066;">-ShowInTaskbar</span> <span style="color: #660033; font-weight: bold;">$False</span> <span style="color: #000066;">-On_MouseDown</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$this</span>.<span style="color: #003366;">DragMove</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;"># Imagine this is your script, working ...</span><br />
<span style="color: #0066cc; font-style: italic;">Start-<span style="font-style: normal;">Sleep</span></span> <span style="color: #cc66cc;">3</span><br />
<span style="color: #666666; font-style: italic;"># And now you're done, and want to close it</span><br />
<span style="color: #0066cc; font-style: italic;">Invoke-<span style="font-style: normal;">BootsWindow</span></span> <span style="color: #660033; font-weight: bold;">$Splash</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$Splash</span>.<span style="color: #003366;">Close</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#125;</span></div>

	<p>The big difference, of course, is that this is fully aync, so unlike the splash screen in the original post, we don&#8217;t have to do any tricks to get it to render, and we are able to add a MouseDown handler so you can drag the splash screen around while you wait for it to go away&#8230;</p>

	<h2>And now, for something completely different</h2>

	<p>Let&#8217;s skip over the very useful information I already wrote about the properties you can set on the &#8220;Window&#8221; object in <span class="caps">WPF</span> (all of which are parameters to New-BootsWindow, by the way) and cut straight to the second example &#8230; I&#8217;ve tweaked an old <span class="caps">WPF</span> sample I had and ported it to PowerShell, <em>and now, adapted it for PoshWpf</em> ... the <a href="http://huddledmasses.org/wpf-from-powershell-updating-windows/clock/" rel="attachment wp-att-528">xaml file is here</a> and I&#8217;ll show you the code just as soon as you take a look at the screenshot (trust me, the code will make more sense if you&#8217;ve seen the output):</p>

	<p><img src="http://huddledmasses.org/images/2008-05-09_0029.png" title="A clock, with working CPU and RAM bars" alt="A clock, with working CPU and RAM bars" width="783" height="312" /></p>

	<p>So basically, the UI is defined in a href=&#8216;http://HuddledMasses.org/wpf-from-powershell-updating-windows/clock/&#8217; rel=&#8216;attachment wp-att-528&#8217;&gt;that xaml file, and all we need to do in the script is update the time, update the <span class="caps">CPU</span> bar, and update the <span class="caps">RAM</span> bar&#8230;</p>

	<h3>Please notice the content block</h3>

	<p>PowerBoots passes the window object itself into the content scriptblock, and the content block <strong>must</strong> output the content you want in the window. Of course, if you&#8217;re loading a fully defined window in <span class="caps">XAML</span>, you don&#8217;t want to change the Window&#8217;s content, but you have to return something, so you should just return &#8230; the content that&#8217;s already in the window. Ie: <code>$Args[0].Content</code></p>

	<p>One other point, for those of you who are going to become <span class="caps">XAML</span> masters. In PowerBoots you can define just the styles and templates as resources in a separate <span class="caps">XAML</span> file which you can load with the <code>Add-BootsTemplate</code>, and then you can still create the UI in <span class="caps">XAML</span> or useing PowerBoots syntax.  In my case, this <span class="caps">XAML</span> was edited  using <a href="http://www.kaxaml.com/">kaxaml</a> (it could have been Expression Blend, or Visual Studio), and kaxaml doesn&#8217;t make it easy to separate out resource files, so I just left it all in one file:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Host</span></span> <span style="color: #009900;">&quot;Initializing Performance Counters, please have patience&quot;</span> <span style="color: #000066;">-fore</span> Cyan<br />
<span style="color: #666666; font-style: italic;">### Import PoshWpf module</span><br />
<span style="color: #0066cc; font-style: italic;">Import-<span style="font-style: normal;">Module</span></span> PowerBoots<br />
<span style="color: #666666; font-style: italic;">### Or, on v1:</span><br />
<span style="color: #666666; font-style: italic;"># Add-PSSnapin PoshWpf</span><br />
<br />
<span style="color: #660033; font-weight: bold;">$script</span>:cpu <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">new-<span style="font-style: normal;">object</span></span> System.<span style="color: #003366;">Diagnostics</span>.<span style="color: #003366;">PerformanceCounter</span> <span style="color: #009900;">&quot;Processor&quot;</span>, <span style="color: #009900;">&quot;% Processor Time&quot;</span>, <span style="color: #009900;">&quot;_Total&quot;</span><br />
<span style="color: #660033; font-weight: bold;">$script</span>:ram <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">new-<span style="font-style: normal;">object</span></span> System.<span style="color: #003366;">Diagnostics</span>.<span style="color: #003366;">PerformanceCounter</span> <span style="color: #009900;">&quot;Memory&quot;</span>, <span style="color: #009900;">&quot;Available KBytes&quot;</span><br />
<br />
<span style="color: #666666; font-style: italic;">## get initial values, because the counters don't work until the second call</span><br />
<span style="color: #660033; font-weight: bold;">$null</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$script</span>:cpu.<span style="color: #003366;">NextValue</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
<span style="color: #660033; font-weight: bold;">$null</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$script</span>:ram.<span style="color: #003366;">NextValue</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
<span style="color: #660033; font-weight: bold;">$script</span>:maxram <span style="color: #66cc66;">=</span> <span style="color: #333;">&#40;</span><span style="color: #660033;">gwmi</span> Win32_OperatingSystem<span style="color: #333;">&#41;</span>.<span style="color: #003366;">TotalVisibleMemorySize</span><br />
<br />
<span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Host</span></span> <span style="color: #009900;">&quot;Loading XAML window...&quot;</span> <span style="color: #000066;">-fore</span> Cyan<br />
<span style="color: #666666; font-style: italic;">## Load the XAML and show the window. It won't be updating itself yet...</span><br />
<span style="color: #660033; font-weight: bold;">$clock</span> <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">BootsWindow</span></span> <span style="color: #000066;">-Async</span> <span style="color: #000066;">-Passthru</span> <span style="color: #000066;">-Content</span> <span style="color: #333;">&#123;</span> <span style="color: #660033; font-weight: bold;">$Args</span><span style="color: #333;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #333;">&#93;</span>.<span style="color: #003366;">Content</span> <br />
<span style="color: #333;">&#125;</span> <span style="color: #000066;">-FileTemplate</span> <span style="color: #009900;">&quot;C:\Users\Joel\Documents\WindowsPowerShell\Scripts\Demo\clock.xaml&quot;</span> <br />
<br />
<span style="color: #666666; font-style: italic;">## Create a script block which will update the UI by changing the Resources!</span><br />
<span style="color: #660033; font-weight: bold;">$counter</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span>;<br />
<span style="color: #660033; font-weight: bold;">$updateBlock</span> <span style="color: #66cc66;">=</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;"># Update the clock</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$clock</span>.<span style="color: #003366;">Resources</span><span style="color: #333;">&#91;</span><span style="color: #009900;">&quot;Time&quot;</span><span style="color: #333;">&#93;</span> <span style="color: #66cc66;">=</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>DateTime<span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">Now</span>.<span style="color: #003366;">ToString</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;hh:MM.ss&quot;</span><span style="color: #333;">&#41;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;"># We only want to update the counters at most once a second</span><br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;"># Otherwise their values are invalid and ...</span><br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;"># The CPU counter fluctuates from 0 to the real number</span><br />
&nbsp; &nbsp;<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$counter</span><span style="color: #66cc66;">++</span> <span style="color: #000066;">-eq</span> <span style="color: #cc66cc;">4</span> <span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$counter</span> <span style="color: #66cc66;">=</span> <span style="color: #cc66cc;">0</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># Update the CPU counter with the absolute value and the percentage</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$cu</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$cpu</span>.<span style="color: #003366;">NextValue</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$clock</span>.<span style="color: #003366;">Resources</span>.<span style="color: #003366;">CpuP</span> <span style="color: #66cc66;">=</span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$cu</span> <span style="color: #66cc66;">/</span> <span style="color: #cc66cc;">100</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$clock</span>.<span style="color: #003366;">Resources</span>.<span style="color: #003366;">Cpu</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;{0:0.0}%&quot;</span> <span style="color: #000066;">-f</span> <span style="color: #660033; font-weight: bold;">$cu</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># Update the RAM counter with the absolute value and the percentage</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$rm</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$ram</span>.<span style="color: #003366;">NextValue</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$clock</span>.<span style="color: #003366;">Resources</span>.<span style="color: #003366;">RamP</span> <span style="color: #66cc66;">=</span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$rm</span> <span style="color: #66cc66;">/</span> <span style="color: #660033; font-weight: bold;">$maxram</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$clock</span>.<span style="color: #003366;">Resources</span>.<span style="color: #003366;">Ram</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">&quot;{0:0.00}Mb&quot;</span> <span style="color: #000066;">-f</span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$rm</span><span style="color: #66cc66;">/</span>1MB<span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;">## Now we need to call that scriptblock on a timer. That's easy, but it</span><br />
<span style="color: #666666; font-style: italic;">## must be done on the window's thread, so we use Invoke-BootsWindow.</span><br />
<span style="color: #666666; font-style: italic;">## Notice the first argument is the window we want to run the script in</span><br />
<span style="color: #0066cc; font-style: italic;">Invoke-<span style="font-style: normal;">BootsWindow</span></span> <span style="color: #660033; font-weight: bold;">$clock</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;">## We'll create a timer</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$global</span>:timer <span style="color: #66cc66;">=</span> <span style="color: #0066cc; font-style: italic;">new-<span style="font-style: normal;">object</span></span> System.<span style="color: #003366;">Windows</span>.<span style="color: #003366;">Threading</span>.<span style="color: #003366;">DispatcherTimer</span><br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;">## Which will fire 4 times every second</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$timer</span>.<span style="color: #003366;">Interval</span> <span style="color: #66cc66;">=</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>TimeSpan<span style="color: #333;">&#93;</span></span><span style="color: #009900;">&quot;0:0:0.25&quot;</span><br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;">## And will invoke the $updateBlock</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$timer</span>.<span style="color: #003366;">Add_Tick</span><span style="color: #333;">&#40;</span> <span style="color: #660033; font-weight: bold;">$updateBlock</span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #666666; font-style: italic;">## Now start the timer running</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$timer</span>.<span style="color: #660033;">Start</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;">## And just like that, the $UpdateBlock is running 4x a second</span><br />
<span style="color: #666666; font-style: italic;">## and the clock is working. &nbsp;Pretty cool, right?</span><br />
<span style="color: #666666; font-style: italic;">## what's really cool is ... we still have full control in the console</span><br />
<span style="color: #666666; font-style: italic;">## so we can add these events afterward:</span><br />
<br />
<span style="color: #666666; font-style: italic;">## If we wanted to, say, handle mouse events to let you drag the window or close it ...</span><br />
<span style="color: #0066cc; font-style: italic;">Invoke-<span style="font-style: normal;">BootsWindow</span></span> <span style="color: #660033; font-weight: bold;">$clock</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$clock</span>.<span style="color: #003366;">Add_MouseLeftButtonDown</span><span style="color: #333;">&#40;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">Handled</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$true</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$clock</span>.<span style="color: #003366;">DragMove</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <span style="color: #666666; font-style: italic;"># WPF Magic!</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span> <span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$clock</span>.<span style="color: #003366;">Add_MouseRightButtonDown</span><span style="color: #333;">&#40;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">Handled</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$true</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$timer</span>.<span style="color: #003366;">Stop</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> &nbsp;<span style="color: #666666; font-style: italic;"># we'd like to stop that timer now, thanks.</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$clock</span>.<span style="color: #003366;">Close</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <span style="color: #666666; font-style: italic;"># and close the windows</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span> <span style="color: #333;">&#41;</span><br />
<span style="color: #333;">&#125;</span><br />
&nbsp;</div>

	<h2>So, meet Register-BootsEvent</h2>

	<p>If you&#8217;ve read the previous PowerBoots articles, you know that you can specify events as parameters to the PowerBoots functions, like <code>-On_Click</code> or <code>-On_MouseLeftButtonDown</code>, but in the case where you&#8217;re loading <span class="caps">XAML</span>, you can&#8217;t specify events in the <span class="caps">XAML</span> (WPF&#8217;s XamlReader won&#8217;t read <span class="caps">XAML</span> with events on it, or even with the <code>Class</code> specified, because you can&#8217;t use the associated code file anyway).  </p>

	<p>In any case, what I&#8217;m adding to the next version of PowerBoots/PoshWpf are two functions: </p>

	<ol>
		<li>Find-BootsControl takes a boots window and the <strong>Name</strong> of a control, and will (try to) find the named control. Of course, for that to work, you need to know the name of the control (your best bet is to name it yourself, but in the next version I may also issue names for elements as they are created if you don&#8217;t specify the name).</li>
	</ol>
	<ol>
		<li>Register-BootsEvent takes either a control, OR a boots window and control name, plus an event name and a ScriptBlock.  If you pass the window and name, it calls Find-BootsControl to find the control; once it has a control, it registers the ScriptBlock as a handler for the specified event.</li>
	</ol>

	<p>For now, both of these require that you&#8217;ve assigned a name to the control, but I&#8217;m working on setting things up so controls would be automatically named (based on their type, as with the old WinForms designer: like Button1, Button2, etc.), or allowing an XPath-like selection without named controls. In any case, this will work nicely for generating event handlers for <span class="caps">WPF</span> imported from <span class="caps">XAML</span>, and you can even specify the element directly (ie: if you don&#8217;t specify the -Name parameter, the event will be hooked on whatever element you pass in) so that last block of code can be written like this in the next release:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #666666; font-style: italic;">## If we wanted to, say, handle mouse events to let you drag the window or close it ...</span><br />
<span style="color: #0066cc; font-style: italic;">Register-<span style="font-style: normal;">BootsEvent</span></span> <span style="color: #660033; font-weight: bold;">$clock</span> <span style="color: #000066;">-Event</span> MouseLeftButtonDown <span style="color: #000066;">-Action</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">Handled</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$true</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$this</span>.<span style="color: #003366;">DragMove</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <span style="color: #666666; font-style: italic;"># WPF Magic!</span><br />
<span style="color: #333;">&#125;</span><br />
<span style="color: #0066cc; font-style: italic;">Register-<span style="font-style: normal;">BootsEvent</span></span> <span style="color: #660033; font-weight: bold;">$clock</span> <span style="color: #000066;">-Event</span> MouseRightButtonDown <span style="color: #000066;">-Action</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$_</span>.<span style="color: #003366;">Handled</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$true</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$timer</span>.<span style="color: #003366;">Stop</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> &nbsp;<span style="color: #666666; font-style: italic;"># we'd like to stop that timer now, thanks.</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$this</span>.<span style="color: #003366;">Close</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span> <span style="color: #666666; font-style: italic;"># and close the windows</span><br />
<span style="color: #333;">&#125;</span><br />
&nbsp;</div>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/powerboots-loading-xaml-windows-in-powershell-10-or-20/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>A quick progress update on PoshConsole</title>
		<link>http://huddledmasses.org/a-quick-progress-update-on-poshconsole/</link>
		<comments>http://huddledmasses.org/a-quick-progress-update-on-poshconsole/#comments</comments>
		<pubDate>Tue, 21 Aug 2007 05:10:43 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[DeskOps]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[PoshConsole]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[UserInterface]]></category>

		<guid isPermaLink="false">http://HuddledMasses.org/jaykul/a-quick-progress-update-on-poshconsole/</guid>
		<description><![CDATA[Click for a teaser screenshot This is actually a bit scary, because you can load sort-of &#8230; anything To prove it (you&#8217;re gonna love this &#8230; I should take a video, really, but it&#8217;s time for bed, so this will have to do): check this out Beat that! So yeah, I have this control written [...]]]></description>
			<content:encoded><![CDATA[	<p><a href="http://poshconsole.org/images/PoshConsole/Out-WPF.png">Click for a teaser screenshot</a></p>

	<p>This is actually a bit scary, because you can load sort-of &#8230; anything  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=';-)' class='wp-smiley' />  To prove it (you&#8217;re gonna love this &#8230; I should take a video, really, but it&#8217;s time for bed, so this will have to do): <a href="http://poshconsole.org/images/PoshConsole/Out-WPF2.png">check this out</a> Beat that!  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=';)' class='wp-smiley' /> </p>

	<p>So yeah, I have this control written from an earlier project which creates a task list with live preview images. When I say &#8220;live previews&#8221; I mean actually live, like on the Vista Alt+Tab: if there&#8217;s a video playing, you can watch it in there (of course, it dies a horrible death on XP, but I have other code for that).  </p>

	<p>As a side note  the TaskBar2.panel stuff is from the <span class="caps">TINS</span> release I made months ago, there&#8217;s several fun .panel files in there &#8230; but you have to load a bunch of the <span class="caps">TINS</span> assemblies first, I didn’t distinguish which ones, I just did:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #660033;">cd</span> C:\Users\Joel\Projects\TechDemos\TINS\bin\Release<br />
<span style="color: #660033;">ls</span> <span style="color: #66cc66;">*</span>.<span style="color: #003366;">dll</span>,<span style="color: #66cc66;">*</span>.<span style="color: #003366;">exe</span> <span style="color: #66cc66;">|</span> <span style="color: #66cc66;">%</span> <span style="color: #333;">&#123;</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span>System.<span style="color: #003366;">Reflection</span>.<span style="color: #003366;">Assembly</span><span style="color: #333;">&#93;</span></span>::<span style="color: #003366;">LoadFrom</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$_</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#125;</span><br />
<span style="color: #660033; font-weight: bold;">$Host</span>.<span style="color: #003366;">PrivateData</span>.<span style="color: #003366;">XamlUI</span>.<span style="color: #003366;">LoadXaml</span><span style="color: #333;">&#40;</span><span style="color: #009900;">&quot;C:\Users\Joel\Projects\TechDemos\TINS\bin\Release\Panels\TaskBar2.panel&quot;</span><span style="color: #333;">&#41;</span>;<br />
&nbsp;</div>

	<p>Of course, in .Net 3.5 the RichTextBox has the ability to go <code>ContentEnabled=&#34;true&#34;</code> which should allow those things to be actual controls you can click to switch active task, right-click to get a task menu, etc (although I haven&#8217;t tried that yet).  We might just have to upgrade  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=';-)' class='wp-smiley' /> </p>

	<p>Anyway, it&#8217;s past time for bed, so I&#8217;ll preempt complaints about how this isn&#8217;t ready for release yet with this quote: </p>

	<blockquote>
		<p>I don’t have to take this abuse from you; I’ve got hundreds of people waiting to abuse me. &#8212;  Bill Murray, “Ghostbusters”</p>
	</blockquote>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/a-quick-progress-update-on-poshconsole/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Lesson: Expectation Setting &#8211; Newton vs. Palm</title>
		<link>http://huddledmasses.org/lesson-expectation-setting-newton-vs-palm/</link>
		<comments>http://huddledmasses.org/lesson-expectation-setting-newton-vs-palm/#comments</comments>
		<pubDate>Mon, 09 Apr 2007 15:08:17 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Rants]]></category>
		<category><![CDATA[UserInterface]]></category>

		<guid isPermaLink="false">http://HuddledMasses.org/jaykul/lesson-expectation-setting-newton-vs-palm/</guid>
		<description><![CDATA[Visionary insights come from thinking more about human needs than technological possibilities. Ben Shneiderman Leonardo&#8217;s Laptop: Human needs and the new computing technologies Shneiderman writes about the success of Palm handhelds: The surprise was that users were willing to learn a variant of the English alphabet called Grafitti so that they could enter data &#8230; [...]]]></description>
			<content:encoded><![CDATA[	<blockquote>
		<p>Visionary insights come from thinking more about human needs than technological possibilities. <cite><strong>Ben Shneiderman</strong> Leonardo&#8217;s Laptop: Human needs and the new computing technologies</cite></p>
	</blockquote>

	<p>Shneiderman writes about the success of Palm handhelds:</p>

	<blockquote>
		<p>The surprise was that users were willing to learn a variant of the English alphabet called Grafitti so that they could enter data &#8230; [a great surprise] in light of the [earlier] failure of the Apple Newton, which offered recognition for handwritten words&#8230;. Most users feel responsible for Graffiti stroke recognition errors, whereas they tended to blame the Newton for word recognition failures.</p>
	</blockquote>

	<p>I would like to think that the lesson is something about the simplicity of the Graffiti stroke system, vs. the complexity of handwriting recognition &#8230; but I&#8217;ve used a Palm, and I haven&#8217;t found the Graffiti system is astonishingly accurate, so I think that Shneiderman has given us the right answer: users feel responsible.</p>

	<p>So, the real lesson is very simple, and it&#8217;s about expectation setting. Palm, by telling you up front that there was no handwriting recognition, but that instead you needed to communicate with the tool using these special strokes &#8230; put the burden on you.  Apple, by telling you the tool could read handwriting, put the burden on the tool.  Of course, there were obviously many other factors involved &#8212; and timing was certainly one of them.  But it seems that <strong>the important lesson</strong> is about <em>controlling the expectations of your users</em>.</p>

	<p>What we learned is that people are willing to put up with quirky software if they can be convinced that any problems are their own fault.  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=';-)' class='wp-smiley' />  Based on what we know about users, you would expect them to be annoyed when they first start using the tool at how hard it is (ok, I know it&#8217;s not <em>that</em> hard, I have a Palm &#8212; but it&#8217;s significantly harder than writing in a Moleskine).  Because of the way that it&#8217;s marketed, pitched, and explained &#8230; the users of the Palm blame themselves, and thus don&#8217;t get <em>angry</em> at the technology.</p>

	<p>Incidentally, this expectation stuff might help fans of Linux and Mac OS X understand the current computer marketplace. In fact, I&#8217;d wager that this is the only way that Windows&#8217; market share dominance can be attacked. If you can figure out how to advertise (without insulting Windows users) so as to raise expectations of software &#8230; you may be able to reach a point where the majority of people will become dissatisfied with their current platform.</p>

	<p>Technorati Tags: <a class="performancingtags" href="http://technorati.com/tag/lessons" rel="tag">lessons</a>, <a class="performancingtags" href="http://technorati.com/tag/newton" rel="tag">newton</a>, <a class="performancingtags" href="http://technorati.com/tag/apple" rel="tag">apple</a>, <a class="performancingtags" href="http://technorati.com/tag/palm" rel="tag">palm</a>, <a class="performancingtags" href="http://technorati.com/tag/user experience" rel="tag">user experience</a>, <a class="performancingtags" href="http://technorati.com/tag/friction" rel="tag">friction</a>, <a class="performancingtags" href="http://technorati.com/tag/expectations" rel="tag">expectations</a></p>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/lesson-expectation-setting-newton-vs-palm/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Vista Thumbnailer</title>
		<link>http://huddledmasses.org/vista-thumbnailer/</link>
		<comments>http://huddledmasses.org/vista-thumbnailer/#comments</comments>
		<pubDate>Thu, 01 Mar 2007 05:47:55 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[My Software]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[UserInterface]]></category>

		<guid isPermaLink="false">http://HuddledMasses.org/jaykul/vista-thumbnailer/</guid>
		<description><![CDATA[I&#8217;ve been crazy busy the last few weeks, but I&#8217;ve been really rather distracted from my main project and it&#8217;s past time I got back to it. To really feel like I&#8217;ve moved on, I need to write a bunch of articles here and publish a couple of cool apps and some source code so [...]]]></description>
			<content:encoded><![CDATA[	<p>I&#8217;ve been crazy busy the last few weeks, but I&#8217;ve been really rather distracted from <a href="/category/development/recommender/">my main project</a> and it&#8217;s past time I got back to it.  To really feel like I&#8217;ve moved on, I need to write a bunch of articles here and publish a couple of cool apps and some source code so that I can feel like I&#8217;ve reached a <em>release</em> point.  What I&#8217;ve been playing with in all this coding is the new technology that&#8217;s been coming out of Microsoft in the last couple months: <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx">PowerShell</a>, .<span class="caps">NET</span> 3 and <a href="http://wpf.netfx3.com/">WPF</a>, and <a href="http://WindowsVista.com">Windows Vista</a> so you may or may not actually be able to use these apps &#8230;</p>

	<p>The Vista Thumbnailer is a really slick little app that shows off some of what Vista can do &#8230; and some of what <span class="caps">WPF</span> can do.  I&#8217;ll write more about it in the morning, because there&#8217;s some interesting things I learned in the coding of it, but in the meantime, you can <a href="/downloads/VistaThumbnailer.7z">download Vista Thumbnailer</a> (7-Zip file with a config file). The only thing you really need to know is that there&#8217;s a hotkey: Win+X which will bring Thumbnailer back to the foreground even if it&#8217;s minimized in the tray.  I&#8217;ll make the hotkey configurable in version 2, but for now you&#8217;re stuck with Win+X.  The rest you can figure out from the screenshots (<a href="/images/WindowsVista/DWM_Thumbnailer.jpg">0</a> <a href="/images/WindowsVista/DWM_Thumbnailer1.jpg">1</a> <a href="/images/WindowsVista/DWM_Thumbnailer2.jpg">2</a>) and by reading the tooltips on the buttons.  </p>

	<p>Play around.  Let me know what you think.</p>

	<p>(Oh, <a href="/downloads/VistaThumbnailer_Source.7z">source code is here</a> with your usual choice of BSD/Ms-PL/<span class="caps">GPL</span>)</p>

	<p>[New] <strong>Edit:</strong> Continuing</p>

	<p>So &#8230; I&#8217;ve had a lot of questions about this app during it&#8217;s development (from a few friends in our <span class="caps">IRC</span> channel) so it&#8217;s clear that I should try to at least give the highlights of how it works.</p>

	<h3>Thumbnails </h3>

	<p>By now you&#8217;ve probably seen more than your fill of <a href="http://blogs.msdn.com/greg_schechter/archive/2006/03/05/544314.aspx">screenshots</a> of Windows Vista&#8217;s semi-transparent windows, and maybe you&#8217;ve had a chance to try Vista and seen these live-updating thumbnails on the Alt+Tab window, the Flip3D Win+Tab task switcher, and even the task-bar&#8217;s mouse-over (or Win+T) popups.  All of this is possible because of the way Windows does desktop compositing using a new <a href="http://msdn2.microsoft.com/en-us/library/aa969540.aspx" title="DWM">Desktop Window Manager</a>.  This <span class="caps">DWM</span> is actually a process which runs all the time when you have the &#8220;Aero&#8221; interface turned on, but it&#8217;s also an <span class="caps">API</span> exposed through dwmapi.dll &#8230; and one of the deepest fundamental changes to the way Windows work in Vista.  Let me quote from &#8220;one of the developers&#8221; blogs:</p>

	<blockquote>
		<p>... The way an application gets pixels on the screen has fundamentally changed. [In the past] applications were asked by Windows to paint their <em>visible</em> region, and they would paint directly to the buffer that was to be displayed by the video card.  With Windows Vista, applications are asked by Windows to paint their <em>entire surface</em> to an offscreen surface (alternatively known as a bitmap, buffer, or texture), and it&#8217;s up to the <span class="caps">DWM</span> to take all of these offscreen surfaces, and &#8220;compose&#8221; them together to the onscreen buffer.</p>
	</blockquote>

	<p>Because the windows are rendered &#8220;offscreen&#8221; and without regard to what part of the window might actually be visible &#8230; the visual representation can be reused in many places very cheaply &#8212; that is, since the visual representation is just a cached picture, there&#8217;s very little <span class="caps">CPU</span> cost involved in repainting it in many places &#8230; and the caching and repainting are easily handled by the <span class="caps">DWM</span>. Now, the <span class="caps">DWM</span> also does lots of other cool things, like using DirectX and hardware accelleration to do all this extra rendering of these cached surfaces, but despite appearances, this isn&#8217;t really an article about the <span class="caps">DWM</span> &#8212; we&#8217;re here to talk about <span class="caps">JUST</span> the thumbnails. Greg Schechter again:</p>

	<blockquote>
		<p>Thumbnails in the <span class="caps">DWM</span> are not static snapshots of a window.  They&#8217;re dynamic, constant connections between a thumbnail source window, and a place on a target window that that source window will get a &#8220;thumbnail rendering&#8221;. </p>
	</blockquote>

	<p>Basically, the <a href="http://blogs.msdn.com/greg_schechter/archive/2006/09/14/753605.aspx">APIs in the Desktop Window Manager</a> allow you to ask for a certain portion of one Window&#8217;s screen to be painted over (at some specified level of opacity) with all or part of the contents of another Window&#8217;s screen.  In every sense, the thumbnail is just as live and up-to-date as the actual window (since it is, in fact, being painted by the same process).</p>

	<p>So anyway, what I did is I took a &#8220;Thumbnail&#8221; class from <a href="http://11011.net/archives/000653.html">Douglas Stockwell</a> and enhanced it a bit &#8230; it&#8217;s still not quite perfect, but it&#8217;s trivially easy to use now: just pass it a Window handle and it hooks itself up and renders within it&#8217;s available bounds &#8212; mostly.</p>

	<h3>Glass Effect </h3>

	<p>The Vista Thumbnailer app also showcases a couple of other slick <span class="caps">API</span> features.  First, I added a couple of simple wrappers to <a href="http://msdn2.microsoft.com/en-us/library/aa969512.aspx">DwmExtendFrameIntoClientArea</a> and <a href="http://msdn2.microsoft.com/en-us/library/aa969518.aspx">DwmIsCompositionEnabled</a> so that you can easily get the translucent &#8220;glass&#8221; effect on your whole window, or write, for instance: <code>if( !NativeMethods.IsCompositionEnabled ) { MessageBox.Show( &#34;Sorry you can&#39;t use this feature!&#34; ); }</code> &#8230; </p>

	<h3>Hotkeys</h3>

	<p>I also wrote a global hotkey manager class which can be easily added to <span class="caps">WPF</span> applications that want to register global hotkeys.  VistaThumbnailer only has one, to focus it (you can change it by editing the .config file).</p>

	<p>These are global hotkeys, not like the &#8220;Ctrl+O&#8221; for opening a file, but like the Win+R to bring up a run dialog.  They&#8217;re registered through Window&#8217;s RegisterHotkey and UnregisterHotkey APIs, and you get notified of them via window messages (but my HotkeyManager class will handle the WM_HOTKEY message for you).  Here&#8217;s the short-form for how to use them (you can grab the source if you&#8217;d like to borrow the class, but I&#8217;ll be re-releasing this in a few days with some enhancements I&#8217;ll detail below).</p>

	<div class="csharp code csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">GeoCore.Win32</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Window1 <span style="color: #008000;">:</span> <span style="color: #000000;">System</span>.<span style="color: #0000FF;">Windows</span>.<span style="color: #0000FF;">Window</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #0600FF;">private</span> WPFHotkeyManager hkManager<span style="color: #008000;">;</span><br />
&nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// You need to keep a reference to the hotkey around</span><br />
&nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// Otherwise you won't know *which* hotkey was pressed!</span><br />
&nbsp; &nbsp;<span style="color: #0600FF;">private</span> Hotkey focusHotkey<span style="color: #008000;">;</span> &nbsp;<br />
<br />
&nbsp; &nbsp;<span style="color: #0600FF;">public</span> Window1<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// ... somewhere ... hook SourceInitialized (you can do it in the XAML)</span><br />
&nbsp; &nbsp; &nbsp; SourceInitialized <span style="color: #008000;">+=</span><a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> EventHandler<span style="color: #000000;">&#40;</span>Window1_SourceInitialized<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp;<span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp;<span style="color: #0600FF;">void</span> Window_SourceInitialized<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> sender, EventArgs e<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// make the whole window glassy, preserving the border</span><br />
&nbsp; &nbsp; &nbsp; NativeMethods.<span style="color: #0000FF;">TryExtendFrameIntoClientArea</span><span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span>, BorderThickness<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// create the hotkey manager in &quot;SourceInitialized&quot; because</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// it doesn't accept hotkeys anyway until the window is Initialized</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// -- this is because it needs a window handle.</span><br />
&nbsp; &nbsp; &nbsp; hkManager <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> WPFHotkeyManager<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">this</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// register for the hotkey event, otherwise ... nothing happens.</span><br />
&nbsp; &nbsp; &nbsp; hkManager.<span style="color: #0000FF;">HotkeyPressed</span> <span style="color: #008000;">+=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> WPFHotkeyManager.<span style="color: #0000FF;">HotkeyPressedEvent</span><span style="color: #000000;">&#40;</span>Hotkey_Pressed<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// create a hotkey -- remember, this is global (and be careful of the types here)</span><br />
&nbsp; &nbsp; &nbsp; focusKey <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Hotkey<span style="color: #000000;">&#40;</span>ModifierKeys.<span style="color: #0000FF;">Win</span>, Keys.<span style="color: #0000FF;">X</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// register the hotkey</span><br />
&nbsp; &nbsp; &nbsp; hkManager.<span style="color: #0000FF;">Register</span><span style="color: #000000;">&#40;</span>FocusKey<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp;<span style="color: #000000;">&#125;</span><br />
<br />
<br />
&nbsp; &nbsp;<span style="color: #0600FF;">void</span> Hotkey_Pressed<span style="color: #000000;">&#40;</span><span style="color: #000000;">System</span>.<span style="color: #0000FF;">Windows</span>.<span style="color: #0000FF;">Window</span> window, Hotkey hotkey<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// an action for each hotkey ....</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>hotkey.<span style="color: #0000FF;">Equals</span><span style="color: #000000;">&#40;</span>FocusKey<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Show<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> Activate<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span></div>

	<p>I want you to notice that I had to create my own Keys enumeration in this, because the <span class="caps">WPF</span> developers unbelievably <em>chose</em> to use an ordering for their Key enumeration which is <em>inconsistent with the Windows API</em> ordering. You <strong>could</strong> use the System.Windows.Forms enumeration, but that would require adding a reference to the Forms assembly in your <span class="caps">WPF</span> application just to get a silly enum!  I have to say that this seems to be  absolutely the <span class="caps">WORST</span> design decision I&#8217;ve seen in the .<span class="caps">NET</span> Framework &#8212; not only are they duplicating enumerations which exist elsewhere, but they couldn&#8217;t even copy-and-paste it in order?</p>

	<h3>Chrome Free Windows!</h3>

	<p>I thought I&#8217;d mention this, just in case you haven&#8217;t seen it before.  Irregular shaped windows are trivially easy to do in <span class="caps">WPF</span>. All you have to do is add the following attributes to your Window tag: WindowStyle=&#8220;None&#8221; AllowsTransparency=&#8220;True&#8221; Background=&#8220;Transparent&#8221;</p>

	<h2>Further Development</h2>

	<h3><span class="caps">DWM</span> <span class="caps">API</span> Thumbnails </h3>

	<p>There are still a few tweaks that need to be made to the Thumbnail Image control.  It doesn&#8217;t adjust when the source window is changed until you resize the window (because it doesn&#8217;t check the size except when <span class="caps">WPF</span> asks it to) and I&#8217;m not sure if it&#8217;s possible apart from re-checking the source window shape on a timer.  I&#8217;m a little afraid that I&#8217;m leaking the thumbnail window so I need to wrap them in a Safe Handle, and override OnUnloaded to clean up.</p>

	<p>I&#8217;m also curious if there&#8217;s a way, in the <span class="caps">DWM</span> api, to get these thumbnails batched into a video or saved to an image handle so you can use them for screenshots &#8230; I&#8217;ll have to take some additional time to research that.</p>

	<h3>Hotkeys</h3>

	<p>There&#8217;s also some changes I want to make to the way my hotkeys class works.  Right now you have to hang on to your Hotkey objects in order to be able to process the hotkey events correctly (unless you only have a single Hotkey) ... and it&#8217;s not possible to use a switch statement on the event, so if you have a lot of hotkeys, it gets a bit ridiculous.  I initially did this because I didn&#8217;t like trusting the user to come up with unique Hotkey Id&#8217;s, but now I&#8217;m thinking that it might be better if you could use just the Ids and create yourself an enumeration for them.  I need to play with this, because one of my key use scenarios is having windowless plugins be able to register and process their own hotkeys &#8212; which makes enums not completely safe, because different pieces of code are dynamically creating hotkeys.</p>

	<p>I also don&#8217;t like not being able to register the hotkeys during construction.  My first attempt to fix that is a version of this HotkeyManager (which I&#8217;ll release shortly) that creates it&#8217;s own window &#8212; this means you don&#8217;t have to worry about calling it in SourceInitialized, but it&#8217;s a pretty expensive fix if you&#8217;ve already got a window.  I&#8217;m going to try doing it another way &#8212; where hotkeys registered before the handle is initialized will be queued for registration later, and this should be a better solution in most cases.  I do have a System.Windows.Forms version  (which I will release with all the other versions) that doesn&#8217;t have this problem (because with Windows.Forms you get a window handle on construction), but again, I don&#8217;t like including that whole assembly for something fairly trivial.</p>

	<p>Hopefully I&#8217;m going to get better at these writeups as time goes on, because I feel like this one is a bit rambling. Questions and comments are welcome!</p>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/vista-thumbnailer/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

