catch the close button event on a windows form
September 18, 2009 Leave a Comment
In order to catch the close button event of the form we need to override the WndProc method.
Code:
public partial class MyForm : Form
{
public MyForm()
{
InitializeComponent();
}
protected override void WndProc(ref Message M)
{
const int WM_SYSCOMMAND = 0x112;
const int SC_CLOSE = 0xf060;
if (M.Msg == WM_SYSCOMMAND & M.WParam.ToInt32() == SC_CLOSE)
{
// User clicked the X - close button
// Any code handling goes here:
return;
}
base.WndProc(ref M);
}
}
Advertisement