Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 440
  • Last Modified:

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?
0
gprodri
Asked:
gprodri
  • 2
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
If you need that functionality then you should probably be using a WinForm instead...   =\

Can you give more details?
0
 
Daniel JungesCommented:
SetConsoleCtrlHandler(new HandlerRoutine(ConsoleCloseEvent), true);

private static bool ConsoleCloseEvent(CtrlTypes 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+BREAK received!");
                    break;

                case CtrlTypes.CTRL_CLOSE_EVENT:
                    isclosing = true;
                    Console.WriteLine("Program being closed!");
                    break;

                case CtrlTypes.CTRL_LOGOFF_EVENT:
                case CtrlTypes.CTRL_SHUTDOWN_EVENT:
                    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
        }

Open in new window

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
*Not for points...here's a VB implementation of junges's approach:
Imports System.Runtime.InteropServices
Module Module1

    Public isclosing As Boolean = False

    <DllImport("Kernel32")> _
    Public Function SetConsoleCtrlHandler(ByVal Handler As HandlerRoutine, ByVal Add As Boolean) As Boolean
    End Function

    Public Delegate Function HandlerRoutine(ByVal CtrlType As CtrlTypes) As Boolean

    Public Enum CtrlTypes
        CTRL_C_EVENT = 0
        CTRL_BREAK_EVENT
        CTRL_CLOSE_EVENT
        CTRL_LOGOFF_EVENT = 5
        CTRL_SHUTDOWN_EVENT
    End Enum

    Sub Main()
        SetConsoleCtrlHandler(New HandlerRoutine(AddressOf ConsoleCloseEvent), True)
        While Not isclosing

            ' ...

            System.Threading.Thread.Sleep(100)
        End While
    End Sub

    Private Function ConsoleCloseEvent(ByVal ctrlType As CtrlTypes) As Boolean
        Select Case ctrlType
            Case CtrlTypes.CTRL_C_EVENT
                Console.WriteLine("CTRL+C received!")

            Case CtrlTypes.CTRL_BREAK_EVENT
                Console.WriteLine("CTRL+BREAK received!")

            Case CtrlTypes.CTRL_CLOSE_EVENT
                Console.WriteLine("")
                Console.WriteLine("Program being closed!")
                Console.WriteLine("")
                Console.WriteLine("Press Enter to continue...")
                Console.ReadLine()

            Case CtrlTypes.CTRL_LOGOFF_EVENT, CtrlTypes.CTRL_SHUTDOWN_EVENT
                Console.WriteLine("User is logging off!")

        End Select

        isclosing = True
        Return True
    End Function

End Module

Open in new window

0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now