<?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>esmithy.net</title>
	<atom:link href="http://esmithy.net/feed/" rel="self" type="application/rss+xml" />
	<link>http://esmithy.net</link>
	<description>Stuff Hammered Out by Eric Smith</description>
	<lastBuildDate>Sun, 31 Jan 2010 05:41:32 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>&#8220;It&#8217;s Just UI&#8221;</title>
		<link>http://esmithy.net/2010/01/30/its-just-ui/</link>
		<comments>http://esmithy.net/2010/01/30/its-just-ui/#comments</comments>
		<pubDate>Sun, 31 Jan 2010 05:41:32 +0000</pubDate>
		<dc:creator>Eric</dc:creator>
				<category><![CDATA[User Interface]]></category>

		<guid isPermaLink="false">http://esmithy.net/?p=348</guid>
		<description><![CDATA[Maybe I&#8217;ve just imagined this &#8212; created my own straw man to take down &#8212; but I&#8217;ve sometimes sensed some disparagement of user interface programming. &#8220;It&#8217;s just UI,&#8221; the architect will say. &#8220;It&#8217;s not like it&#8217;s a hard or interesting part of the system.&#8221; As I&#8217;ve done more and more UI development lately, I&#8217;ve had [...]]]></description>
			<content:encoded><![CDATA[<p>Maybe I&#8217;ve just imagined this &#8212; created my own straw man to take down &#8212; but I&#8217;ve sometimes sensed some disparagement of user interface programming. &#8220;It&#8217;s just UI,&#8221; the architect will say. &#8220;It&#8217;s not like it&#8217;s a hard or interesting part of the system.&#8221; As I&#8217;ve done more and more UI development lately, I&#8217;ve had to consider if this is the direction I want my career to go.<br />
<span id="more-348"></span><br />
Some UIs amount to binding a data grid to a database. In fact, based on Microsoft&#8217;s tools and examples, there are apparently a lot of those. In such cases, I agree, the UI isn&#8217;t terribly interesting. But to build a user interface for more sophisticated applications is a real challenge. Here are some of the reasons why.</p>
<p><strong>Graphic design</strong> &#8211; Even when you&#8217;ve got a talented graphic designer helping out, you usually don&#8217;t get handed everything you need, or everything exactly perfect. You&#8217;ll need to fill in the gaps to get a complete, attractive interface. Being able to fire up Illustrator to tweak and extrapolate, or layout a clean window are  good skills to have. Modern UIs also include animations, which are hard to get from a static &#8220;comp&#8221;.</p>
<p><strong> Validation</strong> &#8211; It is reasonable for the model to establish preconditions for methods. When user input is involved, there are few preconditions. Welcome to the wild west. Sure, you can insist that users enter phone numbers without any dashes, spaces, dots, parentheses or other corruptions of data purity, but if I&#8217;m the user, that just ticks me off.</p>
<p><strong> Error reporting</strong> &#8211; The buck stops here. The model can just throw an exception if something goes wrong, but presenting a meaningful and actionable error message to the user is hard.</p>
<p><strong> Threading</strong> &#8211; For Windows applications, all interaction with a window must happen on the thread that created the window. Yet to keep the UI responsive, you shouldn&#8217;t do time-consuming operations on the UI thread, forcing the introduction of other threads. You can see something slightly at odds here.</p>
<p><strong>Usability</strong> &#8211; Small design and implementation decisions can greatly impact usability, and what is easiest to code is frequently not what is easiest to use. Most applications involve tasks with a fair amount of complexity, and making them as simple as possible is way harder than laying all of the complexities at the feet of the user.</p>
<p><strong>Testability</strong> &#8211; There is  a gulf associated with what it means for UI to work. On one side, you have &#8220;functions as specified without defects&#8221;. On the other, you have &#8220;effectively supports the user in his/her goals&#8221;. Furthermore, it is really hard to unit test UI code, which sort of leads to the next challenge&#8230;</p>
<p><strong>Pattern ambiguity</strong> &#8211; I remember reading once (I wish I could remember the source) that if you ask 10 developers what the Model-View-Controller pattern is, you&#8217;ll get 11 different answers. While the most touted pattern in UI development, Model-View-Controller has so many variations that it can be daunting to figure out what it even means, and a fair amount of experience to begin to understand when a particular variation is most appropriate.</p>
<p><strong>Localization</strong> &#8211; Model code needs to be culture-sensitive, but the biggest impact of localization is in the UI. Some frameworks make it easier than others, but having a UI that lays out equally well with &#8220;Archive Video on PC&#8221; and &#8220;Archivierung aufgezeichneter Videos auf dem PC&#8221; is tricky.</p>
<p><strong> HCI priesthood</strong> &#8211; If you have a CS degree and actually write <em>code</em>, just give up now, you&#8217;ll never get it. Or so I&#8217;ve been told, but I still believe in the enlightened engineer.</p>
<p>I&#8217;m thinking that there&#8217;s enough challenge here to be interesting and skills that will continue to be in demand.</p>
]]></content:encoded>
			<wfw:commentRss>http://esmithy.net/2010/01/30/its-just-ui/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Succumbing to the Smartphone Siren</title>
		<link>http://esmithy.net/2010/01/05/succumbing-to-the-smartphone-siren/</link>
		<comments>http://esmithy.net/2010/01/05/succumbing-to-the-smartphone-siren/#comments</comments>
		<pubDate>Wed, 06 Jan 2010 03:59:22 +0000</pubDate>
		<dc:creator>Eric</dc:creator>
				<category><![CDATA[Hardware]]></category>
		<category><![CDATA[Mobile]]></category>

		<guid isPermaLink="false">http://esmithy.net/?p=279</guid>
		<description><![CDATA[So shortly after crowing about how cheaply I was getting cell phone service, I&#8217;ve sailed headlong into the rocks of an $80 a month smart phone plan. In spite of all the Android buzz of late, I went with the Palm Pre.
As I&#8217;ve posted before, I used to be a big Palm enthusiast but switched [...]]]></description>
			<content:encoded><![CDATA[<p>So shortly after crowing about <a href="/2009/02/21/cell-phone-service-for-the-non-garrulous/">how cheaply I was getting cell phone service</a>, I&#8217;ve sailed headlong into the rocks of an $80 a month smart phone plan. In spite of all the Android buzz of late, I went with the Palm Pre.</p>
<p><span id="more-279"></span>As I&#8217;ve <a href="/2005/06/25/dell-axim-x50v-in-a-palm-users-hands/">posted before</a>, I used to be a big Palm enthusiast but switched to Windows Mobile about five years ago. Palm seemed to be falling apart, and Microsoft seemed about poised to do great things. I was wrong &#8212; at least about Microsoft. Windows Mobile is powerful, but it&#8217;s not <em>enjoyable</em>. I think that user experience has been prioritized somewhere below an integrated nose-hair trimmer, and I credit Windows mobile with turning me from an engaged mobile enthusiast into a passive mobile user because of its stagnation.</p>
<p>It&#8217;s refreshing to be a Palm user again, with the Palm Pre bringing back the elegance and simplicity of those earlier devices combined with the modernness appropriate for the years that have elapsed since then. Palm&#8217;s webOS, the underlying Pre operating system, is beautiful. It also gets updated, unlike Windows Mobile. If something doesn&#8217;t seem quite right with webOS, there&#8217;s at least hope that it will get fixed with a simple, over-the-air and in-place upgrade.</p>
<p>The Pre itself is also an example of first-class industrial design. It feels good and looks good. Contrast that with the Motorola Droid, which is truly ugly. Maybe they thought the boxy form factor would go with the robot branding or something. The Cylon monotoning, &#8220;Droid&#8221; when you turn the thing on is pure cheese. The Nexus One looks more promising, though.</p>
<p>Aside from things I&#8217;ve already mentioned, here a few other things that make me happy about the Pre and webOS:</p>
<ul>
<li>Synergy &#8211; I&#8217;m pretty careful with keeping contacts and email organized, but Synergy changes the game. It does the consolidation for you so you don&#8217;t have to be so, um, anal.</li>
<li>Multitasking &#8211; Of course, Windows Mobile can do that just fine, but webOS&#8217;s card view makes it so simple to work with.</li>
<li>Touchstone &#8211; The Touchstone charger is great &#8212; no cable to plug in, not even a dock. You just set your phone on it and pick it up fully charged later.</li>
<li>Ares &#8211; The online IDE that is in beta right now is pretty impressive. I&#8217;ve been doing a little development, and dropped Eclipse like a rock when Ares showed up. Ares, like webOS, is simple and elegant. Maybe a little <em>too</em> simple right now.</li>
</ul>
<p>And a few things that need improvement:</p>
<ul>
<li>Missing apps &#8211; I need a reader for LDS scriptures, a decent place to put my passwords, and a way to sync my &#8220;to do&#8221; list to a desktop machine (preferably to Google tasks, though I&#8217;m not sure if they have an API yet).</li>
<li>Smoothness &#8211; The iPhone is soooo smoooooth in its operation. The Pre lags a bit sometimes.</li>
<li>Bedroom manners &#8211; Although the Touchstone charger is great, it sometimes doesn&#8217;t behave nicely on your nightstand. The screen turns on or the &#8220;charging&#8221; tone rings during the night.</li>
<li>Dev confusion &#8211; While I think Ares is a nice step forward, it has been hard to really learn WebOS development. I&#8217;m a software engineer, doing causal exploration of the SDK, and sometimes things just seem disjoint. I think I read somewhere that Palm changed the development model late in the game, and sometimes the documentation seems fragmented and the examples possibly obsolete. The one webOS book&#8217;s reviewers say it is all about the old model, so it is almost unusable.</li>
</ul>
<p>So&#8230; Why didn&#8217;t I just get an iPhone? I could probably come up with some good rational reasons, but the biggest ones are more emotional.</p>
<ol>
<li>As I said, having had such a great experience with Palm in the past, the Pre makes me feel warm and fuzzy.</li>
<li>iTunes&#8230; no thanks.</li>
<li>I&#8217;m an anti-herd kind of guy. If everybody is doing something (namely, buying iPhones) it makes me want to do something different. Yes, totally irrational.</li>
</ol>
<p><center><br />
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="425" height="344" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube.com/v/CZGIn9bpALo&amp;hl=en_US&amp;fs=1&amp;" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="425" height="344" src="http://www.youtube.com/v/CZGIn9bpALo&amp;hl=en_US&amp;fs=1&amp;" allowscriptaccess="always" allowfullscreen="true"></embed></object><br />
</center></p>
]]></content:encoded>
			<wfw:commentRss>http://esmithy.net/2010/01/05/succumbing-to-the-smartphone-siren/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>New Depths of Printer Evil</title>
		<link>http://esmithy.net/2010/01/04/new-depths-of-printer-evil/</link>
		<comments>http://esmithy.net/2010/01/04/new-depths-of-printer-evil/#comments</comments>
		<pubDate>Tue, 05 Jan 2010 03:37:11 +0000</pubDate>
		<dc:creator>Eric</dc:creator>
				<category><![CDATA[Hardware]]></category>

		<guid isPermaLink="false">http://esmithy.net/?p=335</guid>
		<description><![CDATA[I frequently say, only half-jokingly, that printers are evil. Actually I love my HP LaserJet 1320, but my Canon Pixma 620 is just plain E-V-I-L.
OK, pretty much any inkjet printer is devil-spawn, with the well-cited fact that printer ink is the most expensive liquid on the planet. Canon has taken the Pixma to a new [...]]]></description>
			<content:encoded><![CDATA[<p>I frequently say, only half-jokingly, that printers are evil. Actually I love my HP LaserJet 1320, but my Canon Pixma 620 is just plain E-V-I-L.</p>
<p><span id="more-335"></span>OK, pretty much any inkjet printer is devil-spawn, with the well-cited fact that printer ink is the most expensive liquid on the planet. Canon has taken the Pixma to a new level, though, because while it is a combination printer/scanner/fax, you can&#8217;t do <em>anything</em> useful if you&#8217;re out of ink. Notably, in spite of the fact that no bits need converting to atoms when scanning an image to your PC, you just can&#8217;t do it if you&#8217;re out of ink. All you get is a message on the display stating, &#8220;The following ink has run out. Replace the ink tank.&#8221;</p>
<p>Yeah, the printer/scanner is pretty good when you&#8217;re willing to open a vein to fill the ink, but disabling non-ink functions by fiat is just infuriating.</p>
]]></content:encoded>
			<wfw:commentRss>http://esmithy.net/2010/01/04/new-depths-of-printer-evil/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>.NET Color Struct Equality</title>
		<link>http://esmithy.net/2009/12/16/net-color-struct-equality/</link>
		<comments>http://esmithy.net/2009/12/16/net-color-struct-equality/#comments</comments>
		<pubDate>Thu, 17 Dec 2009 00:47:55 +0000</pubDate>
		<dc:creator>Eric</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Bafflers]]></category>

		<guid isPermaLink="false">http://esmithy.net/?p=322</guid>
		<description><![CDATA[When is white not white? When one is Color.White and the other is Color.FromArgb(0xff, 0xff, 0xff, 0xff).
I was trying to data bind a list of colors to a ComboBox and have the SelectedValue property bound to a particular color. The frustrating thing was that even though there was a color matching the &#8220;selected&#8221; color in [...]]]></description>
			<content:encoded><![CDATA[<p>When is white not white? When one is Color.White and the other is Color.FromArgb(0xff, 0xff, 0xff, 0xff).</p>
<p><span id="more-322"></span>I was trying to data bind a list of colors to a ComboBox and have the SelectedValue property bound to a particular color. The frustrating thing was that even though there was a color matching the &#8220;selected&#8221; color in the list, the ComboBox never had a selection when first displayed.</p>
<p>Eventually I discovered that my &#8220;selected&#8221; color was white (all color components at 255) while the list&#8217;s equivalent was Color.White. Careful reading of the <a href="http://msdn.microsoft.com/en-us/library/e03x8ct2.aspx">Color.Equals</a> documentation tells me that I was silly to think that white is white or black is black:</p>
<blockquote><p><span> <span><a id="ctl00_MTCS_main_ctl36_ctl00_ctl03" onclick="javascript:Track('ctl00_MTCS_main_ctl36_ctl00_contenthere|ctl00_MTCS_main_ctl36_ctl00_ctl03',this);" href="http://msdn.microsoft.com/en-us/library/system.drawing.color.black.aspx">Black</a></span> and <span class="code">FromArgb(0,0,0)</span> are not considered equal, since <span><a id="ctl00_MTCS_main_ctl36_ctl00_ctl04" onclick="javascript:Track('ctl00_MTCS_main_ctl36_ctl00_contenthere|ctl00_MTCS_main_ctl36_ctl00_ctl04',this);" href="http://msdn.microsoft.com/en-us/library/system.drawing.color.black.aspx">Black</a></span> is a named color and <span class="code">FromArgb(0,0,0)</span> is not.</span></p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://esmithy.net/2009/12/16/net-color-struct-equality/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Defeating Antivirus System Pro</title>
		<link>http://esmithy.net/2009/12/03/defeating-antivirus-system-pro/</link>
		<comments>http://esmithy.net/2009/12/03/defeating-antivirus-system-pro/#comments</comments>
		<pubDate>Fri, 04 Dec 2009 04:46:51 +0000</pubDate>
		<dc:creator>Eric</dc:creator>
				<category><![CDATA[Misc]]></category>

		<guid isPermaLink="false">http://esmithy.net/?p=318</guid>
		<description><![CDATA[Antivirus System Pro is an irksome piece of malware that holds your computer hostage until you pay a ransom. It is nothing short of extortion. I just spent a few evenings removing it from a friend&#8217;s computer, and the dozens of articles on the web (and even YouTube videos) were all wrong about how to [...]]]></description>
			<content:encoded><![CDATA[<p>Antivirus System Pro is an irksome piece of malware that holds your computer hostage until you pay a ransom. It is nothing short of extortion. I just spent a few evenings removing it from a friend&#8217;s computer, and the dozens of articles on the web (and even YouTube videos) were all wrong about how to get rid of this instance.<br />
<span id="more-318"></span><br />
The program throws up all kinds of messages saying that the computer is infected, and occasionally sends Internet Explorer off to a porn or Viagra site for good measure &#8212; I guess to make the &#8220;infection&#8221; seem more urgent. It also prevents new processes from starting, except for IE, since they presumably need to leave a way open for you to pay your ransom. The program sets up a proxy server for IE, though, so you can&#8217;t get anywhere but Antivirus System Pro&#8217;s site.</p>
<p>Usually in a situation like this, I use the <a href="http://www.ubcd4win.com/">Ultimate Boot CD for Windows</a>. The scans I ran that way didn&#8217;t find anything, though.</p>
<p>Other information on the internet suggests booting the system in Safe Mode and running <a href="http://www.malwarebytes.org/mbam.php">Malwarebytes Anti-Malware</a> to clean the system. This also didn&#8217;t work.</p>
<p>There are several sites that give <a href="http://www.ehow.com/how_5146071_manually-remove-antivirus-system-pro.html">manual removal instructions</a>. On my friend&#8217;s system, none of the binaries or registry keys from the instructions existed.</p>
<p>Here&#8217;s what I did that appears to have worked:</p>
<ol>
<li>Get a copy of <a href="http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx">Process Explorer</a>, probably easiest done on another machine and put on a USB flash drive.</li>
<li>Boot the computer normally.</li>
<li>Let the race begin. Start Process Explorer before Antivirus System Pro does.</li>
<li>Bring up Antivirus System Pro&#8217;s scan window.</li>
<li>Use the Process Explorer&#8217;s &#8220;Find Window&#8217;s Process&#8221; tool by dragging it over the scan window.</li>
<li>Now you know the executable that is Antivirus System Pro, which was bsuhsysgaurd.exe on my friend&#8217;s machine.</li>
<li>By showing the &#8220;Image Path&#8221; column in Process Explorer, you can also see where it is running from.</li>
<li>Go ahead and kill the process. It feels good.</li>
<li>Delete the executable.</li>
<li>Search the registry for references to the executable and delete those (such as from Start keys).</li>
<li>Get rid of the proxy in IE by going to <strong>Tools &gt; Internet Options &gt; Connections</strong> &gt; <strong>LAN Settings</strong> and uncheck &#8220;Use a proxy server for your LAN&#8221;.</li>
</ol>
<p>I also installed the newly free <a href="http://www.microsoft.com/Security_Essentials/">Microsoft Security Essentials</a> on my friends machine, since although there was a version of MacAfee on there, it was FUBAR. I&#8217;m not sure if that was something Antivirus System Pro had accomplished or if it being that way was what let the malware through in the first place.</p>
<p>All seems well for now&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://esmithy.net/2009/12/03/defeating-antivirus-system-pro/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Transparent WinForms Label</title>
		<link>http://esmithy.net/2009/11/28/transparent-winforms-label/</link>
		<comments>http://esmithy.net/2009/11/28/transparent-winforms-label/#comments</comments>
		<pubDate>Sat, 28 Nov 2009 18:40:41 +0000</pubDate>
		<dc:creator>Eric</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[User Interface]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://esmithy.net/?p=315</guid>
		<description><![CDATA[Easy control transparency isn&#8217;t exactly a hallmark of Windows Forms. If you have a form with a background image or a gradient, the stock controls paint with a solid background to give a criminally egregious aesthetic.
If you search the web for a solution, you might come up with a couple of proposed solutions:

WinForms: How to [...]]]></description>
			<content:encoded><![CDATA[<p>Easy control transparency isn&#8217;t exactly a hallmark of Windows Forms. If you have a form with a background image or a gradient, the stock controls paint with a solid background to give a criminally egregious aesthetic.</p>
<p><span id="more-315"></span>If you search the web for a solution, you might come up with a couple of proposed solutions:</p>
<ul>
<li><a href="http://support.microsoft.com/kb/943454">WinForms: How to create a control transparent to other controls</a> &#8211; The official word from Microsoft, the solution is presented in several pages of poorly formatted VB code that makes you immediately want to look for something simpler, such as&#8230;</li>
<li><a href="http://www.doogal.co.uk/transparent.php">C# Transparent Label</a> (also <a href="http://www.west-wind.com/WebLog/posts/247977.aspx">referenced</a> from Rick Strahl&#8217;s blog) &#8211; This solution is much simpler; it&#8217;s just a Control subclass that doesn&#8217;t paint a background and draws the label text in OnPaint.</li>
</ul>
<p>After working with the second solution for a while, I discovered that it doesn&#8217;t handle updating the text very well. Just changing the Text property itself doesn&#8217;t do it, and if you invalidate the control, you get the new text painted over the old text. If you&#8217;re the kind of person who fusses about minutiae like <em>legibility</em> and such, this isn&#8217;t ideal.</p>
<p>So here&#8217;s a solution that has worked reasonably well for me, and that scores high marks in the simplicity category:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp"><span class="co1">/// </span>
<span class="co1">/// A label that is transparent.</span>
<span class="co1">/// </span>
<span class="kw1">public</span> <span class="kw4">class</span> TransparentLabel <span class="sy0">:</span> Label
<span class="br0">&#123;</span>
  <span class="co1">/// </span>
  <span class="co1">/// Paints the background with the parent's background image.</span>
  <span class="co1">/// </span>
  <span class="co1">///</span>
e
  <span class="kw1">protected</span> <span class="kw1">override</span> <span class="kw1">void</span> OnPaintBackground<span class="br0">&#40;</span>PaintEventArgs e<span class="br0">&#41;</span>
  <span class="br0">&#123;</span>
    Rectangle rect <span class="sy0">=</span> <span class="kw3">new</span> Rectangle<span class="br0">&#40;</span>Location, Size<span class="br0">&#41;</span><span class="sy0">;</span>
    e.<span class="me1">Graphics</span>.<span class="me1">DrawImage</span><span class="br0">&#40;</span>Parent.<span class="me1">BackgroundImage</span>, <span class="nu0">0</span>, <span class="nu0">0</span>, rect, GraphicsUnit.<span class="me1">Pixel</span><span class="br0">&#41;</span><span class="sy0">;</span>
  <span class="br0">&#125;</span>
<span class="br0">&#125;</span></pre></div></div>

<p>It basically scoops up the correct part of the parent&#8217;s background image and uses it as the background for the control. Yes, it still isn&#8217;t transparency, just a much better job of &#8220;camouflage&#8221; than the default behavior. If you&#8217;re painting the parent&#8217;s background, with a gradient brush for example, you can paint that into a bitmap instead and set the parent&#8217;s BackgroundImage to the bitmap for this solution to still work.</p>
]]></content:encoded>
			<wfw:commentRss>http://esmithy.net/2009/11/28/transparent-winforms-label/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rounded Rectangluar Regions</title>
		<link>http://esmithy.net/2009/11/19/rounded-rectangluar-regions/</link>
		<comments>http://esmithy.net/2009/11/19/rounded-rectangluar-regions/#comments</comments>
		<pubDate>Fri, 20 Nov 2009 04:27:27 +0000</pubDate>
		<dc:creator>Eric</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://esmithy.net/?p=302</guid>
		<description><![CDATA[There are lots of examples that demonstrate how to draw a rectangle with rounded corners using GDI+ in .NET. Converting such a rectangle to a Region so that it can be filled or be used for the geometry of a window can have less than perfect results, though.

For example, here is one of the more [...]]]></description>
			<content:encoded><![CDATA[<p>There are lots of examples that demonstrate how to draw a rectangle with rounded corners using GDI+ in .NET. Converting such a rectangle to a Region so that it can be filled or be used for the geometry of a window can have less than perfect results, though.<br />
<span id="more-302"></span><br />
For example, here is one of the more concise methods for getting a rounded Rectangle:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp"><span class="kw1">public</span> <span class="kw1">static</span> GraphicsPath GetRoundedRectangle<span class="br0">&#40;</span>Rectangle rect, <span class="kw4">int</span> rad<span class="br0">&#41;</span>
<span class="br0">&#123;</span>
	<span class="kw4">int</span> d <span class="sy0">=</span> <span class="nu0">2</span> <span class="sy0">*</span> rad<span class="sy0">;</span>
	<span class="kw5">System</span>.<span class="me1">Drawing</span>.<span class="me1">Drawing2D</span>.<span class="me1">GraphicsPath</span> gp <span class="sy0">=</span>
			<span class="kw3">new</span> <span class="kw5">System</span>.<span class="me1">Drawing</span>.<span class="me1">Drawing2D</span>.<span class="me1">GraphicsPath</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
	gp.<span class="me1">AddArc</span><span class="br0">&#40;</span>rect.<span class="me1">X</span>, rect.<span class="me1">Y</span>, d, d, <span class="nu0">180</span>, <span class="nu0">90</span><span class="br0">&#41;</span><span class="sy0">;</span>
	gp.<span class="me1">AddArc</span><span class="br0">&#40;</span>rect.<span class="me1">X</span> <span class="sy0">+</span> rect.<span class="me1">Width</span> <span class="sy0">-</span> d, rect.<span class="me1">Y</span>, d, d, <span class="nu0">270</span>, <span class="nu0">90</span><span class="br0">&#41;</span><span class="sy0">;</span>
	gp.<span class="me1">AddArc</span><span class="br0">&#40;</span>rect.<span class="me1">X</span> <span class="sy0">+</span> rect.<span class="me1">Width</span> <span class="sy0">-</span> d, rect.<span class="me1">Y</span> <span class="sy0">+</span> rect.<span class="me1">Height</span> <span class="sy0">-</span> d, d, d, <span class="nu0">0</span>, <span class="nu0">90</span><span class="br0">&#41;</span><span class="sy0">;</span>
	gp.<span class="me1">AddArc</span><span class="br0">&#40;</span>rect.<span class="me1">X</span>, rect.<span class="me1">Y</span> <span class="sy0">+</span> rect.<span class="me1">Height</span> <span class="sy0">-</span> d, d, d, <span class="nu0">90</span>, <span class="nu0">90</span><span class="br0">&#41;</span><span class="sy0">;</span>
	gp.<span class="me1">AddLine</span><span class="br0">&#40;</span>rect.<span class="me1">X</span>, rect.<span class="me1">Y</span> <span class="sy0">+</span> rect.<span class="me1">Height</span> <span class="sy0">-</span> d, rect.<span class="me1">X</span>, rect.<span class="me1">Y</span> <span class="sy0">+</span> d <span class="sy0">/</span> <span class="nu0">2</span><span class="br0">&#41;</span><span class="sy0">;</span>
&nbsp;
	<span class="kw1">return</span> gp<span class="sy0">;</span>
<span class="br0">&#125;</span></pre></div></div>

<p>It&#8217;s trivial to convert that GraphicsPath to a Region &#8212; just call the appropriate Region constructor. You can then fill the region:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp">var path <span class="sy0">=</span> GetRoundedRectangle<span class="br0">&#40;</span>rect, <span class="nu0">3</span><span class="br0">&#41;</span><span class="sy0">;</span>
e.<span class="me1">Graphics</span>.<span class="me1">FillRegion</span><span class="br0">&#40;</span>Brushes.<span class="me1">Orange</span>, <span class="kw3">new</span> Region<span class="br0">&#40;</span>path<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>

<p>If your corner radius is sufficiently large, you might not notice anything wrong with the result. With a value of 3, it is pretty obvious:</p>
<p style="text-align: center;"><img class="size-full wp-image-303 aligncenter" title="Not-so-rounded rectangle" src="http://esmithy.net/content/rect-bad.png" alt="Not-so-rounded rectangle" width="321" height="191" /></p>
<p>The top left corner looks perfect:</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-308" title="Top-left" src="http://esmithy.net/content/rect-topleft.png" alt="Top-left" width="105" height="105" /></p>
<p>The bottom right corner is decidedly un-round:</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-309" title="Bottom-right" src="http://esmithy.net/content/rect-bottomright1.png" alt="Bottom-right" width="120" height="126" /></p>
<p>I started to wonder if all those round rectangle functions were wrong, but drawing the GraphicsPath itself shows that the code is correct:</p>
<p style="text-align: center;"><img class="size-full wp-image-307 aligncenter" title="Graphics Path" src="http://esmithy.net/content/rect-path.png" alt="Graphics Path" width="329" height="200" /></p>
<p>The problem is that when converting the GraphicsPath to a Region, it uses the <em>inside </em>of the GraphicsPath, so you lose the outside pixels on the sides, which you can see when drawing the path and then filling the region:</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-305" title="Missing pixels in the region" src="http://esmithy.net/content/rect-detail.png" alt="rect-detail" width="310" height="181" /></p>
<p>It turns out to be a whole lot easier and effective to do a little p/invoke here:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp"><span class="br0">&#91;</span>DllImport<span class="br0">&#40;</span><span class="st0">&quot;gdi32.dll&quot;</span><span class="br0">&#41;</span><span class="br0">&#93;</span>
<span class="kw1">static</span> <span class="kw1">extern</span> IntPtr CreateRoundRectRgn<span class="br0">&#40;</span><span class="kw4">int</span> x1, <span class="kw4">int</span> y1, <span class="kw4">int</span> x2, <span class="kw4">int</span> y2, <span class="kw4">int</span> cx, <span class="kw4">int</span> cy<span class="br0">&#41;</span><span class="sy0">;</span>
<span class="co1">// ...</span>
var region <span class="sy0">=</span> Region.<span class="me1">FromHrgn</span><span class="br0">&#40;</span>CreateRoundRectRgn<span class="br0">&#40;</span>rect.<span class="me1">X</span>, rect.<span class="me1">Y</span>, rect.<span class="me1">X</span> <span class="sy0">+</span> rect.<span class="me1">Width</span>, rect.<span class="me1">Y</span> <span class="sy0">+</span> rect.<span class="me1">Height</span>, <span class="nu0">3</span>, <span class="nu0">3</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="sy0">;</span>
e.<span class="me1">Graphics</span>.<span class="me1">FillRegion</span><span class="br0">&#40;</span>Brushes.<span class="me1">Orange</span>, region<span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>

<p>Here&#8217;s the output from that code:</p>
<p style="text-align: center;"><img class="aligncenter size-full wp-image-306" title="Good Region" src="http://esmithy.net/content/rect-good.png" alt="Good Region" width="310" height="182" /></p>
<p>It looks like a little larger corner radius is needed to get the same rounding, but otherwise it is perfect.</p>
]]></content:encoded>
			<wfw:commentRss>http://esmithy.net/2009/11/19/rounded-rectangluar-regions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Self-Referential Segues</title>
		<link>http://esmithy.net/2009/10/03/self-referential-segues/</link>
		<comments>http://esmithy.net/2009/10/03/self-referential-segues/#comments</comments>
		<pubDate>Sat, 03 Oct 2009 18:14:25 +0000</pubDate>
		<dc:creator>Eric</dc:creator>
				<category><![CDATA[Misc]]></category>
		<category><![CDATA[Pedantry]]></category>

		<guid isPermaLink="false">http://esmithy.net/?p=275</guid>
		<description><![CDATA[If you say &#8220;segue&#8221; while attempting one, you&#8217;ve already blown it.
]]></description>
			<content:encoded><![CDATA[<p>If you say &#8220;segue&#8221; while attempting one, you&#8217;ve already blown it.</p>
]]></content:encoded>
			<wfw:commentRss>http://esmithy.net/2009/10/03/self-referential-segues/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Silverlight Localization</title>
		<link>http://esmithy.net/2009/07/08/silverlight-localization/</link>
		<comments>http://esmithy.net/2009/07/08/silverlight-localization/#comments</comments>
		<pubDate>Thu, 09 Jul 2009 02:00:57 +0000</pubDate>
		<dc:creator>Eric</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://esmithy.net/?p=252</guid>
		<description><![CDATA[The official Silverlight documentation about localization gives lots of information about working with resource files, but it is kind of scant on how to actually get localized strings to show up in the UI in a reasonable way. For example, an unreasonable way would be to set the text/content properties of all the controls in [...]]]></description>
			<content:encoded><![CDATA[<p>The official <a href="http://msdn.microsoft.com/en-us/library/cc838238(VS.95).aspx">Silverlight documentation about localization</a> gives lots of information about working with resource files, but it is kind of scant on how to actually get localized strings to show up in the UI in a reasonable way. For example, an unreasonable way would be to set the text/content properties of all the controls in a page&#8217;s code-behind file. A reasonable way is to use data binding.</p>
<p><span id="more-252"></span></p>
<p>I found a good example of this in Chapter 5 of <em>Professional Silverlight 2 for ASP.NET Developers</em> by Jonathan Swift, Salvador Alvarez Patuel, Chris Barker and Dan Wahlin. As I describe it, and continue the discussion through this post, you&#8217;ll notice that the term &#8220;resource&#8221; is overloaded in Silverlight: There are resources in the normal .NET sense, backed by .resx files, and there are resources that are simply a dictionary of objects instantiated in XAML within a Resources element. Hopefully the usage will not be overly confusing.</p>
<p>In the localization example from the book, they created a resource file called LocalizedStrings.resx. Then they instantiated the automatically generated strongly typed class by including it in the Resources associated with a user control. Finally, they could then bind the Text property of a couple of TextBlock controls to a particular string from the resources:</p>

<div class="wp_syntax"><div class="code"><pre class="xml">&nbsp;</pre></div></div>

<p>You&#8217;ll want to be sure to use the PublicResXFileCodeGenerator custom tool to generate the code from the .resx file, otherwise the binding doesn&#8217;t work.</p>
<p>So what if you want to dynamically change languages, such as with a language drop-down?</p>
<p>The solution I came up with incorporates string resources into the view model. I was hesitant to do this at first because it felt funny having resources in the view model. Aren&#8217;t the strings purely a view component? Ultimately I decided that supplying the correct strings to the view based on language choice is a reasonable thing for the view model to do, and the resulting implementation and usage felt pretty good.</p>
<p>The base class for all view models simply has a Resources member:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp"><span class="kw1">public</span> <span class="kw1">virtual</span> <span class="kw4">object</span> Resources
<span class="br0">&#123;</span>
  get <span class="br0">&#123;</span> <span class="kw1">return</span> null<span class="sy0">;</span> <span class="br0">&#125;</span>
<span class="br0">&#125;</span></pre></div></div>

<p>Each subclass then returns the appropriate object containing the resources:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp"><span class="kw1">public</span> <span class="kw4">class</span> MyViewModel <span class="sy0">:</span> ViewModel
<span class="br0">&#123;</span>
  <span class="kw1">private</span> MyViewModelResources _resources<span class="sy0">;</span>
&nbsp;
  MyViewModel<span class="br0">&#40;</span><span class="br0">&#41;</span>
  <span class="br0">&#123;</span>
    _resources <span class="sy0">=</span> <span class="kw3">new</span> MyViewModelResorces<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span>
  <span class="br0">&#125;</span>
&nbsp;
  <span class="kw1">public</span> <span class="kw1">override</span> <span class="kw4">object</span> Resources
  <span class="br0">&#123;</span>
    get <span class="br0">&#123;</span> <span class="kw1">return</span> _resources<span class="sy0">;</span> <span class="br0">&#125;</span>
  <span class="br0">&#125;</span>
&nbsp;
  <span class="co1">// ...</span>
<span class="br0">&#125;</span></pre></div></div>

<p>The class MyViewModelResources is the class generated from MyViewModelResources.resx. You might be saying, &#8220;Wait a minute. That class is basically <em>static</em>. What are you doing creating an instance of it?&#8221; Well, I don&#8217;t think there&#8217;s a way to bind to statics, so you need to have an actual instance around. Of course, the view will actually end up calling the static property (such as SubmitButtonText) when you include one in the binding path. Also, this isn&#8217;t any different from instantiating the resource class in XAML as the first example does.</p>
<p>Modifying the first example to use the view model (and setting the view model as the DataContext) makes the XAML look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="xml">&nbsp;</pre></div></div>

<p>So what about the dynamic switching of languages at run time?</p>
<p>Since the &#8220;Resources&#8221; property is in the binding path, changing its value will update the bindings and the UI updates with the new strings. You just need to notify the view that the Resources property has changed. To make this easy, I created a helper method in the ViewModel base class, RaiseResourcesChanged, that effectively says, &#8220;Attention all view models, please update your views&#8217; resources.&#8221; The message gets propagated to each view model instance by having a static ResourcesChanged event in the ViewModel base class that each ViewModel instance subscribes to at construction. When the event fires, the base class in turn raises the INotifyPropertyChanged.PropertyChanged event for the &#8220;Resources&#8221; property.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp"><span class="kw1">public</span> <span class="kw4">class</span> ViewModel <span class="sy0">:</span> INotifyPropertyChanged
<span class="br0">&#123;</span>
  <span class="kw1">private</span> <span class="kw1">static</span> <span class="kw1">event</span> EventHandler ResourcesChanged<span class="sy0">;</span>
&nbsp;
  <span class="kw1">public</span> ViewModel<span class="br0">&#40;</span><span class="br0">&#41;</span>
  <span class="br0">&#123;</span>
    ResourcesChanged <span class="sy0">+=</span> ViewModel_ResourcesChanged<span class="sy0">;</span>
  <span class="br0">&#125;</span>
&nbsp;
  <span class="kw1">protected</span> <span class="kw1">void</span> RaiseResourcesChanged<span class="br0">&#40;</span><span class="br0">&#41;</span>
  <span class="br0">&#123;</span>
    var handler <span class="sy0">=</span> ResourcesChanged<span class="sy0">;</span>
    <span class="kw1">if</span> <span class="br0">&#40;</span>handler <span class="sy0">!=</span> <span class="kw1">null</span><span class="br0">&#41;</span>
    <span class="br0">&#123;</span>
      ResourcesChanged<span class="br0">&#40;</span><span class="kw1">this</span>, EventArgs.<span class="me1">Empty</span><span class="br0">&#41;</span><span class="sy0">;</span>
    <span class="br0">&#125;</span>
  <span class="br0">&#125;</span>
&nbsp;
  <span class="kw1">void</span> ViewModel_ResourcesChanged<span class="br0">&#40;</span><span class="kw4">object</span> sender, EventArgs e<span class="br0">&#41;</span>
  <span class="br0">&#123;</span>
    RaisePropertyChanged<span class="br0">&#40;</span><span class="st0">&quot;Resources&quot;</span><span class="br0">&#41;</span><span class="sy0">;</span>
  <span class="br0">&#125;</span>
&nbsp;
  <span class="co1">// ...</span>
<span class="br0">&#125;</span></pre></div></div>

<p>Any single view model can then change the CurrentUICulture, call the helper method, and the UI updates automatically for all views:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp">Thread.<span class="me1">CurrentThread</span>.<span class="me1">CurrentUICulture</span> <span class="sy0">=</span> newCulture<span class="sy0">;</span>
RaiseResourcesChanged<span class="br0">&#40;</span><span class="br0">&#41;</span><span class="sy0">;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://esmithy.net/2009/07/08/silverlight-localization/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Unit Testing Silverlight View Models</title>
		<link>http://esmithy.net/2009/06/08/unit-testing-silverlight-view-models/</link>
		<comments>http://esmithy.net/2009/06/08/unit-testing-silverlight-view-models/#comments</comments>
		<pubDate>Mon, 08 Jun 2009 15:16:23 +0000</pubDate>
		<dc:creator>Eric</dc:creator>
				<category><![CDATA[Silverlight]]></category>

		<guid isPermaLink="false">http://esmithy.net/?p=246</guid>
		<description><![CDATA[Being relatively new to Silverlight development, I&#8217;ve not had the good sense to accept conventional wisdom that the Visual Studio unit test framework can&#8217;t be used to test Silverlight code. For testing view models at least, I&#8217;ve been successfully using Mstest and Rhino Mocks for a few months now.

One of the goals of the model-view-view [...]]]></description>
			<content:encoded><![CDATA[<p>Being relatively new to Silverlight development, I&#8217;ve not had the good sense to accept conventional wisdom that the Visual Studio unit test framework can&#8217;t be used to test Silverlight code. For testing view models at least, I&#8217;ve been successfully using Mstest and Rhino Mocks for a few months now.</p>
<p><span id="more-246"></span></p>
<p>One of the goals of the <em>model-view-view model</em> pattern is to avoid code in the XAML code-behind files. Instead, the view model exposes what the UI needs and the UI consumes it through declarative data binding. <a href="http://msdn.microsoft.com/en-us/magazine/dd458800.aspx">Shawn Wildermuth&#8217;s MSDN Magazine article</a> does a good job of explaining the basics of the pattern. The view model ends up having the logic that you&#8217;d really like to test.</p>
<p>The project structure looks like this:</p>
<ul>
<li><strong>SilverlightApp</strong> &#8211; This project is the <em>view</em>, created with the Visual Studio Silverlight Application project template. There&#8217;s lots of XAML in here.</li>
<li><strong>SilverlightApp.Model</strong> &#8211; This project contains both the <em>model</em> and the <em>view model</em>. It is a Silverlight Class Library project, and the view project has a project reference to this. There&#8217;s no XAML in here.</li>
<li><strong>SilverlightApp.Model.Test</strong> &#8211; This project has the unit tests, and is a normal C# Unit Test project. It has a project reference to the model/view model project.</li>
</ul>
<p>If you write up a simple unit test, it will compile, run, and pass.</p>
<div id="attachment_249" class="wp-caption aligncenter" style="width: 519px"><img class="size-full wp-image-249" title="test-passed" src="http://esmithy.net/content/test-passed.jpg" alt="Woohoo." width="509" height="121" /><p class="wp-caption-text">Woohoo -- passing test.</p></div>
<p><strong>Problem #1</strong></p>
<p>Suppose your view model uses something from System.Core (such as the Action delegate). Now when you try to run your unit test, you get an error:</p>
<blockquote><p>Test method SilverlightApp.Model.Test.UnitTest.TestMethod1 threw exception:  System.IO.FileNotFoundException: Could not load file or assembly &#8216;System.Core, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e&#8217; or one of its dependencies. The system cannot find the file specified.</p></blockquote>
<p>The problem is that the test project is referencing the .NET 3.5 System.Core assembly. You can fix that by referencing the Silverlight assembly instead. Remove the existing System and System.Core references, and add the ones in the Silverlight SDK (for example, C:\Program Files (x86)\Microsoft SDKs\Silverlight\v2.0\Reference Assemblies\). Now the test will pass again.</p>
<p><strong>Problem #2</strong></p>
<p>Now suppose that you want your view model to do something like use the IsolatedStorageSettings class. When you run your test, it will be broken again:</p>
<blockquote><p>Test method SilverlightApp.Model.Test.UnitTest.TestMethod1 threw exception:  System.IO.FileNotFoundException: Could not load file or assembly &#8216;System.Windows, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e&#8217; or one of its dependencies. The system cannot find the file specified.</p></blockquote>
<p>Well, that just looks like problem #1 again, so we&#8217;ll add the Silverlight System.Windows assembly as a reference. Hmm&#8230; still doesn&#8217;t work:</p>
<blockquote><p>Test method SilverlightApp.Model.Test.UnitTest.TestMethod1 threw exception:  System.MissingMethodException: Method not found: &#8216;System.IO.IsolatedStorage.IsolatedStorageFile System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForSite()&#8217;.</p></blockquote>
<p>According to the <a href="http://msdn.microsoft.com/en-us/library/system.io.isolatedstorage.isolatedstoragefile(VS.95).aspx">documentation</a>, IsolatedStorageFile is in mscorlib.dll. Can I just add a reference to that? No; when you try you get an error:</p>
<blockquote><p>A reference to &#8216;C:\Program Files (x86)\Microsoft SDKs\Silverlight\v2.0\Reference Assemblies\mscorlib.dll&#8217; could not be added. This component is automatically referenced by the project system and cannot be referenced directly.</p></blockquote>
<p>It&#8217;s time to step back and think a little. Do we really want to have Silverlight isolated storage as part of our unit tests anyway? Probably not. Just like when you&#8217;re unit testing code that would access a database, you&#8217;d really rather mock the database so you can efficiently test just the unit in question. The same applies here, so you can create an interface to hide the concrete implementation. From there, it&#8217;s a matter of choosing the right implementation at the right time.</p>
<p>The view model can accept an ISettings in its constructor (or it can use a service locator or dependency injection). When creating a test instance, pass in a mock implementation. In the real application, you can use the isolated storage implementation.</p>
<p><strong>So Far So Good</strong></p>
<p>By using these two techniques (referencing Silverlight assemblies in the test project and adding a level of abstraction where needed), I&#8217;ve been able to effectively test my view models with the normal desktop tools. The tests all run as part of the continuous integration builds, just as you&#8217;d hope.</p>
<p>Maybe at some point I&#8217;ll run into something that just doesn&#8217;t work, but so far it&#8217;s working out just fine.</p>
]]></content:encoded>
			<wfw:commentRss>http://esmithy.net/2009/06/08/unit-testing-silverlight-view-models/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
