In the process of installing or demonstrating software, if shutting down a Windows system can have serious consequences, you must prevent users from shutting down Windows until the appropriate action is completed, so how can you prevent users from shutting down Windows? In fact, when the user shuts down Windows, the system sends the application a message wm_queryendsession, tells the application to shut down, if the message returned by the value of 0, then Windows can not shutdown. Therefore, intercept wm_queryendsession, and feedback back to 0, the success of the success, the following is an example.
In the following example, we add a check box (Tcheckbox) to the form, and if the user does not select it, the Windows cannot be turned off, and if it is selected, it can be closed. In practical applications, the user can first set a Boolean variable, when the required operation is completed, so that the value changes, when the program detects the value of this variable to change before allowing windows to shut down. For the convenience of everyone, give the form a complete code (very short):
Add the following code to the private section in the. HPP:
Intercepts wm_queryendsession messages.
BEGIN_MESSAGE_MAP
MESSAGE_HANDLER(WM_QUERYENDSESSION,TMessage,OnWMQueryEndSession)
END_MESSAGE_MAP(TForm);
void __fastcall TForm1::OnWMQueryEndSession(TMessage &Message);
Add the following code in. cpp:
void __fastcall TForm1::OnWMQueryEndSession(TMessage &Message)
{
//如果CheckBox被选中,允许关机。
if(CheckBox1->Checked)
Message.Result=1;
else
Message.Result=0;
}
----Run the above program, first, do not select the check box, in the Windows Start menu, choose Shut down system--> shutdown computer or restart the computer, you will find that the screen flashes and return to normal; Select the check box and then turn off the computer, so you can do it.
----Program Debugging Environment: C + + Builder 6.0 + SP3
Windows ADV Server + SP3