当用户关闭操作系统时,操作系统会发布一个WM_QueryEndSession消息。
程序可以通过设置消息的返回值为0,来禁止用户关闭操作系统,反之为1,就会允许用户关闭操作系统,默认值为1
由于程序中要使用到消息,
所以,USES项中要把Messages 加入。
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs; type TForm1 = class(TForm) private { Private declarations } procedure QueryEndSession(var Msg:TMessage); Message WM_QueryEndSession; public { Public declarations } end; var Form1: TForm1; implementation procedure TForm1.QueryEndSession(var Msg:TMessage); begin Msg.Result:=0; end; end.
查找WINDOWS SDK
可以加深理解
Calls the ExitWindowsEx function to log off the interactive user. Applications should call ExitWindowsEx directly.
BOOL WINAPI ExitWindows( DWORD dwReserved, UINT uReserved );
If the call succeeds, the return value is nonzero.
If the call fails, the return value is zero. To get extended error information, call GetLastError.
The system sends a WM_QUERYENDSESSION to the main window of each running application.
Windows Me/98/95: The system sends a WM_QUERYENDSESSION to all applications except calling application.
An application agrees to terminate by returning TRUE when it receives this message (or by allowing the DefWindowProc function to process the message). If any application returns FALSE when it receives the WM_QUERYENDSESSION message, the logoff is canceled.
After the system processes the results of the WM_QUERYENDSESSION message, it sends the WM_ENDSESSION message with the wParam parameter set to TRUE if the system is shutting down and to FALSE if it is not.
and a example:
The following example uses the ExitWindows function to log off the current user.
// Log off the current user. ExitWindows(0, 0);
The application receives the WM_QUERYENDSESSION message and displays a dialog box asking the whether it is OK to end the session. If the user clicks Yes, the system logs off the user. If the user clicks No, the logoff is canceled.
// Process the message in the window procedure. case WM_QUERYENDSESSION: { int r; r = MessageBox(NULL, "End the session?", "WM_QUERYENDSESSION", MB_YESNO); // Return TRUE to continue, FALSE to stop. return r == IDYES; break; }
The WM_QUERYENDSESSION message is sent when the user chooses to end the session or when an application calls one of the system shutdown functions. 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.
A window receives this message through its WindowProc function.
LRESULT CALLBACK WindowProc( HWND hwnd, // handle to window UINT uMsg, // message identifier WPARAM wParam, // not used LPARAM lParam // logoff option );
Value | Meaning |
---|---|
ENDSESSION_CLOSEAPP 0x1 |
The application is using a file that must be replaced, the system is being serviced, or system resources are exhausted. For more information, see Guidelines for Applications. |
ENDSESSION_LOGOFF 0x80000000 |
The user is logging off. For more information, see 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 or restarting (it is not possible to determine which event is occurring).
参考资料:
http://hi.baidu.com/endlesslove137/blog/item/d9288c661e08c52cab184c99.html