nikos_22

It's possible to prevent my exe from closing when i press ALT+F4

Thank You.



Re: .NET Base Class Library exe ignoring ALT+F4

bashoor

if exe is Windows Forms application you can use FormClosing event, and then in method write

e.Cancel = true;

if exe is Console application generate handler for CancelKeyPress event

Console.CancelKeyPress += new ConsoleCancelEventHandler(Console_CancelKeyPress);

And similiar code in this event

static void Console_CancelKeyPress(object sender, ConsoleCancelEventArgs e)

{

e.Cancel = true;

}





Re: .NET Base Class Library exe ignoring ALT+F4

nobugz

Paste this code anywhere in your form:

protected override void WndProc(ref Message m) {
Console.WriteLine(m.ToString());
// Disable WM_SYSCOMMAND, SC_CLOSE
if (m.Msg == 0x112 && m.WParam.ToInt32() == 0xf060) return;
base.WndProc(ref m);
}

To prevent closing with the system menu:

[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool revert);
[System.Runtime.InteropServices.DllImport("user32.dll")]
private static extern bool EnableMenuItem(IntPtr hMenu, int item, int enable);

protected override void CreateHandle() {
base.CreateHandle();
IntPtr hdl = GetSystemMenu(this.Handle, false);
EnableMenuItem(hdl, 0xF060, 3);
}