Many software have automatic shutdown features, especially when downloading, this feature allows you to wait for your computers, while your computer can automatically turn off according to your prior. Now we use Visual C # to write a multi-function shutdown program. The program has four functions such as timing shutdown, countdown shutdown, shutdown reminder, system information acquisition, and set off time to second. And let you quickly master the operation procedures for the API in Visual C #.
I. Design to close the Windows Form
Interface design
Create a new standard engineering, add a Windows Form to the project and add the following control to the form, and set its properties separately:
Control Name Category Text Control Name Category TEXT
Checkbox1 Checkbox Auto Shutdown GroupBox1 GroupBox Current System Time
CheckBox1 checkbox countdown execution operation Groupbox2 groupbox setting time
CheckBox1 Checkbox Timing Alarm TXTTIME TEXTBOX
Butcancle Button Cancel SetUptime DateTimePicker
Butreopen Button restarts setUpdate datetimepicker
Butclose Button shutdown Timer1 Timer 100
ButSysInto Button System Information Butrelogin Button
Windows Forms Interface:
Set the CAPTION in the form property to "Close Windows" and the name is set to "frmmain".
2. Reference API functions in the form class
The API function is the cornerstone of the Windows application, which is a must-have tool for Windows programming. Each Windows application development tool provides indirect or directly calling a method of calling the Windows API function, or calling the interface of the Windows API function, that is, the ability to call the dynamic connection library. Visual C # and other development tools can also call the dynamic link library API function.
The basic process of calling the API in Visual C #:
First, before calling the API, you must first import the namespace of System.Runtime.InteropServices. The namespace contain some of the necessary collection of the API in Visual C #, the specific methods are as follows:
Using system.Runtime.InteropServices;
Using system.text;
After importing the namespace, we must declare the API function to be used in the program. Our program is mainly information about the system, so the API function used is returned to system information. First give a 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 cyog wflagslong);
Where "DLLIMPORT" attribute is used to call a method from the uncontrollable code, it specifies the location of the DLL, which contains the external method called; "kernel32" sets the class library name; "public" specifies the function of the function The type is public; "Static" modifier declares a static element, which is the type itself instead of the specified object; "extern" indicates that the method will execute outside, while using the DLLIMPORT import method must use "extern" The modifier; the final GetWindowsDirectory function contains two parameters, one for the StringBuilder type, the other is int type, the content returned by this method exists in the parameter of the StringBuilder type. At the same time, because we use the StringBuilder class here, we have to add the namespace of the system.text at the beginning of the program, and the method is the same. Declare that other API functions to be used in the program:
[DLLIMPORT ("User32")]]
Public Static Extern long exitwindowsex (long dwreserve);
[DLLIMPORT ("shell32")]]]
Public Static Extern long shellabout (long dweeserved);
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. Write the form of the form
On the form of the form (write the following code during the event:
Private void frmmain1_load (Object Sender, System.EventArgs E)
{
File: // Initialize the component with system time
Time.text = system.datetime.today.toshortdateString () "" system.datetime.today.tolongtimeString ();
}
Write the following code during the ONTIMER event of the component Timer1:
/ / Write the following code during the ONTIMER event of the component Timer1:
Private void Timer1_timer (Object Sender, System.EventArgs E)
{
File: // Receive the current date and time for instant display
String currdate = system.datetime.today.toshortdateString ();
String currtime = system.datetime.today.toshosttimeString ();
File: // Check if the setting of the 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);
II. Design to obtain the Windows Form for System Information
Interface design
Add a Windows Form to the project and add the following control to the form:
2. Reference API functions in the form class
Using system.Runtime.InteropServices;
Using system.text;
[DLLIMPORT ("kernel32")]]
Public Static Extern Void getWindowsDirectory (StringBuilder WINDIR, INT Count);
[DLLIMPORT ("kernel32")]]
Public Static Extern void getSystemDirectory (StringBuilder sysdir, int count);
[DLLIMPORT ("kernel32")]]
Public Static Extern Void getSystemInfo (Ref CPU_INFO CPUINFO);
[DLLIMPORT ("kernel32")]]
Public Static Extern Void GlobalMemoryStatus (Ref Memory_Info MemInfo);
[DLLIMPORT ("kernel32")]]
Public Static Extern Void getSystemTime (Ref SystemTime_INFO STINFO);
The roles of the above APIs are acquisition system paths, obtain CPU-related information, and obtain information about memory, obtain system time, and the like.
3. Define the following structures
After declaring all the API functions, we found that the three functions were used to use CPU_INFO, MEMORY_INFO, SYSTEMTIME_INFO, etc., which are not .NET inside, where did they come from? In fact, we need to use the above structure when used in the above API call, and we store the information obtained by the function call in the above structure, and finally returns to the program output. These structures are more complicated, but if the developer can be skilled, the entire API world will be in the mastery of the developer. Hereinafter, the statement of the above structure:
/ / Define the information structure of the CPU
[Structlayout (layoutkind.sequential)]
Public struct cpu_info
{
Public uint dwoemid; public uint dwpagesize;
Public uint lpminimumApplicationAddress;
Public uint lpmaximuMapplicationAddress;
Public uint dwactiveprocessormask;
Public uint dwnumberofprocessors;
Public uint dwprocessortype;
Public uint dwallocationGranularity;
Public uint dwprocessorlevel;
Public uint dwprocessorrevision;
}
FILE: / / Define the information structure of the memory
[Structlayout (layoutkind.sequential)]
Public struct memory_info
{
Public uint dwlength;
PUBLIC uint dwmemoryload;
Public uint dwtotalphys;
Public uint dwavailphys;
Public uint dwtotalpagefile;
Public uint dwavailpagefile;
Public uint dwtotalvirtual;
Public uint dwavailvirtual;
}
File: / / Define the information structure of the system time
[Structlayout (layoutkind.sequential)]
Public struct systemtime_info
{
Public ushort wyear;
Public ushort wmonth;
Public Ushort WDAYOFWEEK;
Public ushort wday;
Public ushort wheh;
Public ushort wminute;
Public Ushort WSecond;
Public ushort wmilliseconds;
}
5. Write the form of the form
Private void Button1_Click (Object Sender, System.Eventargs E)
{
File: // Call the getWindowsDirectory and getSystemDirectory functions to get the Windows path and system path, respectively.
Const int nchars = 128;
Stringbuilder buff = new stringbuilder;
GetWindowsDirectory (BUFF, NCHARS);
WindowsDirectory.text = "Windows path:" buff.tostring ();
GetsystemDirectory (BUFF, NCHARS);
SystemDirectory.text = "System Path:" Buff.Tostring ();
File: // Call the getSystemInfo function to get information about the CPU
CPU_INFO CPUINFO;
CPUINFO = New CPU_INFO ();
GetSystemInfo (Ref CPUInfo);
Numberofprocessors.text = "There is" cpuinfo.dwnumberofprocessors.Tostring () "a CPU" in this computer;
Processortype.text = "CPU type" cpuinfo.dwprocessortype.tostring ();
ProcessorLevel.Text = "CPU Level" cpuinfo.dwprocessorleVel.toTString (); OEMID.TEXT = "OEM ID of CPU" cpuinfo.dwoemid.tostring ();
PageSize.text = "The page size in the CPU is" cpuinfo.dwpagesize.tostring ();
File: // Call the GlobalMemoryStatus function to get memory related information
Memory_INFO MEMINFO;
Meminfo = New Memory_info ();
GlobalMemoryStatus (Ref Meminfo);
MemoryLoad.text = MEMINFO.DWMEMORYLOAD.TOSTRING () "% of the memory is being used";
Totalphys.text = "Physical memory shared" MEMINFO.DWTOTALPHYS.TOSTRING () "byte";
Availphys.text = "The physical memory available" MEMINFO.DWAVAILPHYS.TOSTRING () "bytes";
Totalpagefile.text = "The total size of the exchange file is" MEMINFO.DWTOTALPAGEFILE.TOSTOSTRING () "byte";
AvailPageFile.text = "Mounts for Exchange file size MEMINFO.DWAVAILPAGEFILE.TOSTOSTRING () " bytes ";
TotalVirtual.Text = "There is" MEMINFO.DWTOTALVIRTUAL.TOSTRING () "byte";
Availvirtual.Text = "There is no virtual memory" meminfo.dwavailvirtual.toString () "byte";
File: // Call the getSystemTime function to get system time information
Systemtime_info stinfo;
Stinfo = new systemtime_info ();
GetSystemTime (Ref Stinfo);
Date.text = stinfo.wyear.tostring () "Year" stinfo.wmonth.tostring () "Month" stinfo.wday.toString () "Day";
Time.text = (stinfo.wHour 8) .tostring () "point" stinfo.wminute.totring () "分" stinfo.wsecond.toString () "second";
}
Three.
The whole process of developing multi-function shutdown programs above is introduced, which has a practical value. Through this study, I believe that a slightly API usage based developers can touch the category bypass, soaster Master the operation of the API in Visual C #. The examples given above are just a simple program, but interested readers can further improve their functions and make a more perfect system application.