当我关闭控制台VB窗口时,我想做点什么可能吗?

时间:2016-05-02 05:38:41

标签: .net vb.net console

我想在close控制台vb中的窗口可以调用函数或执行一些代码吗?

提前致谢

1 个答案:

答案 0 :(得分:0)

试试这个,我还没有对它进行测试,我会把它留给你:)但是这应​​该会引导你朝着正确的方向前进:)

Module Module1
Public Enum ConsoleEvent
    CTRL_CLOSE_EVENT = 2
End Enum
Private Declare Function SetConsoleCtrlHandler Lib "kernel32" (ByVal handlerRoutine As ConsoleEventDelegate, ByVal add As Boolean) As Boolean
Public Delegate Function ConsoleEventDelegate(ByVal MyEvent As ConsoleEvent) As Boolean
Private handler As ConsoleEventDelegate

Sub Main()
    handler = AddressOf Application_ConsoleEvent
    If Not SetConsoleCtrlHandler(handler, True) Then
        Console.WriteLine("ERROR ADDING HANDLER")
    Else
        ''ADD YOUR MAIN CODE HERE.
        Console.WriteLine("Hello WORLD!")
        Console.ReadLine()

    End If
End Sub
Public Function Application_ConsoleEvent(ByVal [event] As ConsoleEvent) As Boolean
    Dim cancel As Boolean = False
    Select Case [event]
        Case ConsoleEvent.CTRL_CLOSE_EVENT
            'ADD YOUR CLOSING CODE WITHIN HERE, you dont need to have the Msgbox, its just there so you can understand :)
            MsgBox("Program being closed!")
            ''ADD your clean up code if needed here.
    End Select

    Return cancel ' handling the event.
End Function
End Module

让我知道你怎么走:)。

快乐的编码!