Visual C # design a multi-function Shutdown program

Source: Internet
Author: User

 

 

 

 
 

Many software have the automatic shutdown function, especially when you download it for a long time. However, this function does not allow you to wait in front of the computer, but the computer can be automatically disabled according to your preset. Now we use Visual C # To compile a multi-function Shutdown program. This program has four functions: Timed Shutdown, countdown shutdown, shutdown reminder, and system information acquisition. You can set the shutdown time to seconds. And allows you to quickly master the APIs in Visual C.

  I. Design and close Windows Forms

1. Interface Design

Create a standard project, add a Windows form to the project, add the following controls to the form, and set their properties respectively:

Control name Category Text Control name Category Text
Checkbox1 Checkbox Automatic shutdown Groupbox1 Groupbox Current System Time
Checkbox1 Checkbox Countdown operation Groupbox2 Groupbox Set Time
Checkbox1 Checkbox Timed alert Txttime Textbox  
Butcancle Button Cancel Setuptime Datetimepicker  
Butreopen Button Restart Setupdate Datetimepicker  
Butclose Button Shutdown Timer1 Timer 100
Butsysinto Button System Information Butrelogin Button Injection and Elimination

Windows form interface:

 

Set the caption in the form attribute to "Disable Windows" and the name to "frmmain ".

2. reference API functions in the Form class

API functions are the cornerstone of building a Windows application and an essential tool for Windows programming. Every windows application development tool provides methods for indirectly or directly calling Windows API functions, or calling Windows API functions, that is, it can call dynamic connection libraries. Visual C # can call the API functions of the dynamic link library like other development tools.

The basic process of calling an API in Visual C:

First, you must first import system. runtime. interopservices to the namespace before calling the API. This namespace contains some necessary sets for calling APIs in Visual C #. The specific method is as follows:

Using system. runtime. interopservices;
Using system. text;

After the namespace is imported, We need to declare the API functions used in the program. Our program mainly obtains information about the system, so all the API functions used return system information. First, declare 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 );

The "dllimport" attribute is used to call a method from uncontrollable code. It specifies the DLL location, which contains the called external method; and "Kernel32" sets the class library name; "public" indicates that the function access type is public; "static" modifier declares a static element, which belongs to the type rather than the specified object; "extern" indicates that the method will be executed outside the project, and the method imported using dllimport must use the "extern" modifier. Finally, the getwindowsdirectory function contains two parameters, one of which is of the stringbuilder type, the other is int type, and the content returned by this method exists in the stringbuilder type parameter. At the same time, because the stringbuilder class is used here, we have to add the system. Text namespace at the beginning of the program. The method is the same as above.

Declare other API functions used in the program:

[Dllimport ("USER32")]
Public static extern long exitwindowsex (long uflags, long dwreserved );
[Dllimport ("shell32")]
Public static extern long shellabout (long uflags, long dwreserved );

3. Add a variable for the form class

Long dwreserved;
Const int shutdown = 1;
Const int reboot = 2;
Const int logoff = 0;
Long sh;
Int counter, N;

4. Methods for writing form classes

Write the following code in the form load (event process:

Private void frmmaindomainload (Object sender, system. eventargs E)
{
File: // initialize the component at system time
Time. Text = system. datetime. Today. tow.datestring () + "" + system. datetime. Today. tolongtimestring ();
}

Write the following code during the ontimer event of the timer1 component:

// Write the following code during the ontimer event of the timer1 component:
Private void timer1_timer (Object sender, system. eventargs E)
{
File: // receives the current date and time for instant display.
String currdate = system. datetime. Today. tow.datestring ();
String currtime = system. datetime. Today. tow.timestring ();
File: // check whether the specified shutdown date and time are 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 button#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 );}

  2. Design a Windows form for obtaining system information

1. Interface Design

Add a Windows form to the project and add the following controls 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 );

These APIs are used to obtain the system path, CPU information, memory information, and system time.

3. Define the following structures

After declaring all the API functions, we found that the last three functions use cpu_info, memory_info, systemtime_info, and other structures. These structures are not in. net. Where do they come from? In fact, we need to use the above structure when using the above API calls. we store the information obtained by function calls in the above struct and finally return it to the program for output. These structures are complex, but if developers are skilled in using them, the entire API world will be under control by developers. The Declaration of the above struct is as follows:

// Define the CPU Information Structure
[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: // defines 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: // defines 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 whour;
Public ushort wminute;
Public ushort wsecond;
Public ushort wmilliseconds;
}

5. Compile the Form class Method

Private void button#click (Object sender, system. eventargs E)
{
File: // call the getwindowsdirectory and getsystemdirectory functions to obtain the windows and system paths respectively.
Const int nchars = 128;
Stringbuilder buff = new stringbuilder (nchars );
Getwindowsdirectory (buff, nchars );
Windows directory. Text = "Windows path:" + buff. tostring ();
Getsystemdirectory (buff, nchars );
Systemdirectory. Text = "System Path:" + buff. tostring ();

File: // call the getsysteminfo function to obtain CPU information.
Cpu_info cpuinfo;
Cpuinfo = new cpu_info ();
Getsysteminfo (ref cpuinfo );
Numberofprocessors. Text = "in this computer" + cpuinfo. dwnumberofprocessors. tostring () + "CPU ";
Processortype. Text = "CPU type" + cpuinfo. dwprocessortype. tostring ();
Processorlevel. Text = "CPU level" + cpuinfo. dwprocessorlevel. tostring ();
Oemid. Text = "cpu oem id is" + cpuinfo. dwoemid. tostring ();
Pagesize. Text = "page size in CPU is" + cpuinfo. dwpagesize. tostring ();

File: // call the globalmemorystatus function to obtain memory information.
Memory_info meminfo;
Meminfo = new memory_info ();
Globalmemorystatus (ref meminfo );
Memoryload. Text = meminfo. dwmemoryload. tostring () + "% memory in use ";
Totalphys. Text = "total physical memory" + meminfo. dwtotalphys. tostring () + "Byte ";
Availphys. Text = "available physical memory" + meminfo. dwavailphys. tostring () + "Byte ";
Totalpagefile. Text = "the total size of the swap file is" + meminfo. dwtotalpagefile. tostring () + "Byte ";
Availpagefile. Text = "the size of the interchangeable file is" + meminfo. dwavailpagefile. tostring () + "Byte ";
Totalvirtual. Text = "Total virtual memory" + meminfo. dwtotalvirtual. tostring () + "Byte ";
Availvirtual. Text = "unused Virtual Memory" + meminfo. dwavailvirtual. tostring () + "Byte ";

File: // call the getsystemtime function to obtain the 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. tostring () + "Minute" + stinfo. wsecond. tostring () + "seconds ";
}

  Iii. Conclusion.

The whole process of developing a multi-function Shutdown program in Visual C # is introduced above. This program has some practical value. Through the study in this article, I believe that developers who use basic APIs can immediately access the category and quickly learn how to operate APIs in Visual C. The above example is just a simple program, but interested readers can further improve its functions and make more perfect system applications.

 

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.