728x90
private static int WM_QUERYENDSESSION = 0x11;  
private static bool systemShutdown = false;  

//여기 코드가 로그오프, 종료, 재부팅 이벤트
//로그오프, 종료, 재부팅 구분은 안되는거 같음 ㅠ
protected override void WndProc(ref System.Windows.Forms.Message m)  
{  
    if (m.Msg==WM_QUERYENDSESSION)  
    {  
        MessageBox.Show("queryendsession: this is a logoff, shutdown, or reboot");  
        systemShutdown = true;  
    }  

    // If this is WM_QUERYENDSESSION, the closing event should be  
    // raised in the base WndProc.  
    base.WndProc(ref m);  

} //WndProc   


//아래 코드는 구지 없어도 됨
private void Form1_Closing(  
    System.Object sender,   
    System.ComponentModel.CancelEventArgs e)  
{  
    if (systemShutdown)  
        // Reset the variable because the user might cancel the   
        // shutdown.  
    {  
        systemShutdown = false;  
        if (DialogResult.Yes==MessageBox.Show("My application",   
            "Do you want to save your work before logging off?",   
            MessageBoxButtons.YesNo))  
        {  
            e.Cancel = true;  
        }  
        else  
        {  
            e.Cancel = false;  
        }  
    }  
}

테스트할때 비쥬얼 스튜디오에서 하지말고 출력된 프로그램으로 테스트 하는걸 추천 합니다.

저는 비튜얼스튜디오 디버거 모드로 잘 안되네요.

 

출처: https://learn.microsoft.com/ko-kr/dotnet/api/microsoft.win32.systemevents.sessionending?view=dotnet-plat-ext-7.0

728x90

+ Recent posts