<?xml version="1.0" encoding="UTF-8"?>
<!-- generator="wordpress/2.0.1" -->
<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/"
	>

<channel>
	<title>Stanimir Stoyanov's Blog</title>
	<link>http://stoyanoff.info/blog</link>
	<description></description>
	<pubDate>Mon, 21 Jul 2008 21:35:50 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.0.1</generator>
	<language>en</language>
			<item>
		<title>GlassLib for .NET: The Wrapper</title>
		<link>http://stoyanoff.info/blog/code/glasslib/wrapper/</link>
		<comments>http://stoyanoff.info/blog/code/glasslib/wrapper/#comments</comments>
		<pubDate>Mon, 21 Jul 2008 21:16:51 +0000</pubDate>
		<dc:creator>Stanimir Stoyanov</dc:creator>
		
	<category>Windows Vista</category>
		<guid isPermaLink="false">http://stoyanoff.info/blog/code/glasslib/wrapper/</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).
GlassLib is a wrapper around the Desktop Window Manager Application Programming Interface and is created in order [...]]]></description>
			<content:encoded><![CDATA[<p><img style="float: right; margin: 8px 4px 8px 8px; border-width: 0px" border="0" alt="Flip 3D" src="http://www.aeroxp.org/wp-content/uploads/2008/07/flip3d1.png" width="200" height="134" />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><strong>GlassLib </strong>is a wrapper around the <strong>Desktop Window Manager</strong> Application Programming Interface and is created 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>
<h4>Getting Started</h4>
<p><em>Windows Forms and Windows Presentation Foundation will be referred to in this post as WF and WPF respectively.</em></p>
<p>You can download the full <strong>GlassLib</strong> source code and the included test projects from <a href="http://stoyanoff.info/code/dwm/GlassLib_pub.zip" target="_blank">here</a>. It 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://www.aeroxp.org/wp-content/uploads/2008/07/namespace.png"><img style="margin: 0px 3px 0px 0px; border-width: 0px" border="0" alt="namespace_" src="http://www.aeroxp.org/wp-content/uploads/2008/07/namespace-thumb.png" 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://www.aeroxp.org/wp-content/uploads/2008/07/project.png"><img style="margin: 0px 3px 0px 0px; border-width: 0px" border="0" alt="project" src="http://www.aeroxp.org/wp-content/uploads/2008/07/project-thumb.png" 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://www.aeroxp.org/wp-content/uploads/2008/07/project.png"><strong><img style="margin: 0px 3px 0px 0px; border-width: 0px" border="0" alt="project" src="http://www.aeroxp.org/wp-content/uploads/2008/07/project-thumb.png" width="16" height="15" /></strong></a><span style="font-family: Courier New">GlassLib</span> is a WF-powered layer above <strong><a href="http://www.aeroxp.org/wp-content/uploads/2008/07/project.png"><img style="margin: 0px 3px 0px 0px; border-width: 0px" border="0" alt="project" src="http://www.aeroxp.org/wp-content/uploads/2008/07/project-thumb.png" 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://www.aeroxp.org/wp-content/uploads/2008/07/class.png"><strong><img style="margin: 0px 4px 0px 0px; border-width: 0px" border="0" alt="class" src="http://www.aeroxp.org/wp-content/uploads/2008/07/class-thumb.png" 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://www.aeroxp.org/wp-content/uploads/2008/07/project.png"><img style="margin: 0px 3px 0px 0px; border-width: 0px" border="0" alt="project" src="http://www.aeroxp.org/wp-content/uploads/2008/07/project-thumb.png" 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="float: right; margin: 8px 4px 8px 8px; border-width: 0px" border="0" alt="DWM test app" src="http://www.aeroxp.org/wp-content/uploads/2008/07/dwm-test-app1.png" width="200" height="109" />Here 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://www.aeroxp.org/wp-content/uploads/2008/07/namespace.png"><img style="margin: 0px 3px 0px 0px; border-width: 0px" border="0" alt="namespace_" src="http://www.aeroxp.org/wp-content/uploads/2008/07/namespace-thumb.png" 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://www.aeroxp.org/wp-content/uploads/2008/07/class.png"><strong><img style="margin: 0px 3px 0px 0px; border-width: 0px" border="0" alt="class" src="http://www.aeroxp.org/wp-content/uploads/2008/07/class-thumb.png" 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://www.aeroxp.org/wp-content/uploads/2008/07/class.png"><strong><img style="margin: 0px 3px 0px 0px; border-width: 0px" border="0" alt="class" src="http://www.aeroxp.org/wp-content/uploads/2008/07/class-thumb.png" width="15" height="15" /></strong></a><span style="font-family: Courier New">Dwm</span>:</p>
<table border="0" cellspacing="2" cellpadding="2">
<tr>
<td style="width: 10px" valign="top"> </td>
<td style="width: 188px" valign="top"><a href="http://www.aeroxp.org/wp-content/uploads/2008/07/property.png"><img style="margin: 0px 3px 0px 0px; border-width: 0px" border="0" alt="property" src="http://www.aeroxp.org/wp-content/uploads/2008/07/property-thumb.png" width="16" height="15" /></a><span style="font-family: Courier New">Blur</span></td>
<td style="width: 802px" 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 style="width: 10px" valign="top"> </td>
<td style="width: 188px" valign="top"><a href="http://www.aeroxp.org/wp-content/uploads/2008/07/property.png"><img style="margin: 0px 3px 0px 0px; border-width: 0px" border="0" alt="property" src="http://www.aeroxp.org/wp-content/uploads/2008/07/property-thumb.png" width="16" height="15" /></a><span style="font-family: Courier New">Colorization</span></td>
<td style="width: 794px" valign="top">Gets/sets the Aero Glass colorization</td>
</tr>
<tr>
<td style="width: 10px" valign="top"> </td>
<td style="width: 188px" valign="top"><a href="http://www.aeroxp.org/wp-content/uploads/2008/07/property.png"><img style="margin: 0px 3px 0px 0px; border-width: 0px" border="0" alt="property" src="http://www.aeroxp.org/wp-content/uploads/2008/07/property-thumb.png" width="16" height="15" /></a><span style="font-family: Courier New">Composition</span></td>
<td style="width: 787px" valign="top">Enable/disable DWM, check if running and if the system is DWM-capable</td>
</tr>
<tr>
<td style="width: 10px" valign="top"> </td>
<td style="width: 188px" valign="top"><a href="http://www.aeroxp.org/wp-content/uploads/2008/07/property.png"><img style="margin: 0px 3px 0px 0px; border-width: 0px" border="0" alt="property" src="http://www.aeroxp.org/wp-content/uploads/2008/07/property-thumb.png" width="16" height="15" /></a><span style="font-family: Courier New">Events</span></td>
<td style="width: 782px" valign="top">Used to set up notifications for colorization, composition, non-client rendering or window-maximized changes</td>
</tr>
<tr>
<td style="width: 10px" valign="top"> </td>
<td style="width: 188px" valign="top"><a href="http://www.aeroxp.org/wp-content/uploads/2008/07/property.png"><img style="margin: 0px 3px 0px 0px; border-width: 0px" border="0" alt="property" src="http://www.aeroxp.org/wp-content/uploads/2008/07/property-thumb.png" width="16" height="15" /></a><span style="font-family: Courier New">Flip3D</span></td>
<td style="width: 777px" valign="top">Start/Stop Windows Flip3D or Flip</td>
</tr>
<tr>
<td style="width: 10px" valign="top"> </td>
<td style="width: 188px" valign="top"><a href="http://www.aeroxp.org/wp-content/uploads/2008/07/property.png"><img style="margin: 0px 3px 0px 0px; border-width: 0px" border="0" alt="property" src="http://www.aeroxp.org/wp-content/uploads/2008/07/property-thumb.png" width="16" height="15" /></a><span style="font-family: Courier New">Glass</span></td>
<td style="width: 773px" 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://www.aeroxp.org/wp-content/uploads/2008/07/class.png"><strong><img style="margin: 0px 3px 0px 0px; border-width: 0px" border="0" alt="class" src="http://www.aeroxp.org/wp-content/uploads/2008/07/class-thumb.png" width="15" height="15" /></strong></a>IntPtr</span>)</td>
</tr>
<tr>
<td style="width: 10px" valign="top"> </td>
<td style="width: 188px" valign="top"><a href="http://www.aeroxp.org/wp-content/uploads/2008/07/property.png"><img style="margin: 0px 3px 0px 0px; border-width: 0px" border="0" alt="property" src="http://www.aeroxp.org/wp-content/uploads/2008/07/property-thumb.png" width="16" height="15" /></a><span style="font-family: Courier New">NonClientArea</span></td>
<td style="width: 770px" 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://www.aeroxp.org/wp-content/uploads/2008/07/property.png"><img style="margin: 0px 3px 0px 0px; border-width: 0px" border="0" alt="property" src="http://www.aeroxp.org/wp-content/uploads/2008/07/property-thumb.png" width="16" height="15" /></a><span style="font-family: Courier New">Glass</span> above)</td>
</tr>
</table>
<h4>Using the Wrapper</h4>
<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://www.aeroxp.org/wp-content/uploads/2008/07/property.png"><img style="margin: 0px 3px 0px 0px; border-width: 0px" border="0" alt="property" src="http://www.aeroxp.org/wp-content/uploads/2008/07/property-thumb.png" 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://www.aeroxp.org/wp-content/uploads/2008/07/options.png"><img style="margin: 0px 3px 0px 0px; border-width: 0px" border="0" alt="options" src="http://www.aeroxp.org/wp-content/uploads/2008/07/options-thumb.png" width="15" height="15" /></a><span style="font-family: Courier New">DwmExceptionTypes</span></p>
<table border="0" cellspacing="2" cellpadding="2">
<tr>
<td style="width: 10px" valign="top"> </td>
<td style="width: 995px" valign="top"><a href="http://www.aeroxp.org/wp-content/uploads/2008/07/property.png"><img style="margin: 0px 3px 0px 0px; border-width: 0px" border="0" alt="property" src="http://www.aeroxp.org/wp-content/uploads/2008/07/property-thumb.png" width="16" height="15" /></a><span style="font-family: Courier New">None</span></td>
</tr>
<tr>
<td style="width: 10px" valign="top"> </td>
<td style="width: 995px" valign="top"><a href="http://www.aeroxp.org/wp-content/uploads/2008/07/property.png"><img style="margin: 0px 3px 0px 0px; border-width: 0px" border="0" alt="property" src="http://www.aeroxp.org/wp-content/uploads/2008/07/property-thumb.png" width="16" height="15" /></a><span style="font-family: Courier New">UnsupportedFeatures</span></td>
</tr>
<tr>
<td style="width: 10px" valign="top"> </td>
<td style="width: 995px" valign="top"><a href="http://www.aeroxp.org/wp-content/uploads/2008/07/property.png"><img style="margin: 0px 3px 0px 0px; border-width: 0px" border="0" alt="property" src="http://www.aeroxp.org/wp-content/uploads/2008/07/property-thumb.png" width="16" height="15" /></a><span style="font-family: Courier New">DesktopWindowManager</span></td>
</tr>
</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://www.aeroxp.org/wp-content/uploads/2008/07/property.png"><img style="margin: 0px 3px 0px 0px; border-width: 0px" border="0" alt="property" src="http://www.aeroxp.org/wp-content/uploads/2008/07/property-thumb.png" 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://www.aeroxp.org/wp-content/uploads/2008/07/method.png"><img style="margin: 0px 3px 0px 0px; border-width: 0px" border="0" alt="method" src="http://www.aeroxp.org/wp-content/uploads/2008/07/method-thumb.png" 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><span style="font-family: Courier New"><span style="color: #2b91af"><img style="float: right; margin: 8px 4px 8px 8px; border-width: 0px" border="0" alt="DWM colorizations" src="http://www.aeroxp.org/wp-content/uploads/2008/07/dwm-colorizations1.png" width="200" height="140" /></span></span>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 style="font-family: Courier New"><span style="color: #2b91af">Dwm</span>.Events[<span style="color: #0000ff">this</span>].AddHandlers(); // Notifying GlassLib&#8230;</span></p>
<p><span style="font-family: Courier New">// Add our handlers</span></p>
<p><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></p>
<p><span style="font-family: Courier New">&#8230;</span></p>
<p><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></p>
<p><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></p>
<p><span style="font-family: Courier New"><span style="color: #2b91af"><img style="float: right; margin: 8px 4px 8px 8px; border-width: 0px" border="0" alt="Flip 3D" src="http://www.aeroxp.org/wp-content/uploads/2008/07/flip3d1.png" width="200" height="134" /></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://www.aeroxp.org/wp-content/uploads/2008/07/property.png"><img style="margin: 0px 3px 0px 0px; border-width: 0px" border="0" alt="property" src="http://www.aeroxp.org/wp-content/uploads/2008/07/property-thumb.png" 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://www.aeroxp.org/wp-content/uploads/2008/07/property.png"><img style="margin: 0px 3px 0px 0px; border-width: 0px" border="0" alt="property" src="http://www.aeroxp.org/wp-content/uploads/2008/07/property-thumb.png" 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://www.aeroxp.org/wp-content/uploads/2008/07/method.png"><img style="margin: 0px 3px 0px 0px; border-width: 0px" border="0" alt="method" src="http://www.aeroxp.org/wp-content/uploads/2008/07/method-thumb.png" width="16" height="15" /></a><span style="font-family: Courier New">Dwm.Flip3D.EnterAltTab(); </span>and <a href="http://www.aeroxp.org/wp-content/uploads/2008/07/method.png"><img style="margin: 0px 3px 0px 0px; border-width: 0px" border="0" alt="method" src="http://www.aeroxp.org/wp-content/uploads/2008/07/method-thumb.png" width="16" height="15" /></a><span style="font-family: Courier New">Dwm.Flip3D.Enter();</span>, respectively.</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://stoyanoff.info/blog/code/glasslib/wrapper/feed/</wfw:commentRSS>
		</item>
	</channel>
</rss>
