<?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; XAML</title>
	<atom:link href="http://huddledmasses.org/tag/xaml/feed/" rel="self" type="application/rss+xml" />
	<link>http://huddledmasses.org</link>
	<description>You can do more than breathe for free...</description>
	<lastBuildDate>Fri, 27 Apr 2012 05:42:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
<cloud domain='huddledmasses.org' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
		<item>
		<title>Creating WPF UIs for PowerShell with PowerBoots and Visual Studio WPF Designer</title>
		<link>http://huddledmasses.org/creating-wpf-uis-for-powershell-with-powerboots-and-visual-studio-wpf-designer/</link>
		<comments>http://huddledmasses.org/creating-wpf-uis-for-powershell-with-powerboots-and-visual-studio-wpf-designer/#comments</comments>
		<pubDate>Fri, 30 Apr 2010 03:09:42 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[PowerBoots]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[XAML]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1433</guid>
		<description><![CDATA[I&#8217;ve had several people ask me how PowerBoots compares to PrimalForms, or ask for a visual designer for PowerBoots. I usually answer something along the lines of the fact that Microsoft has already created a very good WPF/XAML designer in Visual Studio (including the free Express editions), particularly in 2010, so I don&#8217;t see why [...]]]></description>
			<content:encoded><![CDATA[	<p>I&#8217;ve had several people ask me how <a href="http://boots.codeplex.com/">PowerBoots</a> compares to PrimalForms, or ask for a visual designer for PowerBoots. I usually answer something along the lines of the fact that Microsoft has already created a very good WPF/<span class="caps">XAML</span> designer in Visual Studio (including the free Express editions), particularly in 2010, so I don&#8217;t see why I should duplicate their efforts.  However, up until now I haven&#8217;t written or published any sort of walk through about how to make that work &#8230; </p>

	<p>So crank up your <a href="http://msdn.com/express/">Visual Studio Express Edition</a> and make yourself some user interfaces!</p>

	<p>Basically, you just create a <span class="caps">WPF</span> project in C# or VB.Net or whatever &#8230; I chose to name my project &#8220;XamlForBoots&#8221; &#8212; your project will start off with an empty MainWindow.xaml file which will look something like this:</p>

	<a href="/wordpress/wp-content/uploads/2010/03/NewMainWindow.xaml_.png"><img src="/wordpress/wp-content/uploads/2010/03/NewMainWindow.xaml_-300x199.png" alt="This is what the Main Window looks like at first" title="NewMainWindow.xaml" width="300" height="199" class="size-medium wp-image-1434" /></a>

	<p>After you drag a few more controls onto the form, and you&#8217;ve created a complete user interface, you should delete the x:Class attribute.  You need to make sure you know the <em>name</em> of the controls you want to interact with, so you might end up with something like this (I used the property pane, which you can&#8217;t see in this shot, to name each textbox and the button):</p>

	<a href="/wordpress/wp-content/uploads/2010/04/FinishedMainWindow.xaml_.png"><img src="/wordpress/wp-content/uploads/2010/04/FinishedMainWindow.xaml_-300x212.png" alt="Once you&#039;ve dragged some controls onto it" title="FinishedMainWindow.xaml" width="300" height="212" class="size-medium wp-image-1439" /></a>

	<p>At this point, we&#8217;re ready to drop into PowerShell and write some script. Now &#8230; there is one catch here.  The first script I&#8217;m going to show you here is for <strong>PowerBoots 0.3</strong> (which will be the first release candidate for a gold 1.0 release, and will be out soon&#8482;). However, I&#8217;ll post below some code to make it work on the current release, but it requires an external function.</p>

	<p>So, in the next release, you can just do something like this:</p>

	<div class="posh code posh" style="font-family:monospace;"><span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">BootsWindow</span></span> <span style="color: #000066;">-FileTemplate</span> <span style="color: #660033; font-weight: bold;">$pwd</span>\MainWindow.<span style="color: #003366;">xaml</span> <span style="color: #000066;">-On_Loaded</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Register-<span style="font-style: normal;">BootsEvent</span></span> <span style="color: #000066;">-InputObject</span> <span style="color: #660033; font-weight: bold;">$Calculate</span> <span style="color: #000066;">-EventName</span> Click <span style="color: #000066;">-Action</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Total</span>.<span style="color: #003366;">Text</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">'${0:n2}'</span> <span style="color: #000066;">-f</span> <span style="color: #333;">&#40;</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$Miles</span>.<span style="color: #003366;">Text</span> <span style="color: #000066;">-as</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">Double</span><span style="color: #333;">&#93;</span></span><span style="color: #333;">&#41;</span> <span style="color: #66cc66;">/</span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$Mpg</span>.<span style="color: #003366;">Text</span> <span style="color: #000066;">-as</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">Double</span><span style="color: #333;">&#93;</span></span><span style="color: #333;">&#41;</span> <span style="color: #66cc66;">*</span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$Cost</span>.<span style="color: #003366;">Text</span> <span style="color: #000066;">-as</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">Double</span><span style="color: #333;">&#93;</span></span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
<span style="color: #333;">&#125;</span></div>

	<p>The key thing you&#8217;re supposed to notice here is that the named controls in the <span class="caps">XAML</span> are automatically surfaced as variables in the event handlers, and all you have to do is write your logic and hook it up to the controls. We provide a <code>Register-BootsEvent</code> cmdlet which is like Register-ObjectEvent except that it executes the event handlers on the UI thread (so they can do things to the UI) instead of in a new runspace, but basically it&#8217;s like calling <code>Add_Click</code>. Of course, you <strong>can</strong> use Register-ObjectEvent if you just want to spin off PowerShell tasks that don&#8217;t read/write the UI.</p>

	<h3>Backwards compatibility</h3>

	<p>To get this to work in the current release, you need a function <code>Export-NamedControl</code> to create variables for each of those named controls as variables. Once you&#8217;ve defined that function, you can write code very much like I did before, but you have to <em>call Export-NamedControl yourself</em>, and you won&#8217;t have that Register-BootsEvent cmdlet. You don&#8217;t really need it for this anyway, so that&#8217;s not a big deal, at least in this case. Here&#8217;s the function. and the actual code to create the window.  You can just paste this into a script file:</p>

	<div class="posh code posh" style="font-family:monospace;"><span style="color: #666699; font-weight: bold;">function</span> global:<span style="color: #0066cc; font-style: italic;">Export-<span style="font-style: normal;">NamedControl</span></span> <span style="color: #333;">&#123;</span><br />
<span style="color: #333;">&#91;</span>CmdletBinding<span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><br />
<span style="color: #666699; font-weight: bold;">param</span><span style="color: #333;">&#40;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#91;</span>Parameter<span style="color: #333;">&#40;</span>ValueFromPipeline<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$true</span>, Position<span style="color: #66cc66;">=</span><span style="color: #cc66cc;">1</span>, Mandatory<span style="color: #66cc66;">=</span><span style="color: #660033; font-weight: bold;">$true</span><span style="color: #333;">&#41;</span><span style="color: #333;">&#93;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$Root</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$BootsWindow</span><br />
<span style="color: #333;">&#41;</span><br />
<span style="color: #666699; font-weight: bold;">process</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Invoke-<span style="font-style: normal;">BootsWindow</span></span> <span style="color: #660033; font-weight: bold;">$Root</span> <span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$control</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$BootsWindow</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">while</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$control</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$control</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$control</span> <span style="color: #66cc66;">|</span> ForEach<span style="color: #66cc66;">-</span>Object <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Element</span> <span style="color: #66cc66;">=</span> <span style="color: #660033; font-weight: bold;">$_</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span><span style="color: #66cc66;">!</span><span style="color: #660033; font-weight: bold;">$Element</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span> <span style="color: #666699; font-weight: bold;">return</span> <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Verbose</span></span> <span style="color: #009900;">&quot;This $($Element.GetType().Name) is $Element&quot;</span><br />
&nbsp; <br />
&nbsp; &nbsp; &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;">$Element</span>.<span style="color: #003366;">Name</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Write-<span style="font-style: normal;">Verbose</span></span> <span style="color: #009900;">&quot;Defining $($Element.Name) = $Element&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Set-<span style="font-style: normal;">Variable</span></span> <span style="color: #009900;">&quot;$($Element.Name)&quot;</span> <span style="color: #660033; font-weight: bold;">$Element</span> <span style="color: #000066;">-Scope</span> <span style="color: #cc66cc;">2</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #333;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">## Return all the child controls ...</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @<span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$Element</span>.<span style="color: #003366;">Children</span><span style="color: #333;">&#41;</span> <span style="color: #66cc66;">+</span> @<span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$Element</span>.<span style="color: #003366;">Child</span><span style="color: #333;">&#41;</span> <span style="color: #66cc66;">+</span> @<span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$Element</span>.<span style="color: #003366;">Content</span><span style="color: #333;">&#41;</span> <span style="color: #66cc66;">+</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @<span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$Element</span>.<span style="color: #003366;">Items</span><span style="color: #333;">&#41;</span> <span style="color: #66cc66;">+</span> @<span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$Element</span>.<span style="color: #003366;">Inlines</span><span style="color: #333;">&#41;</span> <span style="color: #66cc66;">+</span> @<span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$Element</span>.<span style="color: #003366;">Blocks</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #333;">&#125;</span><br />
&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><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;">####################################################################################################################</span><br />
<span style="color: #666666; font-style: italic;">## Create the window and hook the click. Make sure to use full paths</span><br />
<span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">BootsWindow</span></span> <span style="color: #333;">&#123;</span><span style="color: #333;">&#125;</span> <span style="color: #000066;">-FileTemplate</span> <span style="color: #660033; font-weight: bold;">$pwd</span>\MainWindow.<span style="color: #003366;">xaml</span> <span style="color: #000066;">-On_Loaded</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Export-<span style="font-style: normal;">NamedControl</span></span> <span style="color: #000066;">-Root</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><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$Calculate</span>.<span style="color: #003366;">Add_Click</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#123;</span> <br />
&nbsp; &nbsp; &nbsp; <span style="color: #660033; font-weight: bold;">$Total</span>.<span style="color: #003366;">Text</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">'${0:n2}'</span> <span style="color: #000066;">-f</span> <span style="color: #333;">&#40;</span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$Miles</span>.<span style="color: #003366;">Text</span> <span style="color: #000066;">-as</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">Double</span><span style="color: #333;">&#93;</span></span><span style="color: #333;">&#41;</span> <span style="color: #66cc66;">/</span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$Mpg</span>.<span style="color: #003366;">Text</span> <span style="color: #000066;">-as</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">Double</span><span style="color: #333;">&#93;</span></span><span style="color: #333;">&#41;</span> <span style="color: #66cc66;">*</span> <span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$Cost</span>.<span style="color: #003366;">Text</span> <span style="color: #000066;">-as</span> <span style="color: #003366; font-weight: bold;"><span style="color: #333;">&#91;</span><span style="color: #003366; font-weight: bold;">Double</span><span style="color: #333;">&#93;</span></span><span style="color: #333;">&#41;</span><span style="color: #333;">&#41;</span><br />
&nbsp; &nbsp;<span style="color: #333;">&#125;</span><span style="color: #333;">&#41;</span><br />
<span style="color: #333;">&#125;</span></div>

	<p>And in order to try <strong>my</strong> demo, you&#8217;re going to need that MainWindow.xaml file:</p>

	<div class="xml code xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Window</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">xmlns:x</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000066;">Title</span>=<span style="color: #ff0000;">&quot;Trip Cost&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Grid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Grid.RowDefinitions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;RowDefinition</span> <span style="color: #000066;">Height</span>=<span style="color: #ff0000;">&quot;28&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;RowDefinition</span> <span style="color: #000066;">Height</span>=<span style="color: #ff0000;">&quot;28&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;RowDefinition</span> <span style="color: #000066;">Height</span>=<span style="color: #ff0000;">&quot;28&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;RowDefinition</span> <span style="color: #000066;">Height</span>=<span style="color: #ff0000;">&quot;28&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Grid.RowDefinitions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Grid.ColumnDefinitions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ColumnDefinition</span> <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;110&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;ColumnDefinition</span> <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;*&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Grid.ColumnDefinitions<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Label</span> <span style="color: #000066;">Content</span>=<span style="color: #ff0000;">&quot;Miles&quot;</span> <span style="color: #000066;">VerticalAlignment</span>=<span style="color: #ff0000;">&quot;Top&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBox</span> <span style="color: #000066;">Grid.Column</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000066;">Height</span>=<span style="color: #ff0000;">&quot;23&quot;</span> <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;200&quot;</span> <span style="color: #000066;">Name</span>=<span style="color: #ff0000;">&quot;miles&quot;</span> <span style="color: #000066;">HorizontalAlignment</span>=<span style="color: #ff0000;">&quot;Stretch&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Label</span> <span style="color: #000066;">Grid.Row</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000066;">Content</span>=<span style="color: #ff0000;">&quot;Miles per Gallon&quot;</span> <span style="color: #000066;">VerticalAlignment</span>=<span style="color: #ff0000;">&quot;Top&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBox</span> <span style="color: #000066;">Grid.Column</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000066;">Grid.Row</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000066;">Height</span>=<span style="color: #ff0000;">&quot;23&quot;</span> <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;200&quot;</span> <span style="color: #000066;">HorizontalAlignment</span>=<span style="color: #ff0000;">&quot;Stretch&quot;</span> <span style="color: #000066;">Name</span>=<span style="color: #ff0000;">&quot;mpg&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Label</span> <span style="color: #000066;">Grid.Row</span>=<span style="color: #ff0000;">&quot;2&quot;</span> <span style="color: #000066;">Content</span>=<span style="color: #ff0000;">&quot;Cost per Gallon&quot;</span> &nbsp;<span style="color: #000066;">VerticalAlignment</span>=<span style="color: #ff0000;">&quot;Top&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBox</span> <span style="color: #000066;">Grid.Column</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000066;">Grid.Row</span>=<span style="color: #ff0000;">&quot;2&quot;</span> <span style="color: #000066;">Height</span>=<span style="color: #ff0000;">&quot;23&quot;</span> <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;200&quot;</span> <span style="color: #000066;">HorizontalAlignment</span>=<span style="color: #ff0000;">&quot;Stretch&quot;</span> <span style="color: #000066;">Name</span>=<span style="color: #ff0000;">&quot;cost&quot;</span> &nbsp;<span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Button</span> <span style="color: #000066;">Name</span>=<span style="color: #ff0000;">&quot;calculate&quot;</span> <span style="color: #000066;">Grid.Row</span>=<span style="color: #ff0000;">&quot;3&quot;</span> <span style="color: #000066;">Content</span>=<span style="color: #ff0000;">&quot;_Calculate&quot;</span> <span style="color: #000066;">HorizontalAlignment</span>=<span style="color: #ff0000;">&quot;Center&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;TextBlock</span> <span style="color: #000066;">Grid.Column</span>=<span style="color: #ff0000;">&quot;1&quot;</span> <span style="color: #000066;">Grid.Row</span>=<span style="color: #ff0000;">&quot;3&quot;</span> <span style="color: #000066;">Height</span>=<span style="color: #ff0000;">&quot;23&quot;</span> <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;200&quot;</span> <span style="color: #000066;">HorizontalAlignment</span>=<span style="color: #ff0000;">&quot;Stretch&quot;</span> <span style="color: #000066;">Name</span>=<span style="color: #ff0000;">&quot;total&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Grid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Window<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></div>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/creating-wpf-uis-for-powershell-with-powerboots-and-visual-studio-wpf-designer/feed/</wfw:commentRss>
		<slash:comments>2</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>WPF Window &#8220;Native&#8221; Behavior: Snap-To Screen Edges</title>
		<link>http://huddledmasses.org/wpf-windows-that-snap-to-screen-edges/</link>
		<comments>http://huddledmasses.org/wpf-windows-that-snap-to-screen-edges/#comments</comments>
		<pubDate>Tue, 14 Oct 2008 05:06:36 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[GUI]]></category>
		<category><![CDATA[Hacking]]></category>
		<category><![CDATA[NativeBehaviors]]></category>
		<category><![CDATA[Win32]]></category>
		<category><![CDATA[WM_WINDOWPOSCHANGING]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[XAML]]></category>

		<guid isPermaLink="false">http://HuddledMasses.org/?p=718</guid>
		<description><![CDATA[There are many desirable behaviors for Windows applications that are just much harder to do than they should be with the tools that Microsoft has provided in the .Net Framework. In WPF, many of these behaviors are even harder to create than in Windows Forms because the necessary hooks take a bit more work to [...]]]></description>
			<content:encoded><![CDATA[	<p>There are many desirable behaviors for Windows applications that are just much harder to do than they should be with the tools that Microsoft has provided in the .Net Framework. In <a href="http://en.wikipedia.org/wiki/Windows_Presentation_Foundation" title="Windows Presentation Foundation" rel="wikipedia" class="zem_slink">WPF</a>, many of these behaviors are even harder to create than in <a href="http://en.wikipedia.org/wiki/Windows_Forms" title="Windows Forms" rel="wikipedia" class="zem_slink">Windows Forms</a> because the necessary hooks take a bit more work to write (thanks to the fact that there&#8217;s no window handles, so dropping down to &#8220;Native&#8221; code is harder.  Luckily, <span class="caps">WPF</span> Attached Properties give us a whole new way to reuse these behaviors once they&#8217;re written.</p>

	<p>I&#8217;ve started working on a few classes I&#8217;m calling NativeBehaviors, because they rely on intercepting the native <em>Window Message</em> processing, and I&#8217;ve put together a simple framework to let me write them, which I thought I would share.  The first one I wrote is a SnapToBehavior, which implements a feature that people seem to be constantly re-implementing in apps. Basically, it makes a window snap to the screen edge when it gets close (and prevents them from being moved off-screen).  I&#8217;ve also written a Global HotkeysBehavior which lets you register Hotkeys that work whenever your app is running (even if another app is active) so you can create a Hotkey to hide your app and show it, or whatever.</p>

	<p>In the rest of this article I&#8217;ll show you how to achieve this in <span class="caps">WPF</span> using my base NativeBehavior class, and how to use it on a Window. Since some of you won&#8217;t really care how it works, let&#8217;s start with:</p>

	<h3>How to make your <span class="caps">WPF</span> Windows snap to screen edges in 3 easy steps:</h3>

	<p>Step 1. Add a reference to the Huddled.Wpf.Interop library.<br />
Step 2. Add my Xml namespace to your root Window element<br />
Step 3. Paste three lines from below&#8230;.</p>

	<div class="xml code xml" style="font-family:monospace;"><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Window</span> <span style="color: #000066;">x:Class</span>=<span style="color: #ff0000;">&quot;GlobalHotkeys.DemoWindow&quot;</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;DemoWindow&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000066;">Title</span>=<span style="color: #ff0000;">&quot;GlobalHotkeys&quot;</span> <span style="color: #000066;">Height</span>=<span style="color: #ff0000;">&quot;300&quot;</span> <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;300&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000066;">xmlns:x</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000066;">xmlns:huddled</span>=<span style="color: #ff0000;">&quot;http://schemas.huddledmasses.org/wpf&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
<span style="color: #808080; font-style: italic;">&lt;!-- You just add a reference to the library, add the &quot;huddled&quot; namespace, and paste: --&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;huddled:Native.Behaviors<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;huddled:SnapToBehavior</span> <span style="color: #000066;">SnapDistance</span>=<span style="color: #ff0000;">&quot;20&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/huddled:Native.Behaviors<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #808080; font-style: italic;">&lt;!-- The rest of your window can be whatever you like. --&gt;</span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Grid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Label</span> <span style="color: #000066;">Content</span>=<span style="color: #ff0000;">&quot;Drag this window near the screen edges&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Grid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Window<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp;</div>

	<p>I should point out the &#8220;SnapDistance&#8221; property of the SnapToBehavior is actually a <span class="caps">WPF</span> &#8220;Thickness&#8221; which lets you specify the distance from the screen edge as a single number to use on all sides, or as a separate number for each side: Left, Top, Right, Bottom. And that&#8217;s pretty much all there is to know.</p>

	<h3>How to implement a NativeBehavior.</h3>

	<p>The implementation of the SnapToBehavior is actually ridiculously simple, given the NativeBehavior framework.  I simply created a SnapToBehavior class which derives from NativeBehavior, and implemented the single mandatory method:</p>

	<div class="csharp code csharp" style="font-family:monospace;"><br />
<span style="color: #008080; font-style: italic;">/// &lt;summary&gt;</span><br />
<span style="color: #008080; font-style: italic;">/// Gets the MessageMappings for this behavior:</span><br />
<span style="color: #008080; font-style: italic;">/// A single mapping of a handler for WM_WINDOWPOSCHANGING.</span><br />
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span><br />
<span style="color: #008080; font-style: italic;">/// &lt;returns&gt;An enumerable collection of MessageMapping objects.&lt;/returns&gt;</span><br />
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">override</span> IEnumerable<span style="color: #008000;">&lt;</span>MessageMapping<span style="color: #008000;">&gt;</span> GetHandlers<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp;yield <span style="color: #0600FF;">return</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> MessageMapping<span style="color: #000000;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; NativeMethods.<span style="color: #0000FF;">WindowMessage</span>.<span style="color: #0000FF;">WindowPositionChanging</span>, <span style="color: #008080; font-style: italic;">// the message</span><br />
&nbsp; &nbsp; &nbsp; OnPreviewPositionChange<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// the delegate which will handle that message</span><br />
<span style="color: #000000;">&#125;</span><br />
&nbsp;</div>

	<p>When my new behavior is added to the Behaviors collection, my handler will be registered, and whenever the WM_WINDOWPOSCHANGING message arrives, it will be called.  Now I defined a <a href="http://msdn.microsoft.com/en-us/library/system.windows.dependencyproperty.aspx">DependencyProperty</a> for the SnapDistance, so that you could set that in <span class="caps">XAML</span>.  It&#8217;s extremely simple, and VisualStudio has a built-in snippet for dependency properties, but here&#8217;s the code:</p>

	<div class="csharp code csharp" style="font-family:monospace;"><br />
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">readonly</span> DependencyProperty SnapDistanceProperty <span style="color: #008000;">=</span><br />
&nbsp; &nbsp;DependencyProperty.<span style="color: #0000FF;">Register</span><span style="color: #000000;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #666666;">&quot;SnapDistance&quot;</span>, &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// The name of the property (must match)</span><br />
&nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?q=typeof+msdn.microsoft.com"><span style="color: #008000;">typeof</span></a><span style="color: #000000;">&#40;</span>Thickness<span style="color: #000000;">&#41;</span>, &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #008080; font-style: italic;">// The type of the values</span><br />
&nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?q=typeof+msdn.microsoft.com"><span style="color: #008000;">typeof</span></a><span style="color: #000000;">&#40;</span>SnapToBehavior<span style="color: #000000;">&#41;</span>, &nbsp; <span style="color: #008080; font-style: italic;">// The type this property shows up on</span><br />
&nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> UIPropertyMetadata<span style="color: #000000;">&#40;</span><a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Thickness<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">20</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #008080; font-style: italic;">// The default value</span><br />
&nbsp; &nbsp;<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #0600FF;">public</span> Thickness SnapDistance<br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp;get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> <span style="color: #000000;">&#40;</span>Thickness<span style="color: #000000;">&#41;</span>GetValue<span style="color: #000000;">&#40;</span>SnapDistanceProperty<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp;set <span style="color: #000000;">&#123;</span> SetValue<span style="color: #000000;">&#40;</span>SnapDistanceProperty, value<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span><br />
&nbsp;</div>

	<p>Once I have that, the last piece of the puzzle is to actually handle the window position changing message (think of it as an event, if you&#8217;re not used to Win32 message-based programming).</p>

	<p>The basics of handling WM_WINDOWPOSCHANGING is that you get a structure passed in which has the <strong>proposed</strong> position of the Window, (including it&#8217;s z-index related to other apps) and you can basically tweak that however you like.  Obviously there are lots of possibilities for this single message: always-on-bottom windows, undraggable windows, etc., but in our case we&#8217;re just concerned about how close we are to the edge.</p>

	<p>We use the SystemParameters class to get the VirtualScreenLeft, VirtualScreenTop, and VirtualScreenWidth and VirtualScreenHeight which define the rectangle we&#8217;ll use for snapping.  In the case of non-rectangular arrangements of multiple monitors this isn&#8217;t quite sufficient, but for our example anything more would be a distraction.  Then we just check to see if the proposed position is within the &#8220;SnapDistance&#8221; of any of the edges, and if so, we change the position to be against the edge.  That&#8217;s really all there is to it.</p>

	<p>If you look at the code example below you&#8217;ll see I have to &#8220;Marshal&#8221; the WindowPosition structure in and out of an IntPtr which is passed in the WindowMessage &#8230; that&#8217;s the downside of intercepting window messages that the framework doesn&#8217;t already translate for us, but in this particular case, it&#8217;s actually fairly trivial.</p>

	<div class="csharp code csharp" style="font-family:monospace;"><br />
<span style="color: #008080; font-style: italic;">/// &lt;summary&gt;lParam for WindowPositionChanging</span><br />
<span style="color: #008080; font-style: italic;">/// &lt;/summary&gt;</span><br />
<span style="color: #000000;">&#91;</span>StructLayout<span style="color: #000000;">&#40;</span>LayoutKind.<span style="color: #0000FF;">Sequential</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span><br />
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">struct</span> WindowPosition<br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #0600FF;">public</span> IntPtr Handle<span style="color: #008000;">;</span><br />
&nbsp; &nbsp;<span style="color: #0600FF;">public</span> IntPtr HandleInsertAfter<span style="color: #008000;">;</span><br />
&nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> Left<span style="color: #008000;">;</span><br />
&nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> Top<span style="color: #008000;">;</span><br />
&nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> Width<span style="color: #008000;">;</span><br />
&nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> Height<span style="color: #008000;">;</span><br />
&nbsp; &nbsp;<span style="color: #0600FF;">public</span> WindowPositionFlags Flags<span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> Right <span style="color: #000000;">&#123;</span> get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> Left <span style="color: #008000;">+</span> Width<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span> <span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp;<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">int</span> Bottom <span style="color: #000000;">&#123;</span> get <span style="color: #000000;">&#123;</span> <span style="color: #0600FF;">return</span> Top <span style="color: #008000;">+</span> Height<span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span> <span style="color: #000000;">&#125;</span><br />
<span style="color: #000000;">&#125;</span> &nbsp; <br />
<br />
&nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">private</span> IntPtr OnPreviewPositionChange<span style="color: #000000;">&#40;</span>IntPtr wParam, IntPtr lParam, <span style="color: #0600FF;">ref</span> <span style="color: #FF0000;">bool</span> handled<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #FF0000;">bool</span> updated <span style="color: #008000;">=</span> false<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;var windowPosition <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>NativeMethods.<span style="color: #0000FF;">WindowPosition</span><span style="color: #000000;">&#41;</span>Marshal.<span style="color: #0000FF;">PtrToStructure</span><span style="color: #000000;">&#40;</span>lParam, <a href="http://www.google.com/search?q=typeof+msdn.microsoft.com"><span style="color: #008000;">typeof</span></a><span style="color: #000000;">&#40;</span>NativeMethods.<span style="color: #0000FF;">WindowPosition</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>windowPosition.<span style="color: #0000FF;">Flags</span> <span style="color: #008000;">&amp;</span> NativeMethods.<span style="color: #0000FF;">WindowPositionFlags</span>.<span style="color: #0000FF;">NoMove</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">==</span> <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// If we use the WPF SystemParameters, these should be &quot;Logical&quot; pixels</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Rect validArea <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Rect<span style="color: #000000;">&#40;</span>SystemParameters.<span style="color: #0000FF;">VirtualScreenLeft</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SystemParameters.<span style="color: #0000FF;">VirtualScreenTop</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SystemParameters.<span style="color: #0000FF;">VirtualScreenWidth</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SystemParameters.<span style="color: #0000FF;">VirtualScreenHeight</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Rect snapToBorder <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> Rect<span style="color: #000000;">&#40;</span>SystemParameters.<span style="color: #0000FF;">VirtualScreenLeft</span> <span style="color: #008000;">+</span> SnapDistance.<span style="color: #0000FF;">Left</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SystemParameters.<span style="color: #0000FF;">VirtualScreenTop</span> <span style="color: #008000;">+</span> SnapDistance.<span style="color: #0000FF;">Top</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SystemParameters.<span style="color: #0000FF;">VirtualScreenWidth</span> <span style="color: #008000;">-</span> <span style="color: #000000;">&#40;</span>SnapDistance.<span style="color: #0000FF;">Left</span> <span style="color: #008000;">+</span> SnapDistance.<span style="color: #0000FF;">Right</span><span style="color: #000000;">&#41;</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SystemParameters.<span style="color: #0000FF;">VirtualScreenHeight</span> <span style="color: #008000;">-</span> <span style="color: #000000;">&#40;</span>SnapDistance.<span style="color: #0000FF;">Top</span> <span style="color: #008000;">+</span> SnapDistance.<span style="color: #0000FF;">Bottom</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Enforce left boundary</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>windowPosition.<span style="color: #0000FF;">Left</span> <span style="color: #008000;">&lt;</span> snapToBorder.<span style="color: #0000FF;">Left</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;windowPosition.<span style="color: #0000FF;">Left</span> <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span><span style="color: #000000;">&#41;</span>validArea.<span style="color: #0000FF;">Left</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;updated <span style="color: #008000;">=</span> true<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Enforce top boundary</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>windowPosition.<span style="color: #0000FF;">Top</span> <span style="color: #008000;">&lt;</span> snapToBorder.<span style="color: #0000FF;">Y</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;windowPosition.<span style="color: #0000FF;">Top</span> <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span><span style="color: #000000;">&#41;</span>validArea.<span style="color: #0000FF;">Top</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;updated <span style="color: #008000;">=</span> true<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Enforce right boundary</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>windowPosition.<span style="color: #0000FF;">Right</span> <span style="color: #008000;">&gt;</span> snapToBorder.<span style="color: #0000FF;">Right</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;windowPosition.<span style="color: #0000FF;">Left</span> <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#40;</span>validArea.<span style="color: #0000FF;">Right</span> <span style="color: #008000;">-</span> windowPosition.<span style="color: #0000FF;">Width</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;updated <span style="color: #008000;">=</span> true<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">// Enforce bottom boundary</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>windowPosition.<span style="color: #0000FF;">Bottom</span> <span style="color: #008000;">&gt;</span> snapToBorder.<span style="color: #0000FF;">Bottom</span><span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;windowPosition.<span style="color: #0000FF;">Top</span> <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#40;</span>validArea.<span style="color: #0000FF;">Bottom</span> <span style="color: #008000;">-</span> windowPosition.<span style="color: #0000FF;">Height</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;updated <span style="color: #008000;">=</span> true<span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>updated<span style="color: #000000;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Marshal.<span style="color: #0000FF;">StructureToPtr</span><span style="color: #000000;">&#40;</span>windowPosition, lParam, <span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #000000;">&#125;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #0600FF;">return</span> IntPtr.<span style="color: #0000FF;">Zero</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000;">&#125;</span><br />
&nbsp;</div>

	<h3>Download it, or get the source code</h3>

	<p>If you&#8217;d like, you can download the current <a href='http://HuddledMasses.org/wordpress/wp-content/uploads/2008/10/huddledinteropwpf.dll'>Huddled Interop for <span class="caps">WPF</span> library</a> right now, or you can check out the source from CodePlex <a href="https://poshconsole.svn.codeplex.com/svn/trunk/Huddled/">SVN</a> or just download the <a href="http://www.codeplex.com/PoshConsole/SourceControl/ListDownloadableCommits.aspx">most recent commit</a> (You are only interested in the &#8220;Huddled&#8221; project which is in the &#8220;trunk&#8221;, not the &#8220;trunk-3.5&#8221;).  <span class="emph1">The source is licensed freely under the <span class="caps">BSD</span> or <span class="caps">GPL</span> v2</span> (and a few other licenses, see the top of the source code files).</p>

	<p>Either way you&#8217;ll get not just the SnapToBehavior but also the global HotkeysBehavior, and the Native Behaviors framework which I&#8217;ll write more about later, but for now, in case you want to try to use it, here&#8217;s an example using both the SnapTo and HotkeysBehavior (you can find this in the <a href="https://poshconsole.svn.codeplex.com/svn/trunk/GlobalHotkeys">GlobalHotkeys</a> demo project, which includes some sample code for handling hotkey conflicts).  Enjoy.</p>

	<div class="xml code xml" style="font-family:monospace;"><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Window</span> <span style="color: #000066;">x:Class</span>=<span style="color: #ff0000;">&quot;GlobalHotkeys.DemoWindow&quot;</span> <span style="color: #000066;">x:Name</span>=<span style="color: #ff0000;">&quot;window&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000066;">xmlns:x</span>=<span style="color: #ff0000;">&quot;http://schemas.microsoft.com/winfx/2006/xaml&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000066;">xmlns:huddled</span>=<span style="color: #ff0000;">&quot;http://schemas.huddledmasses.org/wpf&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000066;">Title</span>=<span style="color: #ff0000;">&quot;Demo Window!&quot;</span> <span style="color: #000066;">Height</span>=<span style="color: #ff0000;">&quot;300&quot;</span> <span style="color: #000066;">Width</span>=<span style="color: #ff0000;">&quot;200&quot;</span></span><br />
<span style="color: #009900;"> &nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;huddled:Native.Behaviors<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;huddled:SnapToBehavior</span> <span style="color: #000066;">SnapDistance</span>=<span style="color: #ff0000;">&quot;80,20,80,10&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;huddled:HotkeysBehavior<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;KeyBinding</span> <span style="color: #000066;">Command</span>=<span style="color: #ff0000;">&quot;huddled:GlobalCommands.ActivateWindow&quot;</span> <span style="color: #000066;">Key</span>=<span style="color: #ff0000;">&quot;K&quot;</span> &nbsp;<span style="color: #000066;">Modifiers</span>=<span style="color: #ff0000;">&quot;Win&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;KeyBinding</span> <span style="color: #000066;">Command</span>=<span style="color: #ff0000;">&quot;huddled:GlobalCommands.CloseWindow&quot;</span> &nbsp; &nbsp;<span style="color: #000066;">Key</span>=<span style="color: #ff0000;">&quot;F4&quot;</span> <span style="color: #000066;">Modifiers</span>=<span style="color: #ff0000;">&quot;Win&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;KeyBinding</span> <span style="color: #000066;">Command</span>=<span style="color: #ff0000;">&quot;huddled:GlobalCommands.ToggleWindow&quot;</span> &nbsp; <span style="color: #000066;">Key</span>=<span style="color: #ff0000;">&quot;S&quot;</span> &nbsp;<span style="color: #000066;">Modifiers</span>=<span style="color: #ff0000;">&quot;Win&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/huddled:HotkeysBehavior<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/huddled:Native.Behaviors<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Grid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Label</span> <span style="color: #000066;">Content</span>=<span style="color: #ff0000;">&quot;Drag this window near the screen edges&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span><br />
&nbsp; &nbsp; <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Grid<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Window<span style="color: #000000; font-weight: bold;">&gt;</span></span></span><br />
&nbsp;</div>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/wpf-windows-that-snap-to-screen-edges/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

