Many software have the automatic shutdown function, especially in the long time downloading, this function but lets you not wait in front of the computer, but the computer can according to you beforehand set up automatically closes. Now we use Visual C # to write a versatile shutdown program. The program has: Timing shutdown, countdown shutdown, shutdown reminder, System Information acquisition, such as four functions, can set the shutdown time accurate to the second. And let you quickly grasp the operating procedures for APIs in Visual C #.
A. Design close Windows Forms
1. The design of the interface
Create a new standard project, add a Windows Form to the project, and include the following controls on the form, and set their properties individually:
Control Name |
Category |
Text |
Control Name |
Category |
Text |
CheckBox1 |
CheckBox |
Automatic shutdown |
GroupBox1 |
GroupBox |
Current system time |
CheckBox1 |
CheckBox |
Countdown Execution Action |
GroupBox2 |
GroupBox |
Set time |
CheckBox1 |
CheckBox |
Timed Alarm |
Txttime |
Textbox |
|
Butcancle |
Button |
Cancel |
Setuptime |
DateTimePicker |
|
Butreopen |
Button |
Reboot |
Setupdate |
DateTimePicker |
|
Butclose |
Button |
Shutdown |
Timer1 |
Timer |
100 |
Butsysinto |
Button |
System Information |
Butrelogin |
Button |
Injection and elimination |
Windows Forms Interface:
Set the caption in the form properties to "Turn off Windows" and the name is set to "Frmmain".
2. Referencing API functions in form classes
API functions are the cornerstone of building Windows applications and are a necessary tool for Windows programming. Each of the Windows Application development tools provides a way to indirectly or directly invoke Windows API functions, or interfaces that invoke Windows API functions, which means the ability to invoke dynamic connection libraries. Visual C #, like other development tools, can also invoke the API functions of a dynamic-link library.
The basic process of invoking the API in Visual C #:
First, before invoking the API, you must first import the System.Runtime.InteropServices namespace. This namespace contains some of the necessary collections of calls to the API in Visual C #, as follows:
using System.Runtime.InteropServices ;
using System.Text ;
After importing the namespaces, we want to declare the API functions to be used in the program. Our program is mainly to obtain information about the system, so the use of API functions are returned to the system information. First, give the method of declaring the API in Visual C #:
[ DllImport("user32") ]
public static extern long SetWindowPos(long hwnd , long hWndInsertAfter, long X , long y , long cx, long cy, long wFlagslong) ;
Where the "DllImport" property is used to call a method from within the controllable code that specifies the location of the DLL that contains the external method of the call; "KERNEL32" Sets the class library name; "Public" indicates that the access type of the function is common; "Static" modifier declares a static element that belongs to the type itself rather than the specified object; "extern" means that the method will be executed outside the project, and the method imported using DllImport must use the "extern" modifier The last GetWindowsDirectory function contains two arguments, one is StringBuilder type and the other is of type int, and the method returns content that exists in the parameters of the StringBuilder type. At the same time, because we use the StringBuilder class here, at the beginning of the program, we have to add the System.Text namespace, the same method as above.
Declare other API functions that you want to use in your program:
[ DllImport("user32") ]
public static extern long ExitWindowsEx(long uFlags, long dwReserved ) ;
[ DllImport("shell32") ]
public static extern long ShellAbout(long uFlags, long dwReserved ) ;
3. Increase the variables of the form class
long dwReserved ;
const int SHUTDOWN = 1 ;
const int REBOOT = 2 ;
const int LOGOFF = 0 ;
long sh ;
int counter , n ;
4. Ways to write form classes
Write the following code in the form's Load (event procedure:
private void Frmmain1_load (object sender, System.EventArgs e)
{
file://Initialize component with system time
Time.Text = Syste M.datetime.today.toshortdatestring () + "" + System.DateTime.Today.ToLongTimeString ();
}
writes the following code in the OnTimer event procedure for component Timer1:
//write the following code in the OnTimer event procedure of the component Timer1:
private void Timer1_timer (object sen Der, System.EventArgs e)
{
file://receive the current date and time to display the
string currdate= immediately System.DateTime.Today.ToShortDateString ();
String currtime=system.datetime.today.toshorttimestring ();
file://to detect whether a set shutdown date and time is valid at any time
if (this. checkbox1.checked = = True)
{
if (currdate== setupdate.tostring () && currtime==setuptime.tostring ())
Colsecomputer ();
}
}
private void Colsecomputer ()
{sh = exitwindowsex (SHUTDOWN, dwreserved);}
private void Button1_Click (object sender, System.EventArgs e)
{
Form2 frm=new Form2 ();
frm. Show ();
}
private void Butreopen_click (object sender, System.EventArgs e)
{sh = exitwindowsex (REBOOT, dwreserved);}
private void Butrelogin_click (object sender, System.EventArgs e)
{sh = exitwindowsex (LOGOFF, dwreserved);
private void Butcancle_click (object sender, System.EventArgs e)
{this. Close ();
private void Butclose_click_1 (object sender, System.EventArgs e)
{sh = exitwindowsex (REBOOT, dwreserved);