Using BCB to prevent users from shutting down window systems

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.