Write a timer shutdown program with C + + Builder

Source: Internet
Author: User
Tags datetime

When downloading larger software from the Internet, many people like to do it at night due to the current network bandwidth, and there is a need to rest at night. At present, the vast majority of download software can estimate the time required to download, if the computer automatically shut down, you can avoid waiting for the pain. This paper introduces a program which can realize the timing automatic shutdown by using C + + Builder 5.

Program features

Figure 1 The top half of the interface is used to instantly display the date and time information for the current system, and the lower part is used to set the date and time of the automatic shutdown. The computer shuts down automatically when the system time and set shutdown time coincide (equal) and the "automatic shutdown takes effect" is selected. In which, only when the shutdown time is set in a valid range (that is, greater than the system time), "Automatic shutdown effective" can be activated.

Component settings

Place two Tgroupbox components on the form: GroupBox1 and GroupBox2, whose caption properties are set to the current system time and preset shutdown time, respectively. Place 4 more Tdatetimepicker components on these two components: dateTimePicker1, Datetimepicker2 and Datetimepicker3, Datetimepicker4, The first two are used to display the date and time of the current system, the last two to set the date and time for automatic shutdown, and their partial property values are set as shown in the following table:

Place a Tcheckbox component on the form: CheckBox1 (its Caption property is set to automatic shutdown effective) and a TTimer component: Timer1 (its Interval property takes the default value of 1000).

Code writing

Write the following code in the OnCreate event procedure for the form:

void __fastcall tform1::formcreate(tobject *sender)
{
checkbox1->checked=false;
//初始设置为不可访问
checkbox1->enabled=false;
datetimepicker4->datetime=now();
datetimepicker3->datetime=now();
datetimepicker2->datetime=now();
//用系统时间初始化组件
datetimepicker1->datetime=now();
}

Write the following code in the OnTimer event procedure for the component timer1:

void __fastcall tform1::timer1timer(tobject *sender)
{
datetimepicker2->datetime=now();
//接收当前日期和时间,用于即时显示
datetimepicker1->datetime=now();
//随时检测设定的关机日期和时间是否有效
if(datetimepicker4->datetime < now())
{
checkbox1->checked=false;
//无效时不可访问
checkbox1->enabled=false;
}
else
checkbox1->enabled=true; //有效时可以访问
if(checkbox1->checked == true && datetimetostr(datetimepicker4->datetime) == datetimetostr(now()))
{//判断定时关机条件是否满足
dword tmp;
//调用win api函数关闭计算机
exitwindowsex(ewx_shutdown,tmp);
}
}

The component Datetimepicker3 is similar to the code for the Datetimepicker4 onchange event procedure to "link" (change) the date and time set by the two components. The onchange code for the component Datetimepicker3 is set as follows:

void __fastcall tform1::datetimepicker3change(tobject *sender)
{//组件变化“联动”
datetimepicker4->datetime=datetimepicker3->datetime;
//检测设置的日期是否有效
if(datetimepicker4->datetime < now())
{
checkbox1->checked=false;
checkbox1->enabled=false;
}
else
checkbox1->enabled=true;
}

The onchange code for the component Datetimepicker4 is set as follows:

void __fastcall tform1::datetimepicker4change(tobject *sender)
{//组件变化“联动”
datetimepicker3->datetime=datetimepicker4->datetime;
//检测设置的时间是否有效
if(datetimepicker3->datetime < now())
{
checkbox1->checked=false;
checkbox1->enabled=false;
}
else
checkbox1->enabled=true;
}

Also, if you add the following code to the OnTimer event procedure for the Timer1 component:

Define date-time objects (variables)

Tdatetime Presetdatetime,currentdatetime;

presetdatetime=datetimepicker3->datetime;

Currentdatetime=now ();

Get time difference (number of seconds)

Edit1->text=int ((presetdatetime.val-currentdatetime.val) *60*60*24);

You can achieve a countdown by edit1 the number of seconds left on the Tedit component that is placed on the form to instantly display distance.

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.