<?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>Jonathan C Dickinson &#187; Code 7 Contest</title>
	<atom:link href="http://jonathan.dickinsons.co.za/blog/category/programming/code-7-contest/feed/" rel="self" type="application/rss+xml" />
	<link>http://jonathan.dickinsons.co.za/blog</link>
	<description>&#34;Jonathan Chayce Dickinson&#34;.ToString()</description>
	<lastBuildDate>Sun, 06 Jun 2010 11:07:31 +0000</lastBuildDate>
	
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Remove System Menu Icon in WPF</title>
		<link>http://jonathan.dickinsons.co.za/blog/2009/07/remove-system-menu-icon-in-wpf/</link>
		<comments>http://jonathan.dickinsons.co.za/blog/2009/07/remove-system-menu-icon-in-wpf/#comments</comments>
		<pubDate>Thu, 16 Jul 2009 13:40:47 +0000</pubDate>
		<dc:creator>Jonathan Dickinson</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Code 7 Contest]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Top Hacks]]></category>

		<guid isPermaLink="false">http://jonathan.dickinsons.co.za/blog/2009/07/remove-system-menu-icon-in-wpf/</guid>
		<description><![CDATA[Howto remove the caption bar in WPF.]]></description>
			<content:encoded><![CDATA[<p>I am entering the <a href="http://code7contest.com" target="_blank">Code7 Contest</a> and part of my project is to mimic the network notification icon.</p>
<div id="attachment_61" class="wp-caption aligncenter" style="width: 312px"><a href="http://jonathan.dickinsons.co.za/blog/wp-content/uploads/2009/07/Windows7Network.png"><img class="size-full wp-image-61" title="Windows 7 Network Menu" src="http://jonathan.dickinsons.co.za/blog/wp-content/uploads/2009/07/Windows7Network.png" alt="Windows 7 Network Menu" width="302" height="454" /></a><p class="wp-caption-text">Windows 7 Network Menu</p></div>
<p>The trick to this is to set the caption of the window to null (not empty string) and remove the system menu – at least as far as Windows Forms goes. WPF doesn’t let you do this. I had a few options:</p>
<ul>
<li>Implement a template to mimic the real window template. Not too sure how glass would work here.</li>
<li>Use API calls to do it instead.</li>
</ul>
<p>The second option seemed to be more sensible. As it turns out Windows Forms is doing quite a bit of heavy lifting under the covers – you don’t set the title to null; you actually need to remove the caption (Windows Forms does this for you). In the end it’s as simple as clearing two style flags for the Window.</p>
<div class="code-header">Remove Caption in WPF</div>
<div id="scid:DFDE9937-D816-47f4-A306-7B60D5CE5AC0:63529eaa-0ca6-4208-853f-10b57d792814" class="wlWriterEditableSmartContent" style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px">
<pre class="brush: csharp; gutter: true; first-line: 1; tab-size: 4;  toolbar: true; ">private class NativeMethods
{
  [DllImport("user32.dll", EntryPoint = "SetWindowLong")]
  static extern IntPtr SetWindowLongPtr32(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
  [DllImport("user32.dll", EntryPoint = "SetWindowLongPtr")]
  static extern IntPtr SetWindowLongPtr64(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
  public static IntPtr SetWindowLong(IntPtr hWnd, int nIndex, IntPtr dwNewLong)
  {
      if (IntPtr.Size == 8)
          return SetWindowLongPtr64(hWnd, nIndex, dwNewLong);
      else
          return SetWindowLongPtr32(hWnd, nIndex, dwNewLong);
  }

  [DllImport("user32.dll", EntryPoint = "GetWindowLong")]
  private static extern IntPtr GetWindowLongPtr32(IntPtr hWnd, int nIndex);
  [DllImport("user32.dll", EntryPoint = "GetWindowLongPtr")]
  private static extern IntPtr GetWindowLongPtr64(IntPtr hWnd, int nIndex);
  public static IntPtr GetWindowLong(IntPtr hWnd, int nIndex)
  {
      if (IntPtr.Size == 8)
          return GetWindowLongPtr64(hWnd, nIndex);
      else
          return GetWindowLongPtr32(hWnd, nIndex);
  }

  const int GWL_STYLE = (-16);
  const int WS_SYSMENU = 0x00080000;
  const int WS_CAPTION = 0x00C00000;

  public static void RemoveMenu(IntPtr hwnd)
  {
      int currentStyle = GetWindowLong(hwnd, GWL_STYLE).ToInt32();
      currentStyle &amp;= ~WS_SYSMENU;
      currentStyle &amp;= ~WS_CAPTION;
      int ret = SetWindowLong(hwnd, GWL_STYLE, new IntPtr(currentStyle)).ToInt32();
  }

}</pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>You simply call that from OnSourceInitialized.</p>
<div class="code-header">How to use the method</div>
<div id="scid:DFDE9937-D816-47f4-A306-7B60D5CE5AC0:bac81a08-a2b0-4291-9490-ade63e6320ea" class="wlWriterEditableSmartContent" style="padding-bottom: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; float: none; padding-top: 0px">
<pre class="brush: csharp; gutter: true; first-line: 1; tab-size: 4;  toolbar: true; ">protected override void OnSourceInitialized(EventArgs e)
{
  base.OnSourceInitialized(e);
  WindowInteropHelper wih = new WindowInteropHelper(this);
  NativeMethods.RemoveMenu(wih.Handle);
}</pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<p>And you will get the following result:</p>
<div id="attachment_63" class="wp-caption aligncenter" style="width: 309px"><a href="http://jonathan.dickinsons.co.za/blog/wp-content/uploads/2009/07/Windows7Mimic.png"><img class="size-full wp-image-63" title="Windows 7 Mimic'ed Menu" src="http://jonathan.dickinsons.co.za/blog/wp-content/uploads/2009/07/Windows7Mimic.png" alt="Windows 7 Mimic'ed Menu" width="299" height="364" /></a><p class="wp-caption-text">Windows 7 Mimic&#39;ed Menu</p></div>
]]></content:encoded>
			<wfw:commentRss>http://jonathan.dickinsons.co.za/blog/2009/07/remove-system-menu-icon-in-wpf/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
