Well, I figured out that Task Manager is sending WM_CLOSE message. Here it is the complete working code in case someone else need the same:
In the MDI parent form:
public static CloseReason RealCloseReason = CloseReason.None;
public const int WM_SYSCOMMAND = 0x0112;
public const int WM_CLOSE = 0x0010;
public const int WM_QUERYENDSESSION = 0x0011;
public const int SC_CLOSE = 0xF060;
private const int WM_ENDSESSION = 0x0016;
private static bool UserClosed;
protected override void WndProc( ref Message m )
{
if( m.Msg == WM_QUERYENDSESSION || m.Msg == WM_ENDSESSION )
{
// Shutdown, Logoff, Restart
RealCloseReason = CloseReason.WindowsShutDown;
}
else if( m.Msg == WM_SYSCOMMAND )
{
if( ( m.WParam.ToInt32() & SC_CLOSE ) == SC_CLOSE )
{
// The user clicked on the "X" button of the MDI Parent
UserClosed = true;
}
}
else if( m.Msg == WM_CLOSE && UserClosed == false )
{
// Task Manager
RealCloseReason = CloseReason.TaskManagerClosing;
}
base.WndProc( ref m );
}
In the OnFormClosing of the MDI child form:
private void Form2_FormClosing( object sender, FormClosingEventArgs e )
{
if( Form1.RealCloseReason == CloseReason.TaskManagerClosing ||
Form1.RealCloseReason == CloseReason.WindowsShutDown ) return;
// Prompt for save
}
Thanks for the right direction.