gprodri
asked on
Detect X button click event on cmd window
How do I detect from within a vb.net console application when the X button on its cmd window has been clicked?
SetConsoleCtrlHandler(new HandlerRoutine(ConsoleClos eEvent), true);
private static bool ConsoleCloseEvent(CtrlType s ctrlType)
{
switch (ctrlType)
{
case CtrlTypes.CTRL_C_EVENT:
isclosing = true;
Console.WriteLine("CTRL+C received!");
break;
case CtrlTypes.CTRL_BREAK_EVENT :
isclosing = true;
Console.WriteLine("CTRL+BR EAK received!");
break;
case CtrlTypes.CTRL_CLOSE_EVENT :
isclosing = true;
Console.WriteLine("Program being closed!");
break;
case CtrlTypes.CTRL_LOGOFF_EVEN T:
case CtrlTypes.CTRL_SHUTDOWN_EV ENT:
isclosing = true;
Console.WriteLine("User is logging off!");
break;
}
return true;
}
private static bool ConsoleCloseEvent(CtrlType
{
switch (ctrlType)
{
case CtrlTypes.CTRL_C_EVENT:
isclosing = true;
Console.WriteLine("CTRL+C received!");
break;
case CtrlTypes.CTRL_BREAK_EVENT
isclosing = true;
Console.WriteLine("CTRL+BR
break;
case CtrlTypes.CTRL_CLOSE_EVENT
isclosing = true;
Console.WriteLine("Program
break;
case CtrlTypes.CTRL_LOGOFF_EVEN
case CtrlTypes.CTRL_SHUTDOWN_EV
isclosing = true;
Console.WriteLine("User is logging off!");
break;
}
return true;
}
[DllImport("Kernel32")]
public static extern bool SetConsoleCtrlHandler(HandlerRoutine Handler, bool Add);
public delegate bool HandlerRoutine(CtrlTypes CtrlType);
public enum CtrlTypes
{
CTRL_C_EVENT = 0,
CTRL_BREAK_EVENT,
CTRL_CLOSE_EVENT,
CTRL_LOGOFF_EVENT = 5,
CTRL_SHUTDOWN_EVENT
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Can you give more details?