系統在正常關機的時候會向所有的程序發兩個個關閉的消息, WM_QUERYENDSESSION 和WM_ENDSESSION具體的看一下微軟的解釋就可以了, WM_QUERYENDSESSIONThe WM_QUERYENDSESSION message is sent when the user chooses to end the session or when an application calls the ExitWindows function. If any application returns zero, the session is not ended. The system stops sending WM_QUERYENDSESSION messages as soon as one application returns zero.After processing this message, the system sends the WM_ENDSESSION message with the wParam parameter set to the results of the WM_QUERYENDSESSION message.所以在程序收到WM_QUERYENDSESSION消息時,反回非0就可以中止關機了,處理好了再用相關的api函數進行關機就行了,如exitwindows,在window2000中關機就有點難,網上有代碼,LRESULT CALLBACK WindowProc( HWND hwnd, // handle to window WM_QUERYENDSESSION, // the message to send WPARAM wParam, // not used LPARAM lParam // logoff option ); Parameters wParam This parameter is reserved for future use. lParam If this parameter includes ENDSESSION_LOGOFF, the user is logging off. (Note that this parameter is a bit mask. To test for this value, use a bit-wise operation; do not test for equality.) If this parameter is zero, the system is shutting down.Return Values If an application can terminate conveniently, it should return TRUE; otherwise, it should return FALSE.Res By default, the DefWindowProc function returns TRUE for this message. When an application returns TRUE for this message, it receives the WM_ENDSESSION message and it is terminated, regardless of how the other applications respond to the WM_QUERYENDSESSION message.
WM_QUERYENDSESSION 和WM_ENDSESSION具體的看一下微軟的解釋就可以了,
WM_QUERYENDSESSIONThe WM_QUERYENDSESSION message is sent when the user chooses to end the session or when an application calls the ExitWindows function. If any application returns zero, the session is not ended. The system stops sending WM_QUERYENDSESSION messages as soon as one application returns zero.After processing this message, the system sends the WM_ENDSESSION message with the wParam parameter set to the results of the WM_QUERYENDSESSION message.所以在程序收到WM_QUERYENDSESSION消息時,反回非0就可以中止關機了,處理好了再用相關的api函數進行關機就行了,如exitwindows,在window2000中關機就有點難,網上有代碼,LRESULT CALLBACK WindowProc(
HWND hwnd, // handle to window
WM_QUERYENDSESSION, // the message to send
WPARAM wParam, // not used
LPARAM lParam // logoff option
);
Parameters
wParam
This parameter is reserved for future use.
lParam
If this parameter includes ENDSESSION_LOGOFF, the user is logging off. (Note that this parameter is a bit mask. To test for this value, use a bit-wise operation; do not test for equality.)
If this parameter is zero, the system is shutting down.Return Values
If an application can terminate conveniently, it should return TRUE; otherwise, it should return FALSE.Res
By default, the DefWindowProc function returns TRUE for this message.
When an application returns TRUE for this message, it receives the WM_ENDSESSION message and it is terminated, regardless of how the other applications respond to the WM_QUERYENDSESSION message.