<?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; Design</title>
	<atom:link href="http://huddledmasses.org/tag/design/feed/" rel="self" type="application/rss+xml" />
	<link>http://huddledmasses.org</link>
	<description>You can do more than breathe for free...</description>
	<lastBuildDate>Sat, 28 Jan 2012 21:37:20 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<cloud domain='huddledmasses.org' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
		<item>
		<title>RFC: Information in PoshCode Module Manifests</title>
		<link>http://huddledmasses.org/rfc-information-in-poshcode-module-manifests/</link>
		<comments>http://huddledmasses.org/rfc-information-in-poshcode-module-manifests/#comments</comments>
		<pubDate>Fri, 13 Aug 2010 03:31:25 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Module Manifest]]></category>
		<category><![CDATA[PoshCode]]></category>
		<category><![CDATA[PowerShell]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1530</guid>
		<description><![CDATA[OK, I&#8217;m putting this out there to see if I&#8217;ve missed anything that would be useful. This is the list of metadata that we&#8217;ll be storing about Modules in the PoshCode repository. It duplicates most of the information in the PowerShell Manifests, but it adds more of information that we feel is important for searching, [...]]]></description>
			<content:encoded><![CDATA[	<p>OK, I&#8217;m putting this out there to see if I&#8217;ve missed anything that would be useful.  This is the list of metadata that we&#8217;ll be storing about Modules in the PoshCode repository. It duplicates most of the information in the PowerShell Manifests, but it adds more of information that we feel is important for searching, sorting and organizing &#8230; and in the PoshCode Module manifests, wildcards are not allowed for any of the &#8220;export&#8221; values.  The export information in the module is intended to represent the initial state of the module after import: listing all the cmdlets, functions, variables and aliases &#8230;</p>

	<h2>PoshCode Module Manifest Format:</h2>

	<h4><span class="caps">GUID</span></h4>

	<p>The globally unique identifier for this module. Helps us tell modules apart  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt=':)' class='wp-smiley' /> </p>

	<h4>Name</h4>

	<p>The name of this module. Helps us find modules, and helps you guess what they&#8217;re for.</p>

	<h4>ModuleVersion</h4>

	<p>The version of this module.  Together with the <span class="caps">GUID</span>, this is the unique key for the module. It should be incremented whenever you release it.</p>

	<h4>Description</h4>

	<p>A short description of the module. What it is, what it does, what it&#8217;s for.</p>

	<h4>ModuleType</h4>

	<p>Tells whether the module is a Script module or a Binary module (if they are both, then they are Binary).</p>

	<h4>Author</h4>

	<p>The <strong>person(s)</strong> who created this module. No organizations here, just people.</p>

	<h4>Company</h4>

	<p>A company (if there is one) that created this module or provides support for it.</p>

	<h4>Copyright</h4>

	<p>The copyright statement. You should always fill this out and assign copyright to the correct author, group or corporation.</p>

	<h4>License</h4>

	<p>The software license(s) that this module can be licensed under. It&#8217;s enough to use an acronym like &#8220;<span class="caps">GPL</span> v2&#8221; or &#8220;MIT&#8221; or &#8220;Ms-PL&#8221; ... but if you have a custom license you can specify a description of it or the full text of it. Multiple entries are allowed.</p>

	<h4>LicenseUri</h4>

	<p>The url(s) for a full copy of the license. If you&#8217;re using an open source license you can link to it&#8217;s page on <a href="http://opensource.org/licenses/">opensource.org</a></p>

	<h4>Category</h4>

	<p>We&#8217;ll publish our official category list later, but the category is intended to be an assignment to a single category in a hierarchical tree. </p>

	<h4>Tags</h4>

	<p>A collection of user-assigned free-text keywords that help to categorize the module and serve as additional search terms.</p>

	<h4>Homepage</h4>

	<p>If the module has a home page on the internet, this is the <span class="caps">URL</span>. You can use the link to the PoshCode Docs page (which our cmdlets will generate for you) if you don&#8217;t have your own page.</p>

	<h4>Download</h4>

	<p>This is the link to download the module. If you&#8217;re hosting it on PoshCode, we will fill this in. If you&#8217;re hosting it elsewhere, this must be a <strong>download</strong> link. If it&#8217;s a commercial module that requires payment, you should leave this link empty unless you have a free trial download.  Note: I&#8217;m still working on the commercial angle.  Would it be helpful to include the price here? If we offered a way to sell your modules through PoshCode, would you use it?</p>

	<h4>RequiredAssemblies</h4>

	<p>Assembly names (or relative paths, if the assemblies are included in the module folder) for assemblies that must be loaded before the module will work. These should be the Assembly FullName, not a partial name.</p>

	<h4>RequiredModules</h4>

	<p>The names and versions (and GUIDs) of any modules that this module depends on.</p>

	<h4>PowerShellVersion</h4>

	<p>The version of Powershell required (2.0, for now).</p>

	<h4>FrameworkVersion</h4>

	<p>The version of the .Net Framework required. The framework version implies the <span class="caps">CLR</span> version, so we&#8217;ll just stick to this.  I&#8217;m still toying with the idea of saying the list is 2.0, 3.0, 3.5, 3.5.1, and 4.0 &#8230; rather than allowing the normal version with the full four digits.</p>

	<h4>VersionChanges</h4>

	<p>This is either the list of changes in this version, or a complete list of the changes in previous versions.</p>

	<h4>Exported Cmdlets</h4>

	<p>The list of (binary) cmdlets exported by this module.</p>

	<h4>Exported Functions</h4>

	<p>The list of script functions exported by this module.</p>

	<h4>Exported Aliases</h4>

	<p>The list of aliases exported by this module.</p>

	<h4>Exported Variables</h4>

	<p>The list of variables exported by this module.</p>

	<h3>Deprecated things &#8230;</h3>

	<p>I&#8217;m leaning against having these items in the PoshCode manifest &#8212; they don&#8217;t seem to be helpful for finding or sorting or understanding a module.</p>

	<ul>
		<li>ModuleFile</li>
		<li>ModuleToProcess</li>
		<li>FormatsToProcess</li>
	</ul>
	<ul>
		<li>TypesToProcess</li>
	</ul>

	<h2>Comments Requested</h2>

	<p>I&#8217;m hoping for feedback about this list, what I might be missing, or if there&#8217;s anything in here I should leave out &#8230;  let me hear it!</p>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/rfc-information-in-poshcode-module-manifests/feed/</wfw:commentRss>
		<slash:comments>1</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>Rage Against the Design</title>
		<link>http://huddledmasses.org/rage-against-the-design/</link>
		<comments>http://huddledmasses.org/rage-against-the-design/#comments</comments>
		<pubDate>Mon, 02 Feb 2009 17:52:30 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Bugs]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[Rants]]></category>
		<category><![CDATA[Software Engineering]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/rage-against-the-design/</guid>
		<description><![CDATA[So we found a problem recently with a certain scripting language&#8217;s argument parsing: function Test-Argument&#40;$a&#41; &#123; &#160; &#160;$a.GetType&#40;&#41;.FullName &#125; &#91;Test 1&#93;: Test-Argument 4 System.Int32 &#91;Test 2&#93;: Test-Argument .5 System.Double &#91;Test 3&#93;: Test-Argument &#34;hello&#34; System.String &#91;Test 4&#93;: Test-Argument Goodbye System.String &#91;Test 5&#93;: Test-Argument -42 System.String &#91;Test 6&#93;: Test-Argument &#40;-42&#41; System.Int32 &#160; Why can&#8217;t it properly parse [...]]]></description>
			<content:encoded><![CDATA[	<p>So we found a problem recently with a certain scripting language&#8217;s argument parsing:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #666699; font-weight: bold;">function</span> <span style="color: #0066cc; font-style: italic;">Test-<span style="font-style: normal;">Argument</span></span><span style="color: #333;">&#40;</span><span style="color: #660033; font-weight: bold;">$a</span><span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #660033; font-weight: bold;">$a</span>.<span style="color: #003366;">GetType</span><span style="color: #333;">&#40;</span><span style="color: #333;">&#41;</span>.<span style="color: #003366;">FullName</span><br />
<span style="color: #333;">&#125;</span><br />
<br />
<span style="color: #333;">&#91;</span>Test <span style="color: #cc66cc;">1</span><span style="color: #333;">&#93;</span>: <span style="color: #0066cc; font-style: italic;">Test-<span style="font-style: normal;">Argument</span></span> <span style="color: #cc66cc;">4</span><br />
System.<span style="color: #003366;">Int32</span><br />
<span style="color: #333;">&#91;</span>Test <span style="color: #cc66cc;">2</span><span style="color: #333;">&#93;</span>: <span style="color: #0066cc; font-style: italic;">Test-<span style="font-style: normal;">Argument</span></span> .5<br />
System.<span style="color: #003366; font-weight: bold;">Double</span><br />
<span style="color: #333;">&#91;</span>Test <span style="color: #cc66cc;">3</span><span style="color: #333;">&#93;</span>: <span style="color: #0066cc; font-style: italic;">Test-<span style="font-style: normal;">Argument</span></span> <span style="color: #009900;">&quot;hello&quot;</span><br />
System.<span style="color: #003366; font-weight: bold;">String</span><br />
<span style="color: #333;">&#91;</span>Test <span style="color: #cc66cc;">4</span><span style="color: #333;">&#93;</span>: <span style="color: #0066cc; font-style: italic;">Test-<span style="font-style: normal;">Argument</span></span> Goodbye<br />
System.<span style="color: #003366; font-weight: bold;">String</span><br />
<span style="color: #333;">&#91;</span>Test <span style="color: #cc66cc;">5</span><span style="color: #333;">&#93;</span>: <span style="color: #0066cc; font-style: italic;">Test-<span style="font-style: normal;">Argument</span></span> <span style="color: #66cc66;">-</span><span style="color: #cc66cc;">42</span><br />
System.<span style="color: #003366; font-weight: bold;">String</span><br />
<span style="color: #333;">&#91;</span>Test <span style="color: #cc66cc;">6</span><span style="color: #333;">&#93;</span>: <span style="color: #0066cc; font-style: italic;">Test-<span style="font-style: normal;">Argument</span></span> <span style="color: #333;">&#40;</span><span style="color: #66cc66;">-</span><span style="color: #cc66cc;">42</span><span style="color: #333;">&#41;</span><br />
System.<span style="color: #003366;">Int32</span><br />
&nbsp;</div>

	<p>Why can&#8217;t it properly parse -42 as an integer, when it can parse .5 as a double? Well, according to the development team of a certain Fortune 100 company, this behavior is <strong>by-design</strong> ...  Apparently, &#8220;.&#8221; can be a number, but &#8220;-&#8221; can&#8217;t.</p>

	<blockquote>
		<p>When you know you&#8217;ve got it all wrong, but you can&#8217;t be bothered to get it right, document it &#8212; make it look intentional, and most people won&#8217;t question you.</p>
	</blockquote>

	<p>I&#8217;m sorry folks, but I&#8217;ve had it <strong>up to here</strong> with the &#8220;it&#8217;s by design&#8221; excuse. I don&#8217;t care who you are, and I don&#8217;t care who wrote the design spec &#8212; when something is as obviously wrong as this, <span class="em1">you need to fix it</span>, not just give us platitudes.</p>

	<p>I had the same thing happen recently with a bug I filed about the way wildcard behavior impedes matching file-names with square brackets in them in PowerShell.  They told me this was by design, and that I could use the -LiteralPath parameter.  Well, if any of you have tried this, you already know what I&#8217;m going to say: it&#8217;s broken.</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #666666; font-style: italic;">## This works if the file already exists</span><br />
<span style="color: #666666; font-style: italic;">## But fails completely if it doesn't exist</span><br />
<span style="color: #0066cc; font-style: italic;">set-<span style="font-style: normal;">content</span></span> <span style="color: #000066;">-LiteralPath</span> <span style="color: #009900;">&quot;logs [www.example.com].txt&quot;</span> <span style="color: #000066;">-Value</span> <span style="color: #009900;">&quot; help &quot;</span><br />
&nbsp;</div>

	<p>And yet, I was initially told it was <em>supposed</em> to be this way.  Now, in this case, I happened to have the email address of the software architect, and they&#8217;ve <a href="https://connect.microsoft.com/feedback/ViewFeedback.aspx?FeedbackID=337243&#38;SiteID=99">reopened my bug</a> after I sent him an email with lots of examples of how this <em>bug</em> defied the behavior that a user expects.</p>

	<p>We software developers need to be <em>very</em> careful about saying &#8220;that&#8217;s by design&#8221; ... because it sometimes makes us sound stupid.  When a user says &#8220;this is broken,&#8221; and your reply is &#8220;that&#8217;s by design,&#8221; what the user hears is &#8220;we broke it on purpose.&#8221;  We should not be willing to excuse bad design.</p>

	<p>Listen up: If you want to be a successful software developer, you need to learn this, and learn it well: <span class="em2">the fact that it was <span class="caps">DESIGNED</span> <span class="caps">WRONG</span> is <span class="caps">NOT</span> AN <span class="caps">EXCUSE</span> for shipping broken software</span>.  Regardless of whether it&#8217;s your design, or someone else&#8217;s, even if it was designed this way by your manager&#8217;s boss. When you create software that doesn&#8217;t behave the way the user expects it to, you need to consider the possibility that <strong>you&#8217;re doing it wrong</strong>.</p>

	<p>Imagine if <em>architectural</em> engineers were to behave in a similar manner &#8230; Suppose the original architect of the golden gate bridge had left a gap in the middle of the bridge, with a little ramp: you could drive up the bridge, but you couldn&#8217;t get across unless you were comfortable jumping your car across a four foot opening.</p>

	<p>When you complained about it, the engineers would say: it&#8217;s by design &#8212; if you don&#8217;t like jumping your car (and yes, we know that jumping is bad for maintainability), there is a workaround: just wait for the ferry we put in last year. There are several boats, running continuously, so the wait is at maximum about 20 minutes, and it only takes a little longer to cross by boat than it would on the bridge.</p>

	<p>That analogy is obviously not perfect, but the point is: just because someone decided it should be done a certain way doesn&#8217;t mean that&#8217;s the right thing to do &#8212; sometimes the design is just wrong.  Perhaps the designer and architects overlooked something, or perhaps the circumstances have changed, but in any case, if the software doesn&#8217;t work the way people expect it to work, or requires different workarounds depending on the situation &#8230; you need to question the design.</p>

	<p>All I&#8217;m asking is this: <span class="em3">don&#8217;t turn your brain off</span>: when someone complains about the way something works (or doesn&#8217;t work), think about what they&#8217;re asking, and if the complaint makes sense, don&#8217;t say &#8220;this misbehavior is by design&#8221; until you&#8217;ve reconsidered the design.</p>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/rage-against-the-design/feed/</wfw:commentRss>
		<slash:comments>4</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>
		<item>
		<title>Rant: Sometimes I hate my job</title>
		<link>http://huddledmasses.org/rant-sometimes-i-hate-my-job/</link>
		<comments>http://huddledmasses.org/rant-sometimes-i-hate-my-job/#comments</comments>
		<pubDate>Wed, 09 Jul 2008 18:08:52 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[Databases]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Rants]]></category>

		<guid isPermaLink="false">http://HuddledMasses.org/?p=564</guid>
		<description><![CDATA[So I&#8217;ve been asked to add a feature to one of the apps that I nominally took over when my former manager left &#8230; they want a management pane where certain authorized super users (to be decided later) can add data to the main lookup tables, and must be able to do clean up by [...]]]></description>
			<content:encoded><![CDATA[	<p>So I&#8217;ve been asked to add a feature to one of the apps that I <em>nominally</em> took over when my former manager left &#8230; they want a management pane where certain authorized super users (to be decided later) can add data to the main lookup tables, and must be able to do clean up by deleting data which has been entered erroneously &#8230; including cleaning up any references to the now missing data.</p>

	<p>I&#8217;m currently trying to figure out what database tables I need to be concerned with, and I just have to vent, because this system is the worst mess I&#8217;ve ever seen.  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt='[disgust]' class='wp-smiley' /> </p>

	<ul>
		<li>There&#8217;s one database supporting five or more applications&#8230;</li>
		<li>There are 87 tables (with names like tblAQ_DcSs, tblAQ_SwNw, tblSFM, and tblSW_PWSOS, tblTestCaseTestLayout )</li>
		<li>There are <strong>three</strong> duplicate user tables: tblPeopleLookup, tblUser, and tblUser3 &#8212; tblUser2 is a view onto an external user database which is what is <strong>supposedly</strong> being used &#8230; and apparently, tblPeopleLookup is some sort of mapping from tblUser2 to tblUser3 &#8230; and tblUser1 is the original user table. I don&#8217;t know why these are all still here &#8212; I can only hope none of these others are still being used.</li>
		<li>There are 144 stored procedures (with names like sp_Fix, sp_Fix2, sp_Fix3, spLeftToTestMulti, spLeftToTestMulti2, spLeftToTestMulti3, sp_Whatever, and the awesome spTestCaseTestLayoutTestsUpdate, spTestCaseTestLayoutTestsSelect, etc.)</li>
	</ul>
	<ul>
		<li>There are <strong>no</strong> Foreign Keys.  Yeah. None.  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt='[crazy]' class='wp-smiley' /> </li>
	</ul>

	<p>Technically, there are lots of foreign keys &#8212; it&#8217;s just that none of them are declared as such, so there&#8217;s no referential integrity (did I mention that there&#8217;s an access database floating around out there with linked tables and a hard-coded login which the end-users pass around to each other so they can insert data into some of the tables <strong>by hand</strong> because the original developers didn&#8217;t get around to writing this management app that I&#8217;ve been asked to write now?)</p>

	<p>You can tell that some of the columns <em>should</em> be Foreign Keys, because obviously a column in a &#8220;tblReq_Tag&#8221; table named &#8220;Feature_ID&#8221; must be an external lookup of some sort &#8230; but there&#8217;s 86 other tables &#8230; and at least two of them have Primary Keys called &#8220;Feature_ID&#8221; ...</p>

	<p>So, I&#8217;m spending a lot of time searching the source code and the 144 stored procedures &#8230; An astonishing number of these stored procedures involve cursors and multiple nested case statements.  I just picked one at random which I thought sounded simple: <strong>spEnterGroupResults</strong> ... it&#8217;s about 150 lines of <span class="caps">SQL</span>, and it uses a single cursor variable &#8220;crsUnit&#8221; which it redefines three separate times onto three different queries which it iterates over. Each of these queries involves joins onto nested subqueries, and I count myself lucky because the tricky part is actually enclosed in a transaction, and at least <em>this one</em> isn&#8217;t doing all of that just to dynamically generate a further <span class="caps">SQL</span> query to execute.</p>

	<p>So yeah, I&#8217;m literally looking through source code to try to understand the database design.  The problem is that there are more than five different applications, each using slightly different technologies.</p>

	<ul>
		<li>One of them which has never been migrated from classic <span class="caps">ASP</span> ... with the business logic written entirely in Javascript, and the data handling performed entirely by sending <strong>huge</strong> <span class="caps">XML</span> files back and forth to a &#8220;do all&#8221; webservice.</li>
		<li>One of them was written in VB.<span class="caps">NET</span> in VS 2003, and has never been upgraded.</li>
	</ul>
	<ul>
		<li>The rest are in C# &#8212; with most in VS 2005, and at least one in VS 2008 and C# 3.0 &#8212; some are Asp.Net, some are rich client &#8230;</li>
	</ul>

	<p>The tables I&#8217;m most concerned with right now (for this app) have some <em>ahem</em> ... impressive design decisions of their own.  Of the 8 tables that I&#8217;m looking at directly (I think these are the only ones I need to <em>modify</em> as part of this app), five of them have multi-column primary keys that involve more than half the columns in the table, including columns which are, in fact, unconstrained foreign keys.  And there are so far 5 foreign key looking columns which I haven&#8217;t been able to find the primary key column for &#8230;  <img src='http://huddledmasses.org/wordpress/wp-includes/' alt='[pullhair]' class='wp-smiley' /> </p>

	<p> <img src='http://huddledmasses.org/wordpress/wp-includes/' alt='[new]' class='wp-smiley' />  <strong>Edit</strong>: Oh yeah, and half of these tables have columns like <code>[Enabled] [char](1) NULL</code> &#8230;  That&#8217;s a <strong>boolean</strong> value folks, stored in the database as a <code>y</code> or a <code>n</code> &#8230; and it&#8217;s nullable even though a null (or any value other than <code>y</code> or <code>n</code>, really) will most likely blow up some code somewhere.  And no, there&#8217;s no script constraint or trigger to ensure that this doesn&#8217;t happen (I checked). For extra fun, the other half of the tables use &#8216;bit&#8217; columns for things like this  &#8212; because they were written after I started working with this team (on a different project) and happened across one of these char columns during our one and only code review ever and wondered aloud why we needed to pretend it was still 1992.  Why they just switched, without changing the others, I&#8217;ll never know&#8230;</p>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/rant-sometimes-i-hate-my-job/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Apple OS X Leopard &#8211; Coverflow</title>
		<link>http://huddledmasses.org/apple-os-x-leopard-coverflow/</link>
		<comments>http://huddledmasses.org/apple-os-x-leopard-coverflow/#comments</comments>
		<pubDate>Mon, 29 Oct 2007 04:14:09 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Apple]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[HCI]]></category>
		<category><![CDATA[OSX]]></category>
		<category><![CDATA[ThisIsBroken]]></category>
		<category><![CDATA[UI]]></category>

		<guid isPermaLink="false">http://HuddledMasses.org/apple-os-x-leopard-coverflow/</guid>
		<description><![CDATA[I have only one thing to say about coverflow. The LOOK of coverflow, and it&#8217;s complete lack of integration into the LOOK of &#8230; well, anything else in finder, frankly&#8230;. just reminds me of Monty Python&#8217;s credits: &#8220;completed in an entirely different style at great expense and at the last minute.&#8221;]]></description>
			<content:encoded><![CDATA[	<p>I have only one thing to say about coverflow.</p>

	<p>The <span class="caps">LOOK</span> of coverflow, and it&#8217;s complete lack of integration into the <span class="caps">LOOK</span> of &#8230; well, anything else in finder, frankly&#8230;. just reminds me of Monty Python&#8217;s credits: &#8220;completed in an entirely different style at great expense and at the last minute.&#8221; </p>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/apple-os-x-leopard-coverflow/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

