<?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:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Ken Kopczyk &#187; WinForms</title>
	<atom:link href="http://kenkopczyk.com/tag/winforms/feed/" rel="self" type="application/rss+xml" />
	<link>http://kenkopczyk.com</link>
	<description>Hurdles in .NET Development</description>
	<lastBuildDate>Sat, 17 Dec 2011 00:12:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='kenkopczyk.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://0.gravatar.com/blavatar/60348bde5d188332d5268692846b25c7?s=96&#038;d=http%3A%2F%2Fs2.wp.com%2Fi%2Fbuttonw-com.png</url>
		<title>Ken Kopczyk &#187; WinForms</title>
		<link>http://kenkopczyk.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://kenkopczyk.com/osd.xml" title="Ken Kopczyk" />
	<atom:link rel='hub' href='http://kenkopczyk.com/?pushpress=hub'/>
		<item>
		<title>Disabling and Enabling the Close Button in .NET 2.0 WinForms</title>
		<link>http://kenkopczyk.com/2010/01/23/disabling-and-enabling-the-close-button-in-net-2-0-winforms/</link>
		<comments>http://kenkopczyk.com/2010/01/23/disabling-and-enabling-the-close-button-in-net-2-0-winforms/#comments</comments>
		<pubDate>Sat, 23 Jan 2010 20:01:35 +0000</pubDate>
		<dc:creator>Ken</dc:creator>
				<category><![CDATA[WinForms Tips]]></category>
		<category><![CDATA[Close Button]]></category>
		<category><![CDATA[WinForms]]></category>

		<guid isPermaLink="false">http://kenkopczyk.com/?p=234</guid>
		<description><![CDATA[In .NET 2.0 WinForms, the user is given the following control over the minimize/maximize/close buttons (otherwise known as the ControlBox): Disable/enable the Minimize button via the Form.MinimizeBox property Disable/enable the Maximize button via the Form.MaximizeBox property Show/hide the entire ControlBox via the Form.ControlBox property However, you may want to allow maximizing and minimizing, but remove [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kenkopczyk.com&amp;blog=6344943&amp;post=234&amp;subd=kkopczyk&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>In .NET 2.0 WinForms, the user is given the following control over the minimize/maximize/close buttons (otherwise known as the ControlBox):</p>
<ul>
<li>Disable/enable the Minimize button via the Form.MinimizeBox property</li>
<li>Disable/enable the Maximize button via the Form.MaximizeBox property</li>
<li>Show/hide the entire ControlBox via the Form.ControlBox property</li>
</ul>
<p>However, you may want to allow maximizing and minimizing, but remove the user&#8217;s ability to close the application using the close button for any number of reasons.  For example:</p>
<ul>
<li>You want to run some process that cannot be cancelled midway through.</li>
<li>You want to ensure a user completes some number of tasks before being able to exit.</li>
</ul>
<p>Unfortunately, this functionality is not supported by default.  One must leverage the unmanaged code of the Windows API to achieve this behavior:</p>
<p><pre class="brush: csharp;">
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

public class SuperForm : Form
{ 
    private const int MF_BYPOSITION = 0x400;

    protected void DisableCloseButton()
    {
        IntPtr hMenu = GetSystemMenu(this.Handle, false);
        int menuItemCount = GetMenuItemCount(hMenu);
        RemoveMenu(hMenu, menuItemCount - 1, MF_BYPOSITION);
        DrawMenuBar((int)this.Handle);
    }
    protected void EnableCloseButton()
    {
        GetSystemMenu(this.Handle, true);
        DrawMenuBar((int)this.Handle);
    }

    // Win32 API declarations
    [DllImport(&quot;User32&quot;)]
    private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);
    [DllImport(&quot;User32&quot;)]
    private static extern int GetMenuItemCount(IntPtr hWnd);
    [DllImport(&quot;User32&quot;)]
    private static extern int RemoveMenu(IntPtr hMenu, int nPosition, int wFlags);
    [DllImport(&quot;User32&quot;)]
    private static extern IntPtr DrawMenuBar(int hwnd);
}
</pre></p>
<p>Notice that I incorporated this functionality into a base class that all your forms can inherit from to gain this ability.</p>
<br />  <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/kkopczyk.wordpress.com/234/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/kkopczyk.wordpress.com/234/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/kkopczyk.wordpress.com/234/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/kkopczyk.wordpress.com/234/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/kkopczyk.wordpress.com/234/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/kkopczyk.wordpress.com/234/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/kkopczyk.wordpress.com/234/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/kkopczyk.wordpress.com/234/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/kkopczyk.wordpress.com/234/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/kkopczyk.wordpress.com/234/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/kkopczyk.wordpress.com/234/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/kkopczyk.wordpress.com/234/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/kkopczyk.wordpress.com/234/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/kkopczyk.wordpress.com/234/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=kenkopczyk.com&amp;blog=6344943&amp;post=234&amp;subd=kkopczyk&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://kenkopczyk.com/2010/01/23/disabling-and-enabling-the-close-button-in-net-2-0-winforms/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/df43ecaeb5e162064d67f882431983c6?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96&#38;r=G" medium="image">
			<media:title type="html">Ken</media:title>
		</media:content>
	</item>
	</channel>
</rss>
