<?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>winJade &#187; C#</title>
	<atom:link href="http://winjade.net/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://winjade.net</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Sun, 27 Jun 2010 23:47:44 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>A lesson on infinite loops</title>
		<link>http://winjade.net/2009/01/lesson-on-infinite-loops/</link>
		<comments>http://winjade.net/2009/01/lesson-on-infinite-loops/#comments</comments>
		<pubDate>Sat, 03 Jan 2009 20:57:35 +0000</pubDate>
		<dc:creator>Bryant Zadegan</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[dev fail]]></category>
		<category><![CDATA[in depth]]></category>
		<category><![CDATA[Infinite Loop]]></category>
		<category><![CDATA[Zune 30]]></category>

		<guid isPermaLink="false">http://www.aeroxp.org/?p=756</guid>
		<description><![CDATA[
pre {
    font-size: medium;
    }

&#160;
&#160;
Yesterday, I took a look at the varying perspectives taken with regards to the Zune 30 debacle. Today, I’ll take a look at what exactly led the Zune 30s to freeze. If you’d like to see the code for the entire driver, click here.
Below the [...]]]></description>
			<content:encoded><![CDATA[<style  TYPE="text/css">
pre {
    font-size: medium;
    }
</style>
<p>&#160;<img style="border-top-width: 0px; border-left-width: 0px; float: none; border-bottom-width: 0px; margin-left: auto; margin-right: auto; display: block; border-right-width: 0px" title="quality assurance baby" border="0" alt="quality assurance baby" src="http://winjade.net/wp-content/uploads/2009/01/qualityassurancebaby1.png" width="640" height="480" /></p>
<p>&#160;</p>
<p>Yesterday, <a href="http://www.aeroxp.org/2009/01/zune-30-bug-overinflated/" target="_blank">I took a look</a> at the varying perspectives taken with regards to the Zune 30 debacle. Today, I’ll take a look at what exactly led the Zune 30s to freeze. If you’d like to see the code for the entire driver, <a href="http://pastie.org/349916">click here</a>.</p>
<p>Below the fold lies a sufficiently sized code sample with everything you’ll need to understand what happened with the Zune 30 bug.</p>
<p><script type="text/javascript">
digg_url = 'http://www.aeroxp.org/2009/01/lesson-on-infinite-loops/';
</script><br />
<script src="http://digg.com/tools/diggthis.js" type="text/javascript"></script> </p>
<p> <span id="more-756"></span>
</p>
<p>Relevant code is non-gray code:</p>
<blockquote><pre><font color="#ff00ff"><font color="#000000">#</font>define</font> <font color="#000080">ORIGINYEAR</font> <font color="#ff0000">1980</font> <font color="#000000">
<font color="#008000">BOOL</font> ConvertDays(<font color="#008000">UINT32</font> days<font color="#808080">, SYSTEMTIME* lpTime</font>)
{
    <font color="#008000">int</font> <font color="#808080">dayofweek, month,</font> year;
    <font color="#808080">UINT8 *month_tab;</font>

    <font color="#808080">//Calculate current day of the week
    dayofweek = GetDayOfWeek(days);</font>

    year = <font color="#000080">ORIGINYEAR</font>;

    <font color="#800000">while</font> (days &gt; <font color="#ff0000">365</font>)
    {
        <font color="#800000">if</font> (IsLeapYear(year))
        {
            <font color="#800000">if</font> (days &gt; <font color="#ff0000">366</font>)
            {
                days -= <font color="#ff0000">366</font>;
                year += <font color="#ff0000">1</font>;
            }
        }
        <font color="#800000">else</font>
        {
            days -= <font color="#ff0000">365</font>;
            year += <font color="#ff0000">1</font>;
        }
    }

<font color="#808080">    // Determine whether it is a leap year
    month_tab = (UINT8 *)((IsLeapYear(year))? monthtable_leap : monthtable);

    for (month=0; month&lt;12; month++)
    {
        if (days &lt;= month_tab[month])
            break;
        days -= month_tab[month];
    }

    month += 1;

    lpTime-&gt;wDay = days;
    lpTime-&gt;wDayOfWeek = dayofweek;
    lpTime-&gt;wMonth = month;
    lpTime-&gt;wYear = year;

    return TRUE;
</font>}</font></pre>
</blockquote>
<p>So what’s the purpose of the while loop? To put it simply, it’s designed to get the number of years from the number of days since 1980 as well as a remainder of days out of the current year. Well, let’s look at how the code plays out in my confusingly simple <em>While Loop in Plain English™</em>. Keep in mind that you’re <em>never </em>supposed to retry the loop without committing some sort of action which will ultimately exit the loop:</p>
<ol>
<li>year is set to ORIGINYEAR. Since ORIGINYEAR is 1980, any addition to year is added on top of 1980. This is fine because the hardware is passing the number of days since January 1, 1980. </li>
<li>Is the number of days greater than 365? If so, proceed. Otherwise, skip to number 6. </li>
<li>Is the current year a leap year? If so, proceed. Otherwise, subtract 365 from the number of days, add 1 to the number of years, and skip to number 5. </li>
<li>Is the number of days greater than 366? If so, subtract 366 from the number of days, add 1 to the number of years, and proceed. </li>
<li>retry number 2. </li>
<li>…</li>
<li>Profit? </li>
</ol>
<p>The problem lies in the fact that there is one case which can never escape the loop. If it’s a leap year, the number 366 will stay within the loop forever because 366 will never be greater than 366, but 366 will also always be greater than 365. 366 days will pass through, in our Plain English While Loop, without having any action committed upon it.</p>
<p>There’s a very quick solution to this: Since 366 is a valid remainder of days in a leap year (December 31 is the 366th day in a leap year), 366 can technically exit the loop without any problems. All we need to do is add a way to exit the loop when the number of days is 366. Let’s see how things go with this new breakpoint:</p>
<ol>
<li>year is set to ORIGINYEAR. Since ORIGINYEAR is 1980, any addition to year is added on top of 1980. This is fine because the hardware is passing the number of days since January 1, 1980. </li>
<li>Is the number of days greater than 365? If so, proceed. Otherwise, skip to number 6. </li>
<li>Is the current year a leap year? If so, proceed. Otherwise, subtract 365 from the number of days, add 1 to the number of years, and skip to number 5. </li>
<li>Is the number of days greater than 366? If so, subtract 366 from the number of days, add 1 to the number of years, and proceed. Otherwise, skip to number 6. </li>
<li>retry number 2. </li>
<li>…</li>
<li>Profit!</li>
</ol>
<p>Thanks to the break (represented by “Otherwise, skip to number 6”), 366 days can escape the loop properly. This would lead to the end result for December 31, 2008 being years == 2008 and days == 366.</p>
<p>This is what the break would look in the while loop:</p>
<blockquote>
<pre><font color="#000000">    <font color="#800000">while</font> (days &gt; <font color="#ff0000">365</font>)
    {
        <font color="#800000">if</font> (IsLeapYear(year))
        {
            <font color="#800000">if</font> (days &gt; <font color="#ff0000">366</font>)
            {
                days -= <font color="#ff0000">366</font>;
                year += <font color="#ff0000">1</font>;
            }
            <font color="#800000">else</font>
            {
                <font color="#800000">break;</font>
            }
        }
        <font color="#800000">else</font>
        {
            days -= <font color="#ff0000">365</font>;
            year += <font color="#ff0000">1</font>;
        }
    }
</font></pre>
</blockquote>
<p>Clarifying one quick thing: A number of people on outlets such as Digg suggested that changing “<font color="#800000">if</font> (days &gt; <font color="#ff0000">366</font>)” to “<font color="#800000">if</font> (days &gt;= <font color="#ff0000">366</font>)”:</p>
<p><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; display: inline; border-right-width: 0px" title="QAdigg" border="0" alt="QAdigg" src="http://winjade.net/wp-content/uploads/2009/01/qadigg.png" width="660" height="158" /></p>
<p>The problem with doing this is that the end result, upon hitting “<font color="#800000">if</font> (days &gt;= <font color="#ff0000">366</font>)” with the value 366, one would end up with the year going up by one and the operation “<font color="#ff0000">366 –</font>= <font color="#ff0000">366</font>” playing out. End result: December 31 of the leap year is suddenly January 0 of the following year.</p>
<p>&#160;</p>
<p><strong>Extra credit:</strong> This loop can be fixed such that the loop’s condition is based on the year’s status as a leap year. The change needed is simple:</p>
<blockquote>
<pre><font color="#000000">    <font color="#800000">while</font> (days &gt; (IsLeapYear(year) ? <font color="#ff0000">366</font> : <font color="#ff0000">365</font>))
    <font color="#008080">//The above line basically says “Is it a leap year? Check days against 366.
    //Otherwise, check it against 365”</font>
    {
        <font color="#800000">if</font> (IsLeapYear(year))
        {
            days -= <font color="#ff0000">366</font>;
            year += <font color="#ff0000">1</font>;
            <font color="#008080">/*You’ll notice we no longer need a break. The loop condition now
             *checks the days properly. Because the loop properly checks
             *whether days is greater than 365 or 366, the second days &gt; 366
             *check is no longer needed. */</font>
        }
        <font color="#800000">else</font>
        {
            days -= <font color="#ff0000">365</font>;
            year += <font color="#ff0000">1</font>;
        }
    }
</font></pre>
</blockquote>
<p>&#160;</p>
<p>However, if this change is made, IsLeapYear() is now being called twice. This is inefficient, especially on small devices (like the devices which would be using this driver). In order to reduce the number of times IsLeapYear() is being called, it would be best to store the value of IsLeapYear() in a variable.</p>
<blockquote>
<pre><font color="#000000">    <font color="#008000">int</font> leap = IsLeapYear(year);
    <font color="#008080">//leap is determined before the loop begins.
    //In this case, it would be true (1980 is a leap year)</font>
    <font color="#800000">while</font> (days &gt; (leap ? <font color="#ff0000">366</font> : <font color="#ff0000">365</font>))
    {
        <font color="#800000">if</font> (leap)
        {
            days -= <font color="#ff0000">366</font>;
            year += <font color="#ff0000">1</font>;
        }
        <font color="#800000">else</font>
        {
            days -= <font color="#ff0000">365</font>;
            year += <font color="#ff0000">1</font>;
        }
        leap = IsLeapYear(year);
        <font color="#008080">//leap is determined again now that the year has changed.</font>
    }
</font></pre>
</blockquote>
<p>This can actually be reduced even further. If you look at how the if/else statement is operating, you can see that it’s basically saying “If it’s a leap year, subtract 366. Otherwise, subtract 365,” which is similar to the while loop’s condition!</p>
<blockquote>
<pre><font color="#000000">    <font color="#008000">int</font> leap = IsLeapYear(year);
    <font color="#800000">while</font> (days &gt; (leap ? <font color="#ff0000">366</font> : <font color="#ff0000">365</font>))
    {
        days -= (leap ? <font color="#ff0000">366</font> : <font color="#ff0000">365</font>);
        <font color="#008080">//days is equal to days - 366 if it's a leap year,
        //                 days - 365 if it isn't.</font>
        year += <font color="#ff0000">1</font>;
        leap = IsLeapYear(year);
    }
</font></pre>
</blockquote>
<p>If you wanted to reduce the number of times (leap ? <font color="#ff0000">366</font> : <font color="#ff0000">365</font>) is checked, you could do this instead:</p>
<blockquote>
<pre><font color="#000000">    <font color="#008000">int</font> daysThisYear = (IsLeapYear(year) ? <font color="#ff0000">366</font> : <font color="#ff0000">365</font>);
    <font color="#008080">//The number of days in the current year is now calculated instead. </font>
    <font color="#800000">while</font> (days &gt; daysThisYear)
    {
        days -= daysThisYear;
        year += <font color="#ff0000">1</font>;
        daysThisYear = (IsLeapYear(year) ? <font color="#ff0000">366</font> : <font color="#ff0000">365</font>);
    }
</font></pre>
</blockquote>
<p>It’s questionable whether or not this code is any better than the original solution with the break, but this would be the “proper” way to do it (instead of using an arbitrary break in your code). It’s also easier to understand if you know C.</p>
<p>The irony in all of this is that this specific driver is used in multiple Windows CE devices, not just the Zune. This is why quality assurance is so necessary.</p>
<p><strong>There are three morals here: </strong></p>
<ol>
<li><strong>Test every condition which could happen in every single loop in your code </strong>(though, as Matt Boehm points out in the comments, this might be a <a href = "http://www.aeroxp.org/2009/01/lesson-on-infinite-loops/#comment-831">tad bit hard</a>). </li>
<li><strong>Don’t visit Digg for programming advice. What you’ll hear is questionable at best</strong> </li>
<li><strong>Make your code easy to read for other developers.</strong> </li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://winjade.net/2009/01/lesson-on-infinite-loops/feed/</wfw:commentRss>
		<slash:comments>157</slash:comments>
		</item>
		<item>
		<title>Tweak DWM from your programs, Part 2</title>
		<link>http://winjade.net/2008/08/tweak-dwm-part-2/</link>
		<comments>http://winjade.net/2008/08/tweak-dwm-part-2/#comments</comments>
		<pubDate>Wed, 27 Aug 2008 18:00:14 +0000</pubDate>
		<dc:creator>Stan</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[dwm]]></category>
		<category><![CDATA[wrapper]]></category>

		<guid isPermaLink="false">http://www.aeroxp.org/?p=146</guid>
		<description><![CDATA[Following the first part of the Tweak DWM from your programs series, we are going to explore the actual usage of the Desktop Window Manager API in a Windows Forms-based program. The test application&#8211;included in the source package&#8211;also makes use of GlassBar, an extension to the existing control set.

Using GlassLib in your program
GlassLib is going [...]]]></description>
			<content:encoded><![CDATA[<p>Following the <a title="Tweak DWM from your programs, Part 1" href="/2008/07/tweak-dwm-part-1/">first part</a> of the <strong>Tweak DWM from your programs</strong> series, we are going to explore the actual usage of the <strong>Desktop Window Manager API</strong> in a Windows Forms-based program. The test application&#8211;included in the <a href="http://stoyanoff.info/code/dwm/GlassLib_pub.zip">source package</a>&#8211;also makes use of <img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 3px 0px 0px; border-right-width: 0px" src="http://winjade.net/wp-content/uploads/2008/08/glassbar.png" border="0" alt="glassbar" width="16" height="16" /><strong>GlassBar</strong>, an extension to the existing control set.<br />
<span id="more-146"></span><br />
<h3>Using GlassLib in your program</h3>
<p><span style="font-family: courier new;"><a href="http://winjade.net/wp-content/uploads/2008/08/project.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 3px 0px 0px; border-right-width: 0px" src="http://winjade.net/wp-content/uploads/2008/08/project24.png" border="0" alt="project" width="16" height="15" /></a>GlassLib </span>is going to be main focus of this part of the series. It supersedes <span style="font-family: courier new;"><a href="http://winjade.net/wp-content/uploads/2008/08/project.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 3px 0px 0px; border-right-width: 0px" src="http://winjade.net/wp-content/uploads/2008/08/project24.png" border="0" alt="project" width="16" height="15" /></a>DwmWrapper </span>by implementing a framework around <img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 3px 0px 0px; border-right-width: 0px" src="http://winjade.net/wp-content/uploads/2008/08/glassbar.png" border="0" alt="glassbar" width="16" height="16" /><span style="font-family: Courier New;">GlassBar</span> which can be used as a complement to your visually-rich forms. In order to use this library in your project, you have to do the following:</p>
<ol>
<li>Include <strong>a)</strong> a reference to the DLL in your project from Visual Studio: <em>Project</em> menu =&gt;<em> Add reference</em>, or <strong>b)</strong> the actual code files to your C# project (adding them as links (shortcuts) as opposed to copying them is more suitable if you wish include the files in multiple projects and/or wish to make changes to the code yourself).</li>
<li>Remember that all of the functionality lies in the <img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 3px 0px 0px; border-right-width: 0px" src="http://winjade.net/wp-content/uploads/2008/08/namespace-thumb.png" border="0" alt="namespace_" width="15" height="15" /><span style="font-family: courier new;">GlassLib </span>namespace. You can use the <span style="font-family: courier new;"><span style="color: #0000ff;">using</span> </span>or <span style="color: #0000ff;">Imports</span> clause in the beginning of your C# or VB.NET code file, respectively, or the global imports if you code exclusively in VB.NET.</li>
</ol>
<h3>Exploring the GlassLib Test app</h3>
<p>Let&#8217;s begin exploring the framework by opening the project file for <span style="font-family: courier new;"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 3px 0px 0px; border-right-width: 0px" src="http://winjade.net/wp-content/uploads/2008/08/project24.png" border="0" alt="project" width="16" height="15" />GlassLibTest</span>. Included in the GlassLib solution, it depends on the <span style="font-family: courier new;"><a href="http://winjade.net/wp-content/uploads/2008/08/project.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 3px 0px 0px; border-right-width: 0px" src="http://winjade.net/wp-content/uploads/2008/08/project24.png" border="0" alt="project" width="16" height="15" /></a>GlassLib </span>project reference, which on its part has all the <span style="font-family: courier new;"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 3px 0px 0px; border-right-width: 0px" src="http://winjade.net/wp-content/uploads/2008/08/project24.png" border="0" alt="project" width="16" height="15" />DwmWrapper</span> code.</p>
<p>Open the <img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 3px 0px 0px; border-right-width: 0px" src="http://winjade.net/wp-content/uploads/2008/08/form.png" border="0" alt="form" width="15" height="15" />frmMain.cs file which is the form which is to be executed right at start up. As described in the first part, the first thing the class constructor <span><span style="font-family: Courier New;"><span style="color: #0000ff;">public</span> frmMain()</span></span> should do is to set <span style="font-family: courier new;"><span style="color: #2b91af;">Dwm</span>.ThrowExceptionTypes</span> to <span><span style="font-family: Courier New;"><span style="color: #2b91af;">DwmExceptionTypes</span>.None</span></span> in order to avoid any exceptions from being raised when DWM is not enabled or available on the OS, or if any function failed. The next thing the program does is to enable dragging from the Glass-extended area.</p>
<p><span style="font-family: Courier New;"><span style="color: #2b91af;">Dwm</span>.Glass[<span style="color: #0000ff;">this</span>].DragExtendedArea = <span style="color: #0000ff;">true</span>; </span>is useful if you want the extended area to act as the caption bar does, like in Windows Media Player. This is what the <img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 3px 0px 0px; border-right-width: 0px" src="http://winjade.net/wp-content/uploads/2008/08/glassbar.png" border="0" alt="glassbar" width="16" height="16" /><span style="font-family: Courier New;">GlassBar </span>toolbar does as well (more on this later in this part).</p>
<p><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 3px 0px 0px; border-right-width: 0px" src="http://winjade.net/wp-content/uploads/2008/08/toolbox.png" border="0" alt="toolbox" width="216" height="99" align="right" /> Switch to Designer mode (<em>View</em> <em>=&gt; Designer</em>) and you can see the layout of the form: the usual Windows Forms buttons, combo and text boxes et cetera, but also <img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 3px 0px 0px; border-right-width: 0px" src="http://winjade.net/wp-content/uploads/2008/08/glassbar.png" border="0" alt="glassbar" width="16" height="16" /><span style="font-family: Courier New;">GlassBar</span>. To add the latter to your own forms, open the Toolbox (<em>View =&gt; Toolbox</em>) and drag it to your form (see figure on right). The toolbar will automatically dock to the bottom of it and extend Glass from this side of the form. You are free to dock it to the whole form (or container for that matter) or one of the other sides.</p>
<p>Because DWM only supports Glass on top-level windows, in designer mode <img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 3px 0px 0px; border-right-width: 0px" src="http://winjade.net/wp-content/uploads/2008/08/glassbar.png" border="0" alt="glassbar" width="16" height="16" /><span style="font-family: Courier New;">GlassBar</span> falls back to UXTheme API in order to get the theme-specific background image or brush which is suitable for all non-Aero Glass themes such as Aero Basic, Luna (on Windows XP) or Windows Classic on any supported Windows version. On top of this, you can choose to write your own <a href="http://winjade.net/wp-content/uploads/2008/08/class.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 3px 0px 0px; border-right-width: 0px" src="http://winjade.net/wp-content/uploads/2008/08/class-thumb.png" border="0" alt="class" width="15" height="15" /></a><span style="font-family: Courier New;">GlassBarRenderer</span>.</p>
<p>This feature provides two key benefits to your application&#8217;s design:</p>
<ul>
<li>For Windows Vista systems and future OS with support for Glass: <em>rich, seamless Glass UI</em></li>
<li>For Windows Vista systems where DWM is disabled or unavailable, <strong>and</strong> all previous Windows releases: <em>seamless UI deriving from the appropriate Windows</em> <em>theme, including custom themes</em> <em>created with software such as </em><a href="http://www.windowblinds.net/"><em>WindowBlinds</em></a>.</li>
</ul>
<p align="center"><a href="http://winjade.net/wp-content/uploads/2008/08/glassbar-themes.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" src="http://winjade.net/wp-content/uploads/2008/08/glassbar-themes-thumb.png" border="0" alt="GlassBar &quot;dressed up&quot; in various Windows themes, including a custom GlassBarRenderer" width="700" height="267" /></a></p>
<h3>Managing the GlassBar items</h3>
<p>Adding your items to GlassBar is quite easy. While in Design Mode, make sure that the Properties pane is visible (<em>View </em>menu =&gt;<em> Properties Window </em>to invoke it), select <span style="font-family: courier new;">Items </span>and expand the Collection Editor. You can add, remove and rearrange the items. For each one of them you can customize their:</p>
<table style="width: auto;" border="0" cellspacing="2" cellpadding="2" width="1010">
<tbody>
<tr width="auto">
<td width="10" valign="top"></td>
<td width="188" valign="top"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 3px 0px 0px; border-right-width: 0px" src="http://winjade.net/wp-content/uploads/2008/08/property-thumb1.png" border="0" alt="property_thumb1" width="16" height="15" /><span style="font-family: courier new;">Name</span></td>
<td width="802" valign="top">Used in code only (does not appear in UI)</td>
</tr>
<tr>
<td width="10" valign="top"></td>
<td width="188" valign="top"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 3px 0px 0px; border-right-width: 0px" src="http://www.aeroxp.org/wp-content/uploads/2008/08/property-thumb11.png" border="0" alt="property_thumb1" width="16" height="15" /><span style="font-family: courier new;">Enabled</span></td>
<td width="794" valign="top">Enables/disables the item from the UI</td>
</tr>
<tr>
<td width="10" valign="top"></td>
<td width="188" valign="top"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 3px 0px 0px; border-right-width: 0px" src="http://winjade.net/wp-content/uploads/2008/08/property-thumb12.png" border="0" alt="property_thumb1" width="16" height="15" /><span style="font-family: courier new;">Icon</span></td>
<td width="787" valign="top">Preferably translucent PNG image 25&#215;25 pixels in size</td>
</tr>
<tr>
<td width="10" valign="top"></td>
<td width="188" valign="top"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 3px 0px 0px; border-right-width: 0px" src="http://winjade.net/wp-content/uploads/2008/08/property-thumb12.png" border="0" alt="property_thumb1" width="16" height="15" /><span style="font-family: courier new;">IsSeparator</span></td>
<td width="782" valign="top">Denotes whether the item is a separator (its Icon will be disregarded)</td>
</tr>
<tr>
<td width="10" valign="top"></td>
<td width="188" valign="top"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 3px 0px 0px; border-right-width: 0px" src="http://winjade.net/wp-content/uploads/2008/08/property-thumb12.png" border="0" alt="property_thumb1" width="16" height="15" /><span style="font-family: courier new;">Overlay</span></td>
<td width="777" valign="top">Image, useful for &#8216;hover&#8217; effects</td>
</tr>
<tr>
<td width="10" valign="top"></td>
<td width="188" valign="top"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 3px 0px 0px; border-right-width: 0px" src="http://winjade.net/wp-content/uploads/2008/08/property-thumb12.png" border="0" alt="property_thumb1" width="16" height="15" /><span style="font-family: courier new;">Text</span></td>
<td width="773" valign="top">Alternative text shown when the item is hovered</td>
</tr>
<tr>
<td width="10" valign="top"></td>
<td width="188" valign="top"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 3px 0px 0px; border-right-width: 0px" src="http://winjade.net/wp-content/uploads/2008/08/property-thumb12.png" border="0" alt="property_thumb1" width="16" height="15" /><span style="font-family: courier new;">Visible</span></td>
<td width="770" valign="top">Shows/hides the item</td>
</tr>
</tbody>
</table>
<p>You can of course manage items programmatically using the <img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 3px 0px 0px; border-right-width: 0px" src="http://winjade.net/wp-content/uploads/2008/08/property-thumb1.png" border="0" alt="property_thumb1" width="16" height="15" /><span style="font-family: courier new;">Items </span>property, for example adding one:</p>
<p><span><span style="font-family: Courier New;"><span style="color: #2b91af;">Image</span> img = <span style="color: #2b91af;">Image</span>.FromFile(<span style="color: #a31515;">&#8220;someFile.png&#8221;</span>);</span></span></p>
<p><span><span style="font-family: Courier New;"><span style="color: #2b91af;">GlassBarItem</span> gbi = <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">GlassBarItem</span>(<span style="color: #a31515;">&#8220;Sample Text&#8221;</span> + i, img);</span></span></p>
<p><span style="font-family: Courier New;">glassBar.Items.Add(gbi);</span></p>
<p>When the logic of your program requires an item be disabled or hidden, e.g. an unavailable feature, you can use <img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 3px 0px 0px; border-right-width: 0px" src="http://www.aeroxp.org/wp-content/uploads/2008/08/property-thumb11.png" border="0" alt="property_thumb1" width="16" height="15" /><span style="font-family: courier new;">Enabled </span>or <img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 3px 0px 0px; border-right-width: 0px" src="http://winjade.net/wp-content/uploads/2008/08/property-thumb12.png" border="0" alt="property_thumb1" width="16" height="15" /><span style="font-family: courier new;">Visible </span>to prevent users from clicking it.</p>
<h3>Custom renderers</h3>
<p>Internally GlassBar defines a standard renderer which on Windows Vista with Aero Glass enables draws on a translucent surface and otherwise falls back to the system-defined theme. If you wish to code a custom renderer for your needs, what you have to do is:</p>
<ol>
<li>Create a class deriving from (inheriting) from one of the following classes:
<ul>
<li><a href="http://winjade.net/wp-content/uploads/2008/08/class.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 3px 0px 0px; border-right-width: 0px" src="http://winjade.net/wp-content/uploads/2008/08/class-thumb.png" border="0" alt="class" width="15" height="15" /></a><span style="font-family: Courier New;">GlassLib.Rendering.DefaultGlassBarRenderer</span> if you wish to customize only specific parts of the default renderer (images, background etc)</li>
<li><a href="http://winjade.net/wp-content/uploads/2008/08/class.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 3px 0px 0px; border-right-width: 0px" src="http://winjade.net/wp-content/uploads/2008/08/class-thumb.png" border="0" alt="class" width="15" height="15" /></a><span style="font-family: Courier New;">GlassLib.Rendering.GlassBarRenderer</span> if you wish to build one from the ground up. This requires you to implement all of the methods and properties and is unlikely to be your choice.</li>
</ul>
</li>
<li>Decide which functions or images you would like to change. You can assign any image to <img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 3px 0px 0px; border-right-width: 0px" src="http://winjade.net/wp-content/uploads/2008/08/property-thumb12.png" border="0" alt="property_thumb1" width="16" height="15" /><span style="font-family: Courier New;">MainPiece, RightSecondaryStates, AnimationStrip etc.</span></li>
<li>The <img style="border-right: 0px; border-top: 0px; margin: 0px 3px 0px 0px; border-left: 0px; border-bottom: 0px" src="http://winjade.net/wp-content/uploads/2008/08/method-thumb1.png" border="0" alt="method_thumb1" width="16" height="15" /><span style="font-family: courier new;">DrawBarBackground </span>function is called every time the UI changes and needs to be redrawn. It is here where you can change most of the behavior of the default renderer and implement your own features.</li>
<li>Assign an instance of your custom renderer to <img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 3px 0px 0px; border-right-width: 0px" src="http://winjade.net/wp-content/uploads/2008/08/glassbar.png" border="0" alt="glassbar" width="16" height="16" /><span style="font-family: Courier New;">GlassBar</span>
<ul>
<li><span style="font-family: Courier New;">glassBar.Renderer = <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">CustomRenderer</span>();</span> or to revert to default:</li>
<li><span style="font-family: Courier New;">glassBar.Renderer = <span style="color: #0000ff;">new</span> GlassLib.Rendering.<span style="color: #2b91af;">DefaultGlassBarRenderer</span>();</span></li>
</ul>
</li>
</ol>
<h3>The test app</h3>
<p>There is a test program added to the package, <a href="file:///C:/Users/Stan/AppData/Roaming/Windows Live Writer/PostSupportingFiles/746266d3-e326-4c4e-8fb9-15492f646457/project3.png"><strong><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px; border-right-width: 0px" src="http://winjade.net/wp-content/uploads/2008/08/project-thumb12.png" border="0" alt="project_thumb1" width="16" height="15" /></strong></a><span style="font-family: courier new;">GlassLibTest</span>, whose code and compiled binary you can explore and experiment with GlassBar and DWM.</p>
<p>If you have any comments, questions, wishes or just an opinion, I would be happy to hear it.</p>
]]></content:encoded>
			<wfw:commentRss>http://winjade.net/2008/08/tweak-dwm-part-2/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Tweak DWM from your programs, Part 1</title>
		<link>http://winjade.net/2008/07/tweak-dwm-part-1/</link>
		<comments>http://winjade.net/2008/07/tweak-dwm-part-1/#comments</comments>
		<pubDate>Mon, 21 Jul 2008 16:13:39 +0000</pubDate>
		<dc:creator>Stan</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[dwm]]></category>
		<category><![CDATA[wrapper]]></category>

		<guid isPermaLink="false">http://www.aeroxp.org/?p=80</guid>
		<description><![CDATA[With Windows Vista, Microsoft introduced a whole new window manager which provides a better experience to the user and an independent graphics surface to us developers by taking advantage of graphics acceleration hardware (your graphics card or integrated GPU).
In a three-part blog post, I will show you how to use a wrapper around the Desktop [...]]]></description>
			<content:encoded><![CDATA[<p>With Windows Vista, Microsoft introduced a whole new window manager which provides a better experience to the user and an independent graphics surface to us developers by taking advantage of graphics acceleration hardware (your graphics card or integrated GPU).</p>
<p>In a three-part blog post, I will show you how to use a wrapper around the <strong>Desktop Window Manager</strong> Application Programming Interface in order to control its state, change colorization, or change the rendering policy of windows from your Windows Forms and .NET 3 Presentation Foundation programs.</p>
<p><span id="more-80"></span></p>
<h2>Getting Started</h2>
<p><em>Windows Forms and Windows Presentation Foundation will be referred to in these posts as WF and WPF respectively.</em></p>
<p>The code you will need can be <a href="http://stoyanoff.info/code/dwm/GlassLib_pub.zip" target="_blank">downloaded from here</a> and is written in C# (as are the code samples in this post). That being said, you can use it in projects of any other .NET language &#8212; you can include the compiled libraries as references. Otherwise (if you code in C#), you also have the option to include the files in your project and not distribute a separate library. The solution is for use in Visual Studio 2005 but is also compatible with version 2008.</p>
<p>The whole framework is located under the <strong><a href="http://winjade.net/wp-content/uploads/2008/07/namespace.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 3px 0px 0px; border-right-width: 0px" src="http://winjade.net/wp-content/uploads/2008/07/namespace-thumb.png" border="0" alt="namespace_" width="15" height="15" /></a></strong><span style="font-family: courier new;">GlassLib</span> namespace and is the source to three main types of projects:</p>
<ul>
<li><strong><a href="http://winjade.net/wp-content/uploads/2008/07/project.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 3px 0px 0px; border-right-width: 0px" src="http://winjade.net/wp-content/uploads/2008/07/project-thumb.png" border="0" alt="project" width="16" height="15" /></a></strong><span style="font-family: courier new;">DwmWrapper</span> is a .NET wrapper around the DWM API, which can be compiled in the following flavors:
<ul>
<li><strong>No dependency</strong> on WF or WPF. It can be used to avoid DLL clutter when coding command-line applications to manipulate the DWM.</li>
<li>Solely <strong>WF</strong> or <strong>WPF</strong></li>
<li><strong>Both</strong> for mixed-framework projects.</li>
</ul>
</li>
<li><a href="http://winjade.net/wp-content/uploads/2008/07/project.png"><strong><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 3px 0px 0px; border-right-width: 0px" src="http://winjade.net/wp-content/uploads/2008/07/project-thumb.png" border="0" alt="project" width="16" height="15" /></strong></a><span style="font-family: courier new;">GlassLib</span> is a WF-powered layer above <strong><a href="http://winjade.net/wp-content/uploads/2008/07/project.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 3px 0px 0px; border-right-width: 0px" src="http://winjade.net/wp-content/uploads/2008/07/project-thumb.png" border="0" alt="project" width="16" height="15" /></a></strong><span style="font-family: courier new;">DwmWrapper</span> and provides a set of classes, including the <a href="http://winjade.net/wp-content/uploads/2008/07/class.png"><strong><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 4px 0px 0px; border-right-width: 0px" src="http://winjade.net/wp-content/uploads/2008/07/class-thumb.png" border="0" alt="class" width="15" height="15" /></strong></a><span style="font-family: courier new;">GlassBar</span> control for use in your forms</li>
<li><strong><strong><a href="http://winjade.net/wp-content/uploads/2008/07/project.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 3px 0px 0px; border-right-width: 0px" src="http://winjade.net/wp-content/uploads/2008/07/project-thumb.png" border="0" alt="project" width="16" height="15" /></a></strong></strong>The <span style="font-family: courier new;">Test Projects</span> you can use to try the various features that the two other projects have. They will be used here ad hoc.</li>
</ul>
<p><img style="border-top-width: 0px; border-left-width: 0px; float: right; border-bottom-width: 0px; margin: 8px 4px 8px 8px; border-right-width: 0px" src="http://winjade.net/wp-content/uploads/2008/07/dwm-test-app1.png" border="0" alt="DWM test app" width="200" height="109" />In this first part, we will explore the architecture of the wrapper, its core functionality and application in Windows Forms programs.</p>
<p><a href="http://msdn.microsoft.com/en-us/library/aa969540.aspx" target="_blank">Microsoft provides an API for DWM</a> in the equivocal DwmApi library. Encapsulating it for easier use in .NET programs is the wrapper&#8217;s main purpose.</p>
<p>Since all necessary classes are located under the <a href="http://winjade.net/wp-content/uploads/2008/07/namespace.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 3px 0px 0px; border-right-width: 0px" src="http://winjade.net/wp-content/uploads/2008/07/namespace-thumb.png" border="0" alt="namespace_" width="15" height="15" /></a><span style="font-family: courier new;">GlassLib</span> namespace, in order to avoid writing it every time, I would suggest that you insert it in your C# or VB code file&#8217;s &#8220;imports&#8221; or, if you are working exclusively with VB, in your project&#8217;s &#8220;Imported Namespaces&#8221; list in order to avoid doing the first as well.</p>
<p>The most important class is <a href="http://winjade.net/wp-content/uploads/2008/07/class.png"><strong><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 3px 0px 0px; border-right-width: 0px" src="http://winjade.net/wp-content/uploads/2008/07/class-thumb.png" border="0" alt="class" width="15" height="15" /></strong></a><span style="font-family: courier new;">Dwm</span> and can be used to control virtually any aspect of DWM as long as the public API allows it. The various features are available via the following properties of <a href="http://winjade.net/wp-content/uploads/2008/07/class.png"><strong><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 3px 0px 0px; border-right-width: 0px" src="http://winjade.net/wp-content/uploads/2008/07/class-thumb.png" border="0" alt="class" width="15" height="15" /></strong></a><span style="font-family: courier new;">Dwm</span>:</p>
<table style="width: auto;" border="0" cellspacing="2" cellpadding="2" width="1019">
<tbody>
<tr width="auto">
<td width="10" valign="top"> </td>
<td width="188" valign="top"><a href="http://winjade.net/wp-content/uploads/2008/07/property.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 3px 0px 0px; border-right-width: 0px" src="http://winjade.net/wp-content/uploads/2008/07/property-thumb.png" border="0" alt="property" width="16" height="15" /></a><span style="font-family: courier new;">Blur</span></td>
<td width="802" valign="top">Enables/disables the black background transition when a window (not necessarily <em>your</em> window) is maximized, sets custom blur region</td>
</tr>
<tr>
<td width="10" valign="top"> </td>
<td width="188" valign="top"><a href="http://winjade.net/wp-content/uploads/2008/07/property.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 3px 0px 0px; border-right-width: 0px" src="http://winjade.net/wp-content/uploads/2008/07/property-thumb.png" border="0" alt="property" width="16" height="15" /></a><span style="font-family: courier new;">Colorization</span></td>
<td width="794" valign="top">Gets/sets the Aero Glass colorization</td>
</tr>
<tr>
<td width="10" valign="top"> </td>
<td width="188" valign="top"><a href="http://winjade.net/wp-content/uploads/2008/07/property.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 3px 0px 0px; border-right-width: 0px" src="http://winjade.net/wp-content/uploads/2008/07/property-thumb.png" border="0" alt="property" width="16" height="15" /></a><span style="font-family: courier new;">Composition</span></td>
<td width="787" valign="top">Enable/disable DWM, check if running and if the system is DWM-capable</td>
</tr>
<tr>
<td width="10" valign="top"> </td>
<td width="188" valign="top"><a href="http://winjade.net/wp-content/uploads/2008/07/property.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 3px 0px 0px; border-right-width: 0px" src="http://winjade.net/wp-content/uploads/2008/07/property-thumb.png" border="0" alt="property" width="16" height="15" /></a><span style="font-family: courier new;">Events</span></td>
<td width="782" valign="top">Used to set up notifications for colorization, composition, non-client rendering or window-maximized changes</td>
</tr>
<tr>
<td width="10" valign="top"> </td>
<td width="188" valign="top"><a href="http://winjade.net/wp-content/uploads/2008/07/property.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 3px 0px 0px; border-right-width: 0px" src="http://winjade.net/wp-content/uploads/2008/07/property-thumb.png" border="0" alt="property" width="16" height="15" /></a><span style="font-family: courier new;">Flip3D</span></td>
<td width="777" valign="top">Start/Stop Windows Flip3D or Flip</td>
</tr>
<tr>
<td width="10" valign="top"> </td>
<td width="188" valign="top"><a href="http://winjade.net/wp-content/uploads/2008/07/property.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 3px 0px 0px; border-right-width: 0px" src="http://winjade.net/wp-content/uploads/2008/07/property-thumb.png" border="0" alt="property" width="16" height="15" /></a><span style="font-family: courier new;">Glass</span></td>
<td width="773" valign="top">Enable/disable Glass on a given form (via its instance, e.g. <span style="color: #0000ff; font-family: courier new;">this</span>) or window (via its handle/hWnd, an <span style="font-family: courier new;"><a href="http://winjade.net/wp-content/uploads/2008/07/class.png"><strong><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 3px 0px 0px; border-right-width: 0px" src="http://winjade.net/wp-content/uploads/2008/07/class-thumb.png" border="0" alt="class" width="15" height="15" /></strong></a>IntPtr</span>)</td>
</tr>
<tr>
<td width="10" valign="top"> </td>
<td width="188" valign="top"><a href="http://winjade.net/wp-content/uploads/2008/07/property.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 3px 0px 0px; border-right-width: 0px" src="http://winjade.net/wp-content/uploads/2008/07/property-thumb.png" border="0" alt="property" width="16" height="15" /></a><span style="font-family: courier new;">NonClientArea</span></td>
<td width="770" valign="top">For advanced users only, gets or sets how DWM is handing the painting of a given form or window (see <a href="http://winjade.net/wp-content/uploads/2008/07/property.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 3px 0px 0px; border-right-width: 0px" src="http://winjade.net/wp-content/uploads/2008/07/property-thumb.png" border="0" alt="property" width="16" height="15" /></a><span style="font-family: courier new;">Glass</span> above)</td>
</tr>
</tbody>
</table>
<h2>Using the Wrapper</h2>
<p>Note that DWM is only available on Windows Vista, so I would suggest that you select how to handle the wrapper&#8217;s behavior on earlier operating systems. Otherwise, exceptions will be raised which you will have to catch.</p>
<p>The first option is to limit the exceptions the wrapper will raise by setting <a href="http://winjade.net/wp-content/uploads/2008/07/property.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 3px 0px 0px; border-right-width: 0px" src="http://winjade.net/wp-content/uploads/2008/07/property-thumb.png" border="0" alt="property" width="16" height="15" /></a><span style="font-family: courier new;"><span style="color: #2b91af;">Dwm</span>.ThrowExceptionTypes</span> to one of the available <a href="http://winjade.net/wp-content/uploads/2008/07/options.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 3px 0px 0px; border-right-width: 0px" src="http://winjade.net/wp-content/uploads/2008/07/options-thumb.png" border="0" alt="options" width="15" height="15" /></a><span style="font-family: courier new;">DwmExceptionTypes</span></p>
<table style="width: auto;" border="0" cellspacing="2" cellpadding="2" width="1019">
<tbody>
<tr width="auto">
<td width="10" valign="top"> </td>
<td width="995" valign="top"><a href="http://winjade.net/wp-content/uploads/2008/07/property.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 3px 0px 0px; border-right-width: 0px" src="http://winjade.net/wp-content/uploads/2008/07/property-thumb.png" border="0" alt="property" width="16" height="15" /></a><span style="font-family: courier new;">None</span></td>
</tr>
<tr>
<td width="10" valign="top"> </td>
<td width="995" valign="top"><a href="http://winjade.net/wp-content/uploads/2008/07/property.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 3px 0px 0px; border-right-width: 0px" src="http://winjade.net/wp-content/uploads/2008/07/property-thumb.png" border="0" alt="property" width="16" height="15" /></a><span style="font-family: courier new;">UnsupportedFeatures</span></td>
</tr>
<tr>
<td width="10" valign="top"> </td>
<td width="995" valign="top"><a href="http://winjade.net/wp-content/uploads/2008/07/property.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 3px 0px 0px; border-right-width: 0px" src="http://winjade.net/wp-content/uploads/2008/07/property-thumb.png" border="0" alt="property" width="16" height="15" /></a><span style="font-family: courier new;">DesktopWindowManager</span></td>
</tr>
</tbody>
</table>
<p>Raising no exception will most likely suit your needs best so adjust this property before you make any other changes to DWM, preferably at program startup or in the form&#8217;s constructor.</p>
<p><span style="font-family: courier new;"><span style="color: #2b91af;">Dwm</span>.ThrowExceptionTypes = <span style="color: #2b91af;">DwmExceptionTypes</span>.None;</span></p>
<p>Now you can make any changes you want to your form&#8217;s state in the DWM, for example enable Glass&#8211;it is as easy as writing</p>
<p><span style="font-family: courier new;"><span style="color: #2b91af;">Dwm</span>.Glass[<span style="color: #0000ff;">this</span>] = <span style="color: #0000ff;">new</span> <span style="color: #2b91af;">DwmMargins</span>(100, 4, 8, 20);</span></p>
<p>where <span style="color: #0000ff; font-family: courier new;">this</span> is the instance of the form (you can replace this with any other form instance you would like) and the margins are defined as left-right-top-bottom. You can use <a href="http://winjade.net/wp-content/uploads/2008/07/property.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 3px 0px 0px; border-right-width: 0px" src="http://winjade.net/wp-content/uploads/2008/07/property-thumb.png" border="0" alt="property" width="16" height="15" /></a><span style="font-family: courier new;"><span style="color: #2b91af;">DwmMargins</span>.EntireWindow</span> instead to extend glass in the whole window client area, like Mobility Center does in Windows Vista.</p>
<p>Restoring the default borders is done by calling the <a href="http://winjade.net/wp-content/uploads/2008/07/method.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 3px 0px 0px; border-right-width: 0px" src="http://winjade.net/wp-content/uploads/2008/07/method-thumb.png" border="0" alt="method" width="16" height="15" /></a><span style="font-family: courier new;">Restore</span> method.</p>
<p><span style="font-family: courier new;"><span style="color: #2b91af;">Dwm</span>.Glass[<span style="color: #0000ff;">this</span>].Restore();</span></p>
<p>Receiving notifications on the global DWM state or on your forms&#8217; is important in order to know how you should handle painting backgrounds or the non-client area. You can choose to get notified when the Aero Glass colorization is changed or DWM starts up or shuts down. In GlassLib this is straightforward: each form that you wish to be notification-aware, GlassLib will include in its list and you have to attach your handlers to the colorization, composition, non-client rendering or window-maximized changes.</p>
<p><span><span style="font-family: courier new;"><span style="color: #2b91af;"><img style="border-top-width: 0px; border-left-width: 0px; float: right; border-bottom-width: 0px; margin: 8px 4px 8px 8px; border-right-width: 0px" src="http://winjade.net/wp-content/uploads/2008/07/dwm-colorizations1.png" border="0" alt="DWM colorizations" width="200" height="140" /> Dwm</span>.Events[<span style="color: #0000ff;">this</span>].AddHandlers(); // Notifying GlassLib&#8230;</span></span></p>
<p><span style="font-family: courier new;">// Add our handlers</span></p>
<p><span><span style="font-family: courier new;"><span style="color: #2b91af;">Dwm</span>.Events[<span style="color: #0000ff;">this</span>].CompositionChanged += new <span style="color: #2b91af;">DwmEventHandler</span>(OnCompositionChanged);<br />
<span style="color: #2b91af;">Dwm</span>.Events[<span style="color: #0000ff;">this</span>].ColorizationChanged += new <span style="color: #2b91af;">DwmEventHandler</span>(OnColorizationChanged);</span></span></p>
<p><span style="font-family: courier new;">&#8230;</span></p>
<p><span><span style="font-family: courier new;"><span style="color: #0000ff;">private void </span>OnCompositionChanged(object sender, DwmEventArgs e)<br />
{<br />
// Update the UI on composition change (extend glass if DWM is enabled, otherwise restore)<br />
if (<span style="color: #2b91af;">Dwm</span>.Glass[<span style="color: #0000ff;">this</span>].Enabled)<br />
<span style="color: #2b91af;">Dwm</span>.Glass[<span style="color: #0000ff;">this</span>].Margins = new <span style="color: #2b91af;">DwmMargins</span>(100, 4, 8, 20);<br />
else<br />
<span style="color: #2b91af;">Dwm</span>.Glass[<span style="color: #0000ff;">this</span>].Restore();<br />
}</span></span></p>
<p><span><span style="font-family: courier new;"><span style="color: #0000ff;">private void</span> OnColorizationChanged(object sender, DwmEventArgs e)<br />
{<br />
<span style="color: #2b91af;">MessageBox</span>.Show(e.Colorization.ToString());<br />
}</span></span></p>
<p><span><span style="font-family: courier new;"><span style="color: #2b91af;"><img style="border-top-width: 0px; border-left-width: 0px; float: right; border-bottom-width: 0px; margin: 8px 4px 8px 8px; border-right-width: 0px" src="http://winjade.net/wp-content/uploads/2008/07/flip3d1.png" border="0" alt="Flip 3D" width="200" height="134" /></span></span></span>Although the public DWM API does not make it easy for one to change the colorization programmatically, DwmWrapper exposes this functionality through the <a href="http://winjade.net/wp-content/uploads/2008/07/property.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 3px 0px 0px; border-right-width: 0px" src="http://winjade.net/wp-content/uploads/2008/07/property-thumb.png" border="0" alt="property" width="16" height="15" /></a><span style="font-family: courier new;"><span style="color: #2b91af;">Dwm</span>.Colorization </span>property. Controlling the Desktop Window Manager is also as easy as setting <a href="http://winjade.net/wp-content/uploads/2008/07/property.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 3px 0px 0px; border-right-width: 0px" src="http://winjade.net/wp-content/uploads/2008/07/property-thumb.png" border="0" alt="property" width="16" height="15" /></a><span style="font-family: courier new;"><span style="color: #2b91af;">Dwm</span>.Composition.Enabled </span>to the desired value, either <span style="color: #0000ff; font-family: courier new;">true</span> or <span style="color: #0000ff; font-family: courier new;">false</span>. Other useful &#8220;one-liners&#8221; include invoking Windows Flip and Flip 3D: <a href="http://winjade.net/wp-content/uploads/2008/07/method.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 3px 0px 0px; border-right-width: 0px" src="http://winjade.net/wp-content/uploads/2008/07/method-thumb.png" border="0" alt="method" width="16" height="15" /></a><span style="font-family: courier new;">Dwm.Flip3D.EnterAltTab(); </span>and <a href="http://winjade.net/wp-content/uploads/2008/07/method.png"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 3px 0px 0px; border-right-width: 0px" src="http://winjade.net/wp-content/uploads/2008/07/method-thumb.png" border="0" alt="method" width="16" height="15" /></a><span style="font-family: courier new;">Dwm.Flip3D.Enter();</span>, respectively.</p>
<p>You can tinker with the <a href="http://stoyanoff.info/code/dwm/GlassLib_pub.zip" target="_blank">full GlassLib source code</a> and the included test projects to see how all of the aforementioned features work in a real environment. In the next part I am going to cover the more advanced functionality of the wrapper, the Windows Forms test app and how it manages DWM thumbnails.</p>
<p>If you have any comments, questions, wishes or just an opinion, feel free to post it here.</p>
<p><strong>Part 2 coming soon&#8230;</strong></p>
<p><strong><em>You are free to use GlassLib in your freeware programs, but please at least acknowledge it in your release notes/about dialog/website. In case your program is commercial/shareware, I would appreciate it if you could <a href="http://stoyanoff.info/contact/" target="_blank">contact</a> me beforehand. Thanks.</em></strong></p>
]]></content:encoded>
			<wfw:commentRss>http://winjade.net/2008/07/tweak-dwm-part-1/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Tutorial: How to work with the System Tray from .Net</title>
		<link>http://winjade.net/2008/06/tutorial-how-to-work-with-the-system-tray-from-net/</link>
		<comments>http://winjade.net/2008/06/tutorial-how-to-work-with-the-system-tray-from-net/#comments</comments>
		<pubDate>Mon, 02 Jun 2008 06:13:17 +0000</pubDate>
		<dc:creator>Karl</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Windows]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[System Tray]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://www.aeroxp.org/?p=45</guid>
		<description><![CDATA[After I released the System Tray tile for the Sidebar, I&#8217;ve had quite a lot of PMs and emails regarding interacting with the tray from .Net. So I&#8217;ve written this short tutorial for you all to go through to help with any difficulties you might have been having. It&#8217;s written with C# source code.
How to [...]]]></description>
			<content:encoded><![CDATA[<p>After I released the System Tray tile for the Sidebar, I&#8217;ve had quite a lot of PMs and emails regarding interacting with the tray from .Net. So I&#8217;ve written this short tutorial for you all to go through to help with any difficulties you might have been having. It&#8217;s written with C# source code.</p>
<p><a href="http://winjade.net/wp-content/uploads/2008/06/systray-tutorial.pdf">How to work with the System Tray from .Net</a> (PDF)</p>
]]></content:encoded>
			<wfw:commentRss>http://winjade.net/2008/06/tutorial-how-to-work-with-the-system-tray-from-net/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
	</channel>
</rss>
