Override the OnFormClosing method.
CAUTION: You need to check the CloseReason and only alter the behaviour if it is UserClosing. You should not put anything in here that would stall the Windows shutdown routine.
Application Shutdown Changes in Windows Vista
This is from the Windows 7 logo program requirements.