<?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; WM_WINDOWPOSCHANGING</title>
	<atom:link href="http://huddledmasses.org/tag/wm_windowposchanging/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>WPF Window &#8220;Native&#8221; Behavior: Snap-To Screen Edges</title>
		<link>http://huddledmasses.org/wpf-windows-that-snap-to-screen-edges/</link>
		<comments>http://huddledmasses.org/wpf-windows-that-snap-to-screen-edges/#comments</comments>
		<pubDate>Tue, 14 Oct 2008 05:06:36 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Design]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[GUI]]></category>
		<category><![CDATA[Hacking]]></category>
		<category><![CDATA[NativeBehaviors]]></category>
		<category><![CDATA[Win32]]></category>
		<category><![CDATA[WM_WINDOWPOSCHANGING]]></category>
		<category><![CDATA[WPF]]></category>
		<category><![CDATA[XAML]]></category>

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

