<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:gml="http://www.opengis.net/gml"
>

<channel>
	<title>Rohit's Blog &#187; Coding/ Programming</title>
	<atom:link href="http://www.smart-techie.com/blog/category/computing/coding-programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.smart-techie.com/blog</link>
	<description>My opinion about anything and everything!</description>
	<lastBuildDate>Sun, 15 Jan 2012 05:13:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Inspecting Web Interfaces</title>
		<link>http://www.smart-techie.com/blog/2007/06/inspecting-web-interfaces/</link>
		<comments>http://www.smart-techie.com/blog/2007/06/inspecting-web-interfaces/#comments</comments>
		<pubDate>Wed, 27 Jun 2007 15:22:52 +0000</pubDate>
		<dc:creator>Rohit</dc:creator>
				<category><![CDATA[Coding/ Programming]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Web Links]]></category>

		<guid isPermaLink="false">http://www.smart-techie.com/blog/2007/06/inspecting-web-interfaces/</guid>
		<description><![CDATA[(Continued from Secure Web Interfaces) OK, I had a tremendous urge to call this post &#8220;Hacking Web Interfaces&#8221;. But due to some uneducated folks who believe hacking is a bad word, I think I will stick to its euphemism. The most direct way to feed random data to a web-service is in a raw format. [...]]]></description>
			<content:encoded><![CDATA[<p><em>(Continued from <a href="http://www.smart-techie.com/blog/2007/06/creating-secure-web-interfaces/">Secure Web Interfaces</a>)<br />
</em></p>
<p>OK, I had a tremendous urge to call this post &#8220;Hacking Web Interfaces&#8221;. But due to some uneducated folks who believe hacking is a bad word, I think I will stick to its euphemism.</p>
<p>The most direct way to feed random data to a web-service is in a raw format. Use telnet for absolute low level communications or tools like Fiddler, for communications at a higher level like HTTP.&nbsp;But with sites hosting hundreds of script files and html pages on their server, this approach has become more and more impractical. But there are other means to mess with the client side code.</p>
<h3>DOM Inspector</h3>
<p>If you use Firefox, you will notice there&#8217;s a utility called <a href="http://www.mozilla.org/projects/inspector/">DOM Inspector</a> that is included with it. As the name suggests, it allows you to see the DOM of the rendered HTML page. But, it also allows you to modify that DOM. You can modify the values of the properties of different DOM objects in the tree and even insert new objects.</p>
<p>Let&#8217;s try this technique on a product of one of the largest software companies, Google Calendar. I will use no other software than Firefox and DOM inspector.</p>
<p>Let&#8217;s go to the Settings screen. They have a setting for the &#8220;Custom View&#8221; property for the calendar.</p>
<p><a href="http://i104.photobucket.com/albums/m190/rohit_sud/Blog/image-1.png" atomicselection="true" rel="lightbox"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="99" alt="Google Calendar Settings" src="http://i104.photobucket.com/albums/m190/rohit_sud/Blog/image_thumb-2.png" width="122" border="0" /></a></p>
<p>Now I am a guy who wants to know what he would be doing in next 2 months but there is no option for that kind of view. Hmm, lets see if the DOM can help us. Firing the DOM Inspector, we see that the logic is quite simple.</p>
<p><a href="http://i104.photobucket.com/albums/m190/rohit_sud/Blog/image_3.png" atomicselection="true" rel="lightbox"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="99" alt="Firefox DOM Inspector" src="http://i104.photobucket.com/albums/m190/rohit_sud/Blog/image_thumb_3.png" width="127" border="0" /></a></p>
<p>The value of the ComboBox determines how the duration for which the custom view display the calendar. The value in the ComboBox is simply translated to the number of days. So I change the value to 42 (which is 6 weeks) and save my Settings.&nbsp;Et voilÃ , I get a custom view with 6 weeks <img src='http://www.smart-techie.com/blog/smilies/msn_smiley.png' alt='&#58;&#41;' class='wp-smiley' width='21' height='21' title='&#58;&#41;' /> Cool!</p>
<p><a href="http://i104.photobucket.com/albums/m190/rohit_sud/Blog/image_4.png" atomicselection="true" rel="lightbox"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="99" alt="Customized Google Calendar" src="http://i104.photobucket.com/albums/m190/rohit_sud/Blog/image_thumb_4.png" width="122" border="0"/></a></p>
<h3>Using Visual Studio with IE</h3>
<p>IE has a script debugger built in, but it is worthwhile to go in for Visual Studio as the debugger of choice. There&#8217;s a free version of the Visual Studio suite called the Express edition available which you can download and use.</p>
<p>Now to make it work&#8230;</p>
<ol>
<li>
<p>Enable Script debugging in Internet Explorer.</p>
<p><a href="http://i104.photobucket.com/albums/m190/rohit_sud/Blog/image_5.png" atomicselection="true" rel="lightbox"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="99" alt="Internet Explorer Settings" src="http://i104.photobucket.com/albums/m190/rohit_sud/Blog/image_thumb_5.png" width="78" border="0"/></a></p>
<li>
<p>Create a New Solution with an Empty Website (File &gt; New &gt; Web Site&#8230;)</p>
<p><a href="http://i104.photobucket.com/albums/m190/rohit_sud/Blog/WebProject.png" atomicselection="true" rel="lightbox"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="87" alt="New WebSite Project" src="http://i104.photobucket.com/albums/m190/rohit_sud/Blog/WebProject_thumb.png" width="132" border="0"/></a></p>
<li>
<p>Go to Property Pages for the new Website in Solution Explorer and enter the URL you want to debug. Don&#8217;t worry if you don&#8217;t know the URL, just enter any URL and fire the breakpoints only when you hit the required URL.</p>
<p><a href="http://i104.photobucket.com/albums/m190/rohit_sud/Blog/PropertyPages.png" atomicselection="true" rel="lightbox"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="75" alt="Property Pages" src="http://i104.photobucket.com/albums/m190/rohit_sud/Blog/PropertyPages_thumb.png" width="132" border="0"/></a></p>
<li>
<p>You might get a dialog asking you to include the web.config file. Answer Yes and continue. Now you will be surfing with the debugger attached to Internet Explorer. You can hit pause, set breakpoints and even skip those functions that don&#8217;t interest you <img src='http://www.smart-techie.com/blog/smilies/msn_wink.gif' alt='&#59;&#41;' class='wp-smiley' width='19' height='19' title='&#59;&#41;' /></p>
</li>
</ol>
<h3>Using&nbsp;Venkman with Firefox</h3>
<p><a href="http://i104.photobucket.com/albums/m190/rohit_sud/Blog/image_6.png" atomicselection="true" rel="lightbox"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="99" alt="Venkman Firefox shortcut" src="http://i104.photobucket.com/albums/m190/rohit_sud/Blog/image_thumb_6.png" width="56" border="0"/></a></p>
<p>I would have loved it if Visual Studio also worked with Firefox. There&#8217;s a solution called <a href="http://www.mozilla.org/projects/venkman/" target="_blank" rel="nofollow">Venkman</a>. I won&#8217;t go through the complete detail of how to go about using it, you can read it up <a href="http://www.mozilla.org/projects/venkman/venkman-walkthrough.html" target="_blank" rel="nofollow">here</a>.</p>
<p><a href="http://i104.photobucket.com/albums/m190/rohit_sud/Blog/image_7.png" atomicselection="true" rel="lightbox"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="94" alt="Venkman Firefox" src="http://i104.photobucket.com/albums/m190/rohit_sud/Blog/image_thumb_7.png" width="132" border="0"/></a></p>
<p>I haven&#8217;t found a way to skip code (a la Visual Studio) using this yet. </p>
<p>You can change variable values by directly typing the JavaScript code for it. </p>
<p>The interface doesn&#8217;t have all the bells and whistles but should be able to get most of your work done.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.smart-techie.com/blog/2007/06/inspecting-web-interfaces/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Secure Web Interfaces</title>
		<link>http://www.smart-techie.com/blog/2007/06/creating-secure-web-interfaces/</link>
		<comments>http://www.smart-techie.com/blog/2007/06/creating-secure-web-interfaces/#comments</comments>
		<pubDate>Sun, 24 Jun 2007 11:47:55 +0000</pubDate>
		<dc:creator>Rohit</dc:creator>
				<category><![CDATA[Coding/ Programming]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://www.smart-techie.com/blog/2007/06/creating-secure-web-interfaces/</guid>
		<description><![CDATA[On the Internet, security is overrated. Every third site you visit talks about it. But&#160;still, not many sites take steps to implement it. This has been the complaint of people all these years and is still true today. Unless it is a site that is dealing with financial transactions, most interfaces provided by these sites [...]]]></description>
			<content:encoded><![CDATA[<p>On the Internet, security is overrated. Every third site you visit talks about it. But&nbsp;still, not many sites take steps to implement it. This has been the complaint of people all these years and is still true today. Unless it is a site that is dealing with financial transactions, most interfaces provided by these sites are non-secure and prone to client side attacks. I believe it is a false assumption that unless your site is processing a credit card payments, it doesn&#8217;t need security. Security is not just about protecting &#8216;monetary transactions&#8217;. It is more than that, it involves protecting your computational resources (bandwidth, CPU usage), intellectual resources (JS files, web services) and of course, the services that the site is providing (like acting as a SMS gateway).</p>
<p>In this post I take a look at some ridiculously simple security blunders committed by popular sites. These might not always be critical but provide an interesting insight into the design quality metrics followed at these companies.</p>
<p>While surfing the Net, whenever I see a website offering a cool service, my natural instinct is to find out how they are doing it. For example, Rediff used to have this ActiveX based multilingual mail composition feature, which they replaced with a JavaScript based one. Cool! So now I could work on Firefox and still send my mail in Hindi! How (naively?) were they doing this? Create an IFrame and put the entire composition feature in it. Hmmm, OK, but couldn&#8217;t someone just rip off the JS files and create, say, a multilingual composition plugin for WordPress using their code. Access to the .js files doesn&#8217;t even require a login to the Rediff servers and using them is as easy as pointing the URL to <a title="http://f1mail.rediff.com/quill/QuillPadWeb.html" href="http://f1mail.rediff.com/quill/QuillPadWeb.html">http://f1mail.rediff.com/quill/QuillPadWeb.html</a>. I am not sure what their testing team (if they have one) thought of this, but this allows someone to use the cool text entry interface by just copying 4 files off the Rediff site.</p>
<p><a href="http://i104.photobucket.com/albums/m190/rohit_sud/Blog/image.png" atomicselection="true" rel="lightbox"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="76" alt="image" src="http://i104.photobucket.com/albums/m190/rohit_sud/Blog/image_thumb.png" width="132"/></a> </p>
<p>The easiest way to thwart leechers is to deny access to the scripts unless you are logged on. That would take care of <a href="http://en.wikipedia.org/wiki/Direct_linking" target="_blank">direct linking</a> (protecting computational resources). But there&#8217;s no easy solution if someone just copies the files off the server and hosts them on his website (protecting intellectual resources). What do you do then? Probably obfuscate the code enough so that he never knows what file he has to copy. Yahoo mail does an excellent job of the obfuscation. Even their images are <em>protected</em> (in a loose way) by referring them through their MD5 hashes; enough to deter a casual hacker.</p>
<p>Another flaw is doing all (and I mean <strong>ALL</strong>) the validation at the client side using JavaScript. Client side validation is a nice way to&nbsp;reduce load&nbsp;at the server end. But sometimes, this solution is implemented without safeguards.
<p>While designing any API, there is a golden rule that has to be followed&#8230; <code><br />
<blockquote>All input is evil</p></blockquote>
<p></code>
<p>Agreed, you have JS to check and ensure that the user input is correct. But what if I <a href="http://en.wikipedia.org/wiki/Code_injection" target="_blank">inject code</a> through your web service using a simple script debugger like Visual Studio for IE and <a href="http://www.mozilla.org/projects/venkman/" target="_blank" rel="nofollow">Venkman</a> for Firefox. Even if I am not able to inject code, I will certainly be able to inject syntactically valid input data which&nbsp;is still invalid.&nbsp;To illustrate, imagine a banking service where&nbsp;the amount that can be withdrawn is checked against current balance using JavaScript. I could very well skip the function, overwrite the function which gets the current balance and a lot more.&nbsp;Unless the server is checking the validity of the transaction itself, I&nbsp;would be able&nbsp;to make any kind of transaction.</p>
<p>In my <a href="http://www.smart-techie.com/blog/2007/06/inspecting-web-interfaces/">next post</a> I will discuss, some techniques that can be misused to thwart JavaScript based validations. The purpose of the post will be to further educate software designers about the pitfalls of client side validation which is not backed by robust server side validation.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.smart-techie.com/blog/2007/06/creating-secure-web-interfaces/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Your GPRS mobile is spying on you!</title>
		<link>http://www.smart-techie.com/blog/2007/01/your-gprs-mobile-is-spying-on-you/</link>
		<comments>http://www.smart-techie.com/blog/2007/01/your-gprs-mobile-is-spying-on-you/#comments</comments>
		<pubDate>Mon, 08 Jan 2007 12:53:26 +0000</pubDate>
		<dc:creator>Rohit</dc:creator>
				<category><![CDATA[Coding/ Programming]]></category>
		<category><![CDATA[General]]></category>

		<guid isPermaLink="false">http://www.smart-techie.com/blog/2007/01/your-gprs-mobile-is-spying-on-you/</guid>
		<description><![CDATA[One of the great things about the Internet is anonymity; one can surf the net without disclosing who he/she is and where he comes from (OK, your IP address can be used to track you, but then unless you go through the long process of getting your ISP to yield details about the IP address, [...]]]></description>
			<content:encoded><![CDATA[<p>One of the great things about the Internet is anonymity; one can surf the net without disclosing who he/she is and where he comes from (OK, your IP address can be used to track you, but then unless you go through the long process of getting your ISP to yield details about the IP address, such means can be safely ruled out). Also people using a shared IP address <em>can</em> be more anonymous. In Qatar, there is just one ISP with only one IP address. How can one trace an individual net-surfer from only that information?</p>
<p>No wonder, people expect to have the same kind of anonymity while surfing the web through their mobile devices too. The mobile operator <em>does</em> give you a shared IP address. Though surfing through mobile phones is still very painful in India (3KBps download speeds aren&#8217;t that great), mobile Internet access through GPRS is becoming cheaper every passing month; Airtel offers a HTTP only package for just Rs. 99 per month which would encourage more people to at least try mobile web surfing. But cheap access doesn&#8217;t translate into secure access.</p>
<p>I always used to wonder how various websites like Yahoo, Rediff et al could get access to mobile subscriber data and charge them for their products and services. So I created  a small servlet at http://www.myjavaserver.com/servlet/proteus.SmallFiles.WmlHead [Server is dead now].  And the results turned out to be shocking. I accessed the page from my friend&#8217;s phone and this is what I saw (the number was <strong>not masked</strong>, it has been done so to protect his privacy.</p>
<blockquote><pre><code>Host : www.myjavaserver.com

X-Wap-Profile : &quot;http://wap.sonyericsson.com/UAprof/W700iR101.xml&quot;

Accept-Language : en

Accept : application/vnd.wap.xhtml+xml, application/vnd.wap.wmlc, application/xhtml+xml, image/gif, */*, text/vnd.wap.wml

User-Agent : SonyEricssonW700i/R1CA Browser/SEMC-Browser/4.2 Profile/MIDP-2.0 Configuration/CLDC-1.1

Accept-Charset : *

Accept-Encoding : deflate, gzip

TE : deflate, gzip
<strong>
MSISDN : 9198xxxxxxxx</strong>

WAP-Connection : Stack-Type=HTTP

Cookie : $Version=0;User-Identity-Forward-<strong>msisdn=9198xxxxxxxx</strong>;&#8203;User-Identity-Forward-ppp-username=void&#8203;;Bearer-Type=w-TCP;&#8203;wtls-security-level=none;network-access-type=CSD;&#8203;nas-ip-address=10.151.1.18;apn=airtelfun.com;<strong>imsi=404490016894720</strong>;&#8203;Called-station-id=airtelfun.com;&#8203;accounting-session-id=3D5FF84246578127;&#8203;accounting-authentication-method=2;&#8203;sgsn-ip-address=125.19.6.40

SCRIPT_URL : /servlet/proteus.SmallFiles.WmlHead

SCRIPT_URI : http://www.myjavaserver.com/servlet/proteus.SmallFiles.WmlHead

Remote Address : 203.145.131.158

Remote Host : 203.145.131.158

Server Name : www.myjavaserver.com

Protocol : HTTP/1.1</code>></pre>
</blockquote>
<p>This is from Airtel network in Andhra Pradesh, India. As one can see it is transmitting your mobile number in plain text as well as your mobile operator information to each and every website you visit. It also has your <a href="http://en.wikipedia.org/wiki/IMSI">IMSI</a> number which, about which Wikipedia says &quot;<em>In order to avoid the subscriber being identified and tracked by eavesdroppers on the radio interface, the IMSI is sent as rarely as possible and a randomly generated TMSI is sent instead.</em>&quot;. This is potentially a great way for spammers to send you spam SMSs or track your surfing habits. They can put in a 0 X 0 pixel image on web pages and track you without you having to visit their site. Though much of this is what they can do through normal browsers on computers, here they can <strong>personally identify</strong> you through your phone number. You can visit the <a href="http://www.myjavaserver.com/servlet/proteus.SmallFiles.WmlHead" target="_new">link</a> given above to verify what headers you ISP is attaching to your outbound requests over GPRS. It is a WML page so even the oldest browsers should be able to open it.</p>
<p>It would be futile to even talk to your Customer Care guys about this, write to your ISP about this and ask them WHY they are exposing you to dangers which involve endangering your privacy. As a defence against this, you can use an anonymizing proxy like Opera Mini which will hide your information, but prevention is better than cure!</p>
<p>Source for the Java Servlet is available <a href="http://www.smart-techie.com/web/programming/java-servlets/servlet-to-view-browser-information-while-visiting-a-site-in-wml-format.html">here </a>.<br />
<em><strong><br />
Remember you need to access the URL with your Mobile phones , accessing through your normal browser will display the header information of your PC Browser. Also you need a GPRS connection that allows you to surf external sites and not just those of your service provider&#8217;s.</strong></em><br />
<br/><br />
For the curious, I have also added a Java Servlet that displays the headers sent by your PC browsers <a href="http://www.myjavaserver.com/servlet/proteus.SmallFiles.ShowStore">here</a>. The source for this one is available <a href="http://www.smart-techie.com/web/programming/java-servlets/servlet-to-view-browser-information-while-visiting-a-site-in-html-format.html">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.smart-techie.com/blog/2007/01/your-gprs-mobile-is-spying-on-you/feed/</wfw:commentRss>
		<slash:comments>33</slash:comments>
		<georss:point featurename="Hyderabad, India">17.385044 78.486671</georss:point>
	</item>
		<item>
		<title>ControlTemplates in XAML</title>
		<link>http://www.smart-techie.com/blog/2006/12/controltemplates-in-xaml/</link>
		<comments>http://www.smart-techie.com/blog/2006/12/controltemplates-in-xaml/#comments</comments>
		<pubDate>Fri, 15 Dec 2006 19:16:46 +0000</pubDate>
		<dc:creator>Rohit</dc:creator>
				<category><![CDATA[Coding/ Programming]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://www.smart-techie.com/blog/2006/12/16/23/</guid>
		<description><![CDATA[XAML has this great concept of look-less controls. When we see a normal Win32 control like say a checkbox, we always assume that it will be a square box along with some text written to the left of it. Taking an example from our old friend WordPad, we would always expect a checkbox to look [...]]]></description>
			<content:encoded><![CDATA[<div class="entrytext">
<p>XAML has this <em>great</em> concept of look-less controls. When we see a normal Win32 control like say a checkbox, we always assume that it will be a square box along with some text written to the left of it. Taking an example from our old friend WordPad, we would always expect a checkbox to look like</p>
</div>
<p class="c6"><img src="http://i104.photobucket.com/albums/m190/rohit_sud/picidd5908DsDmfED-6ipuz2GcoaxIcYOOd.jpg" /></p>
<p>
  </p>
<p>Pretty boring, if you would ask a kid (or even a teenager!). Even though it makes the control much more usable (which is a <strong>very</strong> important thing BTW), it doesn&#8217;t look <strong><em>cool</em></strong>. So how does WPF help us here?</p>
<p>For starters, when you define a control, WPF doesn&#8217;t force to you to define the look of the control along with the functionality. Here comes the concept of a look-less control; a control whose behaviour is defined but whose look is not defined. For instance, we might define the behavior of the check box (it has a bool or a bool? property called IsChecked, it has a check mark to the left of the content, etc&#8230<img src='http://www.smart-techie.com/blog/smilies/msn_wink.gif' alt='&#59;&#41;' class='wp-smiley' width='19' height='19' title='&#59;&#41;' /> but leave it to the author to define the look for it. So we might have something like</p>
<div>
<table cellspacing="0" cellpadding="3" width="100%" border="0">
<tbody>
<tr>
<td width="50%">
<p><img src="http://i104.photobucket.com/albums/m190/rohit_sud/picidd5908DsDmfED-6ipuz2GcoaxLJao3I.jpg" align="left" /></p>
</td>
<td width="50%">Checked Checkbox<br />
          </td>
</tr>
<tr>
<td width="50%">
<p><img src="http://i104.photobucket.com/albums/m190/rohit_sud/picidd5908DsDmfED-6ipuz2GcoaxNvsd7G.jpg" align="left" /></p>
</td>
<td width="50%">Unchecked Checkbox</td>
</tr>
</tbody>
</table>
</div>
<p>
  </p>
<p>How to go about doing this? This is all done by redefining the ControlTemplate for the checkbox. ControlTemplate is basically what it says, it is a Template for the Control and decides how the control will <span class="c7">look</span> like. To achieve such a look we define the Style of the Checkbox as</p>
<blockquote>
<pre class="xmlcode"><code><span class="xml01">&lt;Style</span><span class="xml08"> </span><span class="xml03">x:Key</span><span class="xml08">=</span><span class="xml19">&quot;</span><span class="xml08">{</span><span class="xml03">x:Type</span><span class="xml08"> </span><span class="xml03">CheckBox</span><span class="xml08">}</span><span class="xml03">&quot;</span><span class="xml08"> </span><span class="xml03">TargetType</span><span class="xml08">=</span><span class="xml19">&quot;</span><span class="xml08">{</span><span class="xml03">x:Type</span><span class="xml08"> </span><span class="xml03">CheckBox</span><span class="xml08">}</span><span class="xml03">&quot;</span><span class="xml01">&gt;</span>
    <span class="xml01">&lt;Setter</span><span class="xml08"> </span><span class="xml03">Property</span><span class="xml08">=</span><span class="xml19">&quot;SnapsToDevicePixels&quot;</span><span class="xml08"> </span><span class="xml03">Value</span><span class="xml08">=</span><span class="xml19">&quot;true&quot;</span><span class="xml08">/</span><span class="xml01">&gt;</span>
    <span class="xml01">&lt;Setter</span><span class="xml08"> </span><span class="xml03">Property</span><span class="xml08">=</span><span class="xml19">&quot;OverridesDefaultStyle&quot;</span><span class="xml08"> </span><span class="xml03">Value</span><span class="xml08">=</span><span class="xml19">&quot;true&quot;</span><span class="xml08">/</span><span class="xml01">&gt;</span>
    <span class="xml01">&lt;Setter</span><span class="xml08"> </span><span class="xml03">Property</span><span class="xml08">=</span><span class="xml19">&quot;Template&quot;</span><span class="xml01">&gt;</span>
      <span class="xml01">&lt;Setter.Value&gt;</span>
        <span class="xml01">&lt;ControlTemplate</span><span class="xml08"> </span><span class="xml03">TargetType</span><span class="xml08">=</span><span class="xml19">&quot;</span><span class="xml08">{</span><span class="xml03">x:Type</span><span class="xml08"> </span><span class="xml03">CheckBox</span><span class="xml08">}</span><span class="xml03">&quot;</span><span class="xml01">&gt;</span>
          <span class="xml01">&lt;WrapPanel</span><span class="xml08"> </span><span class="xml03">Orientation</span><span class="xml08">=</span><span class="xml19">&quot;Horizontal&quot;</span><span class="xml01">&gt;</span>
            <span class="xml01">&lt;Border</span><span class="xml08"> </span><span class="xml03">x:Name</span><span class="xml08">=</span><span class="xml19">&quot;Border&quot;</span><span class="xml08">
              </span><span class="xml03">CornerRadius</span><span class="xml08">=</span><span class="xml19">&quot;0</span><span class="xml08">&#8221; </span><span class="xml01">&gt;</span>
              <span class="xml01">&lt;Image</span><span class="xml08"> </span><span class="xml03">Name</span><span class="xml08">=</span><span class="xml19">&quot;CheckMark&quot;</span><span class="xml08">
                      </span><span class="xml03">Height</span><span class="xml08">=</span><span class="xml19">&quot;</span><span class="xml08">{</span><span class="xml03">Binding</span><span class="xml08"> </span><span class="xml03">ElementName</span><span class="xml08">=</span><span class="xml19">MyContent</span><span class="xml08">, </span><span class="xml03">Path</span><span class="xml08">=</span><span class="xml19">ActualHeight</span><span class="xml08">}</span><span class="xml03">&quot;</span><span class="xml08"> </span><span class="xml03">Source</span><span class="xml08">=</span><span class="xml19">&quot;happy.gif&quot;</span><span class="xml01">&gt;</span>
                <span class="xml01">&lt;Image.BitmapEffect&gt;</span>
                  <span class="xml01">&lt;OuterGlowBitmapEffect</span><span class="xml08"> </span><span class="xml03">GlowColor</span><span class="xml08">=</span><span class="xml19">&quot;Blue&quot;</span><span class="xml08"> </span><span class="xml03">GlowSize</span><span class="xml08">=</span><span class="xml19">&quot;2</span><span class="xml08">&#8221; </span><span class="xml03">Noise</span><span class="xml08">=</span><span class="xml19">&quot;.1</span><span class="xml08">&#8221; </span><span class="xml03">Opacity</span><span class="xml08">=</span><span class="xml19">&quot;0.8</span><span class="xml08">&quot;</span><span class="xml11">/&gt;</span>
                <span class="xml01">&lt;/Image.BitmapEffect&gt;</span>
              <span class="xml01">&lt;/Image&gt;</span>
            <span class="xml01">&lt;/Border&gt;</span>
            <span class="xml01">&lt;ContentPresenter</span><span class="xml08"> </span><span class="xml03">Margin</span><span class="xml08">=</span><span class="xml19">&quot;4</span><span class="xml08">,</span><span class="xml05">0</span><span class="xml08">,</span><span class="xml05">0</span><span class="xml08">,</span><span class="xml05">0</span><span class="xml08">&quot; </span><span class="xml03">Name</span><span class="xml08">=</span><span class="xml19">&quot;MyContent&quot;</span><span class="xml08">
              </span><span class="xml03">VerticalAlignment</span><span class="xml08">=</span><span class="xml19">&quot;Center&quot;</span><span class="xml08">
              </span><span class="xml03">HorizontalAlignment</span><span class="xml08">=</span><span class="xml19">&quot;Left&quot;</span><span class="xml08">
              </span><span class="xml03">RecognizesAccessKey</span><span class="xml08">=</span><span class="xml19">&quot;True&quot;</span><span class="xml08">/</span><span class="xml01">&gt;</span>
          <span class="xml01">&lt;/WrapPanel&gt;</span>
          <span class="xml01">&lt;ControlTemplate.Triggers&gt;</span>
            <span class="xml01">&lt;Trigger</span><span class="xml08"> </span><span class="xml03">Property</span><span class="xml08">=</span><span class="xml19">&quot;IsChecked&quot;</span><span class="xml08"> </span><span class="xml03">Value</span><span class="xml08">=</span><span class="xml19">&quot;</span><span class="xml08">{</span><span class="xml03">x:Null</span><span class="xml08">}</span><span class="xml03">&quot;</span><span class="xml01">&gt;</span>
              <span class="xml01">&lt;Setter</span><span class="xml08"> </span><span class="xml03">TargetName</span><span class="xml08">=</span><span class="xml19">&quot;CheckMark&quot;</span><span class="xml08"> </span><span class="xml03">Property</span><span class="xml08">=</span><span class="xml19">&quot;Source&quot;</span><span class="xml08"> </span><span class="xml03">Value</span><span class="xml08">=</span><span class="xml19">&quot;unsure.gif&quot;</span><span class="xml08"> </span><span class="xml11">/&gt;</span>
              <span class="xml01">&lt;Setter</span><span class="xml08"> </span><span class="xml03">Property</span><span class="xml08">=</span><span class="xml19">&quot;BitmapEffect&quot;</span><span class="xml08"> </span><span class="xml03">TargetName</span><span class="xml08">=</span><span class="xml19">&quot;CheckMark&quot;</span><span class="xml01">&gt;</span>
                <span class="xml01">&lt;Setter.Value&gt;</span>
                  <span class="xml01">&lt;OuterGlowBitmapEffect</span><span class="xml08"> </span><span class="xml03">GlowColor</span><span class="xml08">=</span><span class="xml19">&quot;Black&quot;</span><span class="xml08"> </span><span class="xml03">GlowSize</span><span class="xml08">=</span><span class="xml19">&quot;2</span><span class="xml08">&quot; </span><span class="xml03">Noise</span><span class="xml08">=</span><span class="xml19">&quot;.1</span><span class="xml08">&quot;
                  </span><span class="xml03">Opacity</span><span class="xml08">=</span><span class="xml19">&quot;0.8</span><span class="xml08">&quot; </span><span class="xml11">/&gt;</span>
                <span class="xml01">&lt;/Setter.Value&gt;</span>
              <span class="xml01">&lt;/Setter&gt;</span>
            <span class="xml01">&lt;/Trigger&gt;</span>
            <span class="xml01">&lt;Trigger</span><span class="xml08"> </span><span class="xml03">Property</span><span class="xml08">=</span><span class="xml19">&quot;IsChecked&quot;</span><span class="xml08"> </span><span class="xml03">Value</span><span class="xml08">=</span><span class="xml19">&quot;false&quot;</span><span class="xml01">&gt;</span>
              <span class="xml01">&lt;Setter</span><span class="xml08"> </span><span class="xml03">TargetName</span><span class="xml08">=</span><span class="xml19">&quot;CheckMark&quot;</span><span class="xml08"> </span><span class="xml03">Property</span><span class="xml08">=</span><span class="xml19">&quot;Source&quot;</span><span class="xml08"> </span><span class="xml03">Value</span><span class="xml08">=</span><span class="xml19">&quot;sad.gif&quot;</span><span class="xml08"> </span><span class="xml11">/&gt;</span>
              <span class="xml01">&lt;Setter</span><span class="xml08"> </span><span class="xml03">Property</span><span class="xml08">=</span><span class="xml19">&quot;BitmapEffect&quot;</span><span class="xml08"> </span><span class="xml03">TargetName</span><span class="xml08">=</span><span class="xml19">&quot;CheckMark&quot;</span><span class="xml08"> </span><span class="xml03">Value</span><span class="xml08">=</span><span class="xml19">&quot;</span><span class="xml08">{</span><span class="xml03">x:Null</span><span class="xml08">}</span><span class="xml03">&quot;/</span><span class="xml01">&gt;</span>
            <span class="xml01">&lt;/Trigger&gt;</span>
            <span class="xml01">&lt;Trigger</span><span class="xml08"> </span><span class="xml03">Property</span><span class="xml08">=</span><span class="xml19">&quot;IsMouseOver&quot;</span><span class="xml08"> </span><span class="xml03">Value</span><span class="xml08">=</span><span class="xml19">&quot;true&quot;</span><span class="xml01">&gt;</span>
              <span class="xml01">&lt;Setter</span><span class="xml08"> </span><span class="xml03">Property</span><span class="xml08">=</span><span class="xml19">&quot;BitmapEffect&quot;</span><span class="xml08"> </span><span class="xml03">TargetName</span><span class="xml08">=</span><span class="xml19">&quot;CheckMark&quot;</span><span class="xml01">&gt;</span>
                <span class="xml01">&lt;Setter.Value&gt;</span>
                  <span class="xml01">&lt;OuterGlowBitmapEffect</span><span class="xml08"> </span><span class="xml03">GlowColor</span><span class="xml08">=</span><span class="xml19">&quot;Red&quot;</span><span class="xml08"> </span><span class="xml03">GlowSize</span><span class="xml08">=</span><span class="xml19">&quot;2</span><span class="xml08">&quot; </span><span class="xml03">Noise</span><span class="xml08">=</span><span class="xml19">&quot;.1</span><span class="xml08">&quot;
                  </span><span class="xml03">Opacity</span><span class="xml08">=</span><span class="xml19">&quot;0.8</span><span class="xml08">&quot; </span><span class="xml11">/&gt;</span>
                <span class="xml01">&lt;/Setter.Value&gt;</span>
              <span class="xml01">&lt;/Setter&gt;</span>
            <span class="xml01">&lt;/Trigger&gt;</span>
            <span class="xml01">&lt;Trigger</span><span class="xml08"> </span><span class="xml03">Property</span><span class="xml08">=</span><span class="xml19">&quot;IsPressed&quot;</span><span class="xml08"> </span><span class="xml03">Value</span><span class="xml08">=</span><span class="xml19">&quot;true&quot;</span><span class="xml01">&gt;</span>
              <span class="xml01">&lt;Setter</span><span class="xml08"> </span><span class="xml03">Property</span><span class="xml08">=</span><span class="xml19">&quot;BitmapEffect&quot;</span><span class="xml08"> </span><span class="xml03">TargetName</span><span class="xml08">=</span><span class="xml19">&quot;CheckMark&quot;</span><span class="xml01">&gt;</span>
                <span class="xml01">&lt;Setter.Value&gt;</span>
                  <span class="xml01">&lt;OuterGlowBitmapEffect</span><span class="xml08"> </span><span class="xml03">GlowColor</span><span class="xml08">=</span><span class="xml19">&quot;Yellow&quot;</span><span class="xml08"> </span><span class="xml03">GlowSize</span><span class="xml08">=</span><span class="xml19">&quot;2</span><span class="xml08">&quot; </span><span class="xml03">Noise</span><span class="xml08">=</span><span class="xml19">&quot;.1</span><span class="xml08">&quot;
                  </span><span class="xml03">Opacity</span><span class="xml08">=</span><span class="xml19">&quot;0.8</span><span class="xml08">&quot; </span><span class="xml11">/&gt;</span>
                <span class="xml01">&lt;/Setter.Value&gt;</span>
              <span class="xml01">&lt;/Setter&gt;</span>
            <span class="xml01">&lt;/Trigger&gt;</span>
            <span class="xml01">&lt;Trigger</span><span class="xml08"> </span><span class="xml03">Property</span><span class="xml08">=</span><span class="xml19">&quot;IsEnabled&quot;</span><span class="xml08"> </span><span class="xml03">Value</span><span class="xml08">=</span><span class="xml19">&quot;false&quot;</span><span class="xml01">&gt;</span>
              <span class="xml01">&lt;Setter</span><span class="xml08"> </span><span class="xml03">TargetName</span><span class="xml08">=</span><span class="xml19">&quot;Border&quot;</span><span class="xml08"> </span><span class="xml03">Property</span><span class="xml08">=</span><span class="xml19">&quot;Background&quot;</span><span class="xml08"> </span><span class="xml03">Value</span><span class="xml08">=</span><span class="xml19">&quot;</span><span class="xml08">{</span><span class="xml03">StaticResource</span><span class="xml08"> </span><span class="xml03">DisabledBackgroundBrush</span><span class="xml08">}</span><span class="xml03">&quot;</span><span class="xml08"> </span><span class="xml11">/&gt;</span>
              <span class="xml01">&lt;Setter</span><span class="xml08"> </span><span class="xml03">TargetName</span><span class="xml08">=</span><span class="xml19">&quot;Border&quot;</span><span class="xml08"> </span><span class="xml03">Property</span><span class="xml08">=</span><span class="xml19">&quot;BorderBrush&quot;</span><span class="xml08"> </span><span class="xml03">Value</span><span class="xml08">=</span><span class="xml19">&quot;</span><span class="xml08">{</span><span class="xml03">StaticResource</span><span class="xml08"> </span><span class="xml03">DisabledBorderBrush</span><span class="xml08">}</span><span class="xml03">&quot;</span><span class="xml08"> </span><span class="xml11">/&gt;</span>
              <span class="xml01">&lt;Setter</span><span class="xml08"> </span><span class="xml03">Property</span><span class="xml08">=</span><span class="xml19">&quot;Foreground&quot;</span><span class="xml08"> </span><span class="xml03">Value</span><span class="xml08">=</span><span class="xml19">&quot;</span><span class="xml08">{</span><span class="xml03">StaticResource</span><span class="xml08"> </span><span class="xml03">DisabledForegroundBrush</span><span class="xml08">}</span><span class="xml03">&quot;/</span><span class="xml01">&gt;</span>
            <span class="xml01">&lt;/Trigger&gt;</span>
          <span class="xml01">&lt;/ControlTemplate.Triggers&gt;</span>
        <span class="xml01">&lt;/ControlTemplate&gt;</span>
      <span class="xml01">&lt;/Setter.Value&gt;</span>
    <span class="xml01">&lt;/Setter&gt;</span>
  <span class="xml01">&lt;/Style&gt;</span></code></pre>
</blockquote>
<p>Let&#226;&#8364;&#8482;s dissect this monster to see how it works <img class="wp-smiley" title=":)" height="21" alt=":)" src="http://www.smart-techie.com/blog/smilies/msn_smiley.png" width="21" /></p>
<p>The first line</p>
<blockquote>
<pre class="xmlcode"><code><span class="xml01">&lt;Style</span><span class="xml08"> </span><span class="xml03">x:Key</span><span class="xml08">=</span><span class="xml19">&quot;</span><span class="xml08">{</span><span class="xml03">x:Type</span><span class="xml08"> </span><span class="xml03">CheckBox</span><span class="xml08">}</span><span class="xml03">&quot;</span><span class="xml08"> </span><span class="xml03">TargetType</span><span class="xml08">=</span><span class="xml19">&quot;</span><span class="xml08">{</span><span class="xml03">x:Type</span><span class="xml08"> </span><span class="xml03">CheckBox</span><span class="xml08">}</span><span class="xml03">&quot;</span><span class="xml01">&gt;</span></code></pre>
</blockquote>
<p><span class="c10"><span class="c9">means that this is a Style being defined for Type CheckBox in the namespace referred to by x which is <span class="c8">http://schemas.microsoft.com/winfx/2006/xaml</span> in my case.</span></span></p>
<p class="c11">Moving on, the core part of the Style is in defining the ControlTemplarte.</p>
<blockquote>
<pre class="xmlcode"> <code><span class="xml01">&lt;ControlTemplate</span><span class="xml08"> </span><span class="xml03">TargetType</span><span class="xml08">=</span><span class="xml19">&quot;</span><span class="xml08">{</span><span class="xml03">x:Type</span><span class="xml08"> </span><span class="xml03">CheckBox</span><span class="xml08">}</span><span class="xml03">&quot;</span><span class="xml01">&gt;</span></code></pre>
</blockquote>
<p>Note that I could have defined the ControlTemplate for the CheckBox using CheckBox.ControlTemplate but have chosen to implement it using a Style as it is quite common to do so.</p>
<p class="c11">It is in this ControlTemplate that I define the <span class="c7">look</span> of the CheckBox. Here I say that it should be a <span class="c12">WrapPanel</span> which should contain a <span class="c12">Border</span> which has an <span class="c12">Image</span> and a <span class="c12">ContentPresenter</span> in it. You can read more about the <span class="c12">ContentPresenter</span> in MSDN, it is just a <span class="c12">UIElement</span> which can contain and display any other object. The <span class="c12">Image</span> has a binding to the Height of the <span class="c12">ContentPresenter</span> so that it resizes automatically to the Height of the content, else you might have a case when you have text with FontSize 36 points but you checkmark is just 10 pixels high. That&#8217;s it! That&#8217;s the definition of our customized CheckBox; could it be any easier?</p>
<p class="c11">I added some Triggers to change the Image in the CheckBox depending on its state. I have added an image when IsChecked is null to account for a Tri State CheckBox. I also added some BitmapEffects for a Glow (just to make it more pretty!)</p>
<p class="c11">Go through the code, it isn&#8217;t very hard to understand or comprehend. Now whenever you wish your CheckBox to use this style, simply use it as</p>
<blockquote>
<pre class="xmlcode"><code><span class="xml01">&lt;CheckBox</span><span class="xml08"> </span><span class="xml03">Margin</span><span class="xml08">=</span><span class="xml19">&quot;8</span><span class="xml08">&#8221; </span><span class="xml03">Style</span><span class="xml08">=</span><span class="xml19">&quot;myCheckBoxStyle&quot;</span><span class="xml01">&gt;</span>
    <span class="xml01">&lt;TextBlock</span><span class="xml08"> </span><span class="xml03">FontSize</span><span class="xml08">=</span><span class="xml19">&quot;20&quot;</span><span class="xml01">&gt;</span>Happy or Sad?<span class="xml01">&lt;/TextBlock&gt;</span>
<span class="xml01">&lt;/CheckBox&gt;</span></code></pre>
</blockquote>
<p>If you find this article useful, do comment and let me know</p>
<p><a href="http://www.dotnetkicks.com/kick/?url=http://www.smart-techie.com/blog/2006/12/controltemplates-in-xaml/"><img alt="kick it on DotNetKicks.com" src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http://www.smart-techie.com/blog/2006/12/controltemplates-in-xaml/" border="0" /></a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.smart-techie.com/blog/2006/12/controltemplates-in-xaml/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Random thoughts == System.Random(object thought)</title>
		<link>http://www.smart-techie.com/blog/2006/09/random-thoughts-systemrandomobject-thought/</link>
		<comments>http://www.smart-techie.com/blog/2006/09/random-thoughts-systemrandomobject-thought/#comments</comments>
		<pubDate>Fri, 15 Sep 2006 19:30:03 +0000</pubDate>
		<dc:creator>Rohit</dc:creator>
				<category><![CDATA[Coding/ Programming]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[WPF]]></category>

		<guid isPermaLink="false">http://www.smart-techie.com/blog/2006/09/general/random-thoughts-systemrandomobject-thought/</guid>
		<description><![CDATA[This is the state of my mind at this precise moment. Thinking of code as well as trying to think of something else. Its Friday night, I&#8217;ve had my dinner and am really tired to do any more work. Thought of updating my blog, as it has been weeks since I last did it. Just [...]]]></description>
			<content:encoded><![CDATA[<p>This is the state of my mind at this precise moment. Thinking of code as well as trying to think of something else. Its Friday night, I&#8217;ve had my dinner and am really tired to do any more work. Thought of updating my blog, as it has been weeks since I last did it. Just when I had thought I was going to be working on Java at Trilogy, I changed my plans for no <em>apparent</em> reason and came to Microsoft. Here one, obviously, won&#8217;t work on Java. But imagine my luck, I got to work on its cousin or should I say successor (as Microsoft would like to see it IMHO; I don&#8217;t express any opinions about and of the company, this is a personal blog). This is really cool, I mean working in a familiar domain, with System.Console.println replaced by System.Console.WriteLine most of the things are same. What is not same is the GUI features provided by WinFX or Avalon framework. They are really cool, I mean we can use markup to generate complete UIs which are absolutely STUNNING. In Java or even VC++, I would have to spend hours to write the code, but thanks to the combination of C# and XAML, its a piece of cake to do so.</p>
<p>&nbsp;</p>
<p>&nbsp;<a href='http://www.smart-techie.com/blog/wp-content/uploads/2007/05/winfx.png' title='WinFX' rel="lightbox"><img src='http://www.smart-techie.com/blog/wp-content/uploads/2007/05/winfx.thumbnail.png' alt='WinFX' />WinFX / Avalon</a> </p>
<p>&nbsp;</p>
<p><u><strong>XAML, the new tool (pronounced <em>zammel)</em></strong></u> XAML is an XML based system to create GUIs. BUt it can do much more than that. It provides for cool concepts like data binding. This is something like an element will automatically update its properties based on the properties of another element. So you can have the color of text in a TextBox to be the same as the Background of the Button which is an image. It as simple as writing a markup without any lines of code in C#. The power of XAML is truly phenomenal! <strong>Code: </strong></p>
<blockquote><p><code><span style="color: #0000ff">&lt;</span><span style="color: #800000">Page </span><span style="color: #ff0000">xmlns</span><span style="color: #0000ff">="http://schemas.microsoft.com/winfx/2006/xaml/presentation"</span><span style="color: #ff0000"> xmlns: sys</span><span style="color: #0000ff">="clr-namespace: System;assembly=mscorlib"</span><span style="color: #ff0000"><br />
 xmlns: x</span><span style="color: #0000ff">="http://schemas.microsoft.com/winfx/2006/xaml"</span><span style="color: #ff0000"> </span><span style="color: #0000ff">&gt;</span><span style="color: #000000"><br />
</span><span style="color: #0000ff">&lt;</span><span style="color: #800000">Page</span><span style="color: #ff0000">.Resources</span><span style="color: #0000ff">&gt;</span><span style="color: #000000"><br />
</span><span style="color: #0000ff">&lt;</span><span style="color: #800000">ImageBrush </span><span style="color: #ff0000">x:Key</span><span style="color: #0000ff">="SomeImage"</span><span style="color: #ff0000"> ImageSource</span><span style="color: #0000ff">="C:\\Documents and Settings\\All Users\\Documents\\My Pictures\\Sample Pictures\\Sunset.jpg"</span><span style="color: #0000ff">/&gt;</span><span style="color: #000000"><br />
</span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">Page.Resources</span><span style="color: #0000ff">&gt;</span><span style="color: #000000"><br />
</span><span style="color: #0000ff">&lt;</span><span style="color: #800000">StackPanel </span><span style="color: #ff0000">Height</span><span style="color: #0000ff">="150"</span><span style="color: #ff0000"> Width</span><span style="color: #0000ff">="350"</span><span style="color: #0000ff">&gt;</span><span style="color: #000000"><br />
    </span><span style="color: #0000ff">&lt;</span><span style="color: #800000">Button </span><span style="color: #ff0000">Background</span><span style="color: #0000ff">="{StaticResource SomeImage}"</span><span style="color: #ff0000"> Name</span><span style="color: #0000ff">="button"</span><span style="color: #ff0000"> Height</span><span style="color: #0000ff">="60"</span><span style="color: #0000ff">/&gt;</span><span style="color: #000000"><br />
      </span><span style="color: #0000ff">&lt;</span><span style="color: #800000">TextBox </span><span style="color: #ff0000">Foreground</span><span style="color: #0000ff">="{Binding Path=Background, ElementName=button}"</span><span style="color: #ff0000"> FontSize</span><span style="color: #0000ff">="20"</span><span style="color: #0000ff">&gt;</span><span style="color: #000000"><br />
        Some Text with a picture background<br />
    </span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">TextBox</span><span style="color: #0000ff">&gt;</span><span style="color: #000000"><br />
</span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">StackPanel</span><span style="color: #0000ff">&gt;</span><span style="color: #000000"><br />
</span><span style="color: #0000ff">&lt;/</span><span style="color: #800000">Page</span><span style="color: #0000ff">&gt;</span><span style="color: #000000"></p>
<p></span><br />
</code>
</p></blockquote>
<p>And the result! </p>
<p><img alt="XAML screenshot" src="http://i104.photobucket.com/albums/m190/rohit_sud/xaml.jpg" border="0" /> Isn&#8217;t this great? Just a few lines of code was needed to produce such an application!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.smart-techie.com/blog/2006/09/random-thoughts-systemrandomobject-thought/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Creating Dynamic Avatars for forums, Php scripting + .htaccess in Apache</title>
		<link>http://www.smart-techie.com/blog/2006/08/creating-dynamic-avatars-for-forums-php-scripting-htaccess-in-apache/</link>
		<comments>http://www.smart-techie.com/blog/2006/08/creating-dynamic-avatars-for-forums-php-scripting-htaccess-in-apache/#comments</comments>
		<pubDate>Sat, 19 Aug 2006 15:27:26 +0000</pubDate>
		<dc:creator>Rohit</dc:creator>
				<category><![CDATA[Coding/ Programming]]></category>
		<category><![CDATA[Computing]]></category>

		<guid isPermaLink="false">http://www.smart-techie.com/blog/2006/08/computing/creating-dynamic-avatars-for-forums-php-scripting-htaccess-in-apache/</guid>
		<description><![CDATA[Digg This Now you guys have probably seen that the avatars on this forum are static, they have to be manually changed before you can use a new one. Now take a look at my avatar urlhttp://www.smart-techie.com/images/avatar.gifNow what happens when you view it? You see a jpg or a gif image.Try accessing the same url [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://digg.com/submit?phase=2&amp;url=http://www.smart-techie.com/blog/2006/08/computing/creating-dynamic-avatars-for-forums-php-scripting-htaccess-in-apache/"><b>Digg This</b></a>
<p>Now you guys have probably seen that the avatars on this forum are static, they have to be manually changed before you can use a new one. <img class="wp-smiley" title=":-?" height="18" alt=":-?" src="http://www.smart-techie.com/blog/smilies/yahoo_think.gif" width="18"><br />Now take a look at my avatar url<br /><a href="http://www.smart-techie.com/images/avatar.gif" target="_blank">http://www.smart-techie.com/images/avatar.gif</a><br />Now what happens when you view it? You see a jpg or a gif image.<br />Try accessing the same url 5 or more timesâ€¦ Did you notice something?<br />The picture changes automatically at random (amongst a set of 3 pics) If you didnâ€™t see this try more times <img class="wp-smiley" title=";)" height="18" alt=";)" src="http://www.smart-techie.com/blog/smilies/yahoo_wink.gif" width="18"><br />How does it happen?<br />The first part is a VERY simple php script on my webserver (you need a hosting account with php scripting enabled for running scripts). Also (in Linux) CHMOD the files to give execute permission to everyone.<br />The php code that I am using is </p>
<blockquote><pre><code><span style="color: #000000">&lt;?</span><span style="color: #000000">php
</span><span style="color: #800080">$random</span><span style="color: #000000"> </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #008080">rand</span><span style="color: #000000">(</span><span style="color: #000000">1</span><span style="color: #000000">,</span><span style="color: #000000">3</span><span style="color: #000000">);

</span><span style="color: #0000ff">switch</span><span style="color: #000000"> (</span><span style="color: #800080">$random</span><span style="color: #000000">)

{
</span><span style="color: #0000ff">case</span><span style="color: #000000"> </span><span style="color: #000000">1</span><span style="color: #000000">:</span><span style="color: #008080">header</span><span style="color: #000000">(</span><span style="color: #000000">"</span><span style="color: #000000">location: /images/konqi.jpg</span><span style="color: #000000">"</span><span style="color: #000000"> ) ;

</span><span style="color: #0000ff">break</span><span style="color: #000000">;

</span><span style="color: #0000ff">case</span><span style="color: #000000"> </span><span style="color: #000000">2</span><span style="color: #000000">:</span><span style="color: #008080">header</span><span style="color: #000000">(</span><span style="color: #000000">"</span><span style="color: #000000">location: /images/Homer.gif</span><span style="color: #000000">"</span><span style="color: #000000"> ) ;

</span><span style="color: #0000ff">break</span><span style="color: #000000">;

</span><span style="color: #0000ff">case</span><span style="color: #000000"> </span><span style="color: #000000">3</span><span style="color: #000000">:</span><span style="color: #008080">header</span><span style="color: #000000">(</span><span style="color: #000000">"</span><span style="color: #000000">location: /images/Hobbes.gif</span><span style="color: #000000">"</span><span style="color: #000000"> ) ;

</span><span style="color: #0000ff">break</span><span style="color: #000000">;
}

</span><span style="color: #008080">header</span><span style="color: #000000">(</span><span style="color: #000000">'</span><span style="color: #000000">Cache-Control: no-store, no-cache, must-revalidate</span><span style="color: #000000">'</span><span style="color: #000000">) ;

</span><span style="color: #008080">header</span><span style="color: #000000">(</span><span style="color: #000000">'</span><span style="color: #000000">Cache-Control: post-check=0, pre-check=0</span><span style="color: #000000">'</span><span style="color: #000000">,</span><span style="color: #000000"> </span><span style="color: #0000ff">FALSE</span><span style="color: #000000">) ;

</span><span style="color: #008080">header</span><span style="color: #000000">(</span><span style="color: #000000">"</span><span style="color: #000000">Expires: Mon, 26 Jul 1997 05:00:00 GMT</span><span style="color: #000000">"</span><span style="color: #000000"> ) ; </span><span style="color: #008000">//</span><span style="color: #008000"> Date in the past</span><span style="color: #008000">
</span><span style="color: #000000">
</span><span style="color: #008080">header</span><span style="color: #000000">(</span><span style="color: #000000">'</span><span style="color: #000000">Pragma: no-cache</span><span style="color: #000000">'</span><span style="color: #000000">) ;

</span><span style="color: #000000">?&gt;</span></code></pre>
</blockquote>
<p><!--ec2-->Doesnâ€™t take a genius to figure out what is going on here. Anyways Iâ€™ll explain <img class="wp-smiley" title=":)" height="18" alt=":)" src="http://www.smart-techie.com/blog/smilies/yahoo_smiley.gif" width="18"><br />1. A random number is chosen between 1 and 3 and the corresponding image is displayed. This is done by the header function which is used to send HTTP headers ( a temporary redirect HTTP 302 response) This says that the resource that you want to see is at some other location).2. The headers at the end are DESPERATE ones which tell the browser â€œPlease donâ€™t cache the images or you will spoil my image changing effect?â€ Now to save bandwidth and load pages faster, browsers store copies of resources that they retrieved from the Internet on your disk in a location called cache/Temporary Internet files or similar location. Try about:cache in mozilla to see whats in it.<br />Also an expiry date is given in past so that the browser thinks â€œOh, this content that I retrieved has become old. I shouldnâ€™t cache it. Next time Iâ€™ll ask for a fresh copyâ€ Sweetâ€¦<br />Now the file is saved as somename.php on your server and on running it the effect of changing avatars is complete.<br />2. Now for the final bit. Try pasting this url in your avatar field and the forum complains. It wants the URL to end in gif/jpg or whatever. So how to bypass this? Simple. Use the Apache .htaccess file (not available on IIS)<br />.htaccess is a file that stores settings on a folder basis on you webserver. I modify this file to add a statement<br /><code></code></p>
<blockquote><pre><code>Redirect /avatar.gif http://www.website.com/images/somefile.php</code></pre>
</blockquote>
<p>There you have it! It is always easy to fool software.<br />Try this at least once to see the power of web scripting. </p>
<p>Addendum</p>
<blockquote><p>&#8220;The great thing about mod_rewrite is it gives you all the   configurability and flexibility of Sendmail. The downside to mod_rewrite  is that it gives you all the configurability and flexibility of Sendmail.&#8221;<br />
&#8211;<br />
Brian Behlendorf, Apache Group</p></blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.smart-techie.com/blog/2006/08/creating-dynamic-avatars-for-forums-php-scripting-htaccess-in-apache/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

