Multifunctional shutdown program designed by Visual C #

Source: Internet
Author: User

Visual C #
Design multi-function shutdown program

Many software have auto power off function,
Especially in the long time
Download
The time,
This feature allows you not to wait in front of the computer,
And the computer can follow
Your prior set-up is automatically turned off. Now we use
Visual
C#
To write a multi-function shutdown program. The program has: Timer shutdown, countdown shutdown, shut down the computer
Four functions such as reminders, system information acquisition, etc.
,
The shutdown time can be set precisely to seconds. And let you know it quickly.
Visual
C#
In the
Api
Operating procedures.

One
.
Design off
Windows
Form

    1. Design of the interface

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

Control Name

Category

Text
Control Name

Category

Text
CheckBox1
CheckBox
Auto Power off

GrouPBox1
GroupBox
Current system time

CheckBox1
CheckBox
Countdown Execution Action

GroupBox2
GroupBox
Set time

CheckBox1
CheckBox
Timed alarms

Txttime
Textbox

Butcancle
Button
Cancel

Setuptime
DateTimePicker

Butreopen
Button
Restart

Setupdate
DateTimePicker

Butclose
Button
Shutdown

Timer1
Timer
100
Butsysinto
Button
System Information

Butrelogin
Button
Note Elimination

Windows
Form interface:
In the form properties, set the
Caption
Set to

Shut down
Windows
, the name is set to
"Frmmain"

    1. Referencing in a form class
      Api
      Function

Api
function is to construct
Windows
The cornerstone of the application is
Windows
A must-have tool for programming. Each type of
Windows
Application development tools provide an indirect or direct call to the
Windows
Api
Method of the function,
or call the
Windows
Api
The interface of the function,
This means that you have the ability to invoke dynamic connection libraries.
Visual
C#
As well as other development tools, you can invoke dynamic-link libraries
Api
Function.

In
Visual C #
Called in
Api
The basic process:

First, call the
Api
Before, you must first import
System.Runtime.InteropServices
This namespace. The namespace contains the
Visual
C#
Called in
Api
Some of the necessary collections, specific methods are as follows:

Using System.Runtime.InteropServices

Using System.Text

After the namespaces have been imported,
We're going to declare what we're going to use in the program.
Api
Function.
Our program is mainly to obtain information about the system,
So the use of
Api
Functions are returned to system information. First give the
Visual C #
Declared in
Api
Method of:

[DllImport ("user32")]
public static extern long SetWindowPos (long hwnd, long hwndinsertafter, long X, long y, long CX, long CY,
Long Wflagslong)

which
"DllImport"
property is used to invoke a method from a code that is never controlled.
It specifies the
Dll
The location,
The
Dll
The external method that contains the call;
"Kernel32"
Set the name of the class library;
"Public"
Indicates that the access type of the function is public;
"Static"
modifier declares a static element,
and the element belongs to the type itself, not
The specified object;
"extern"
Indicates that the method will be executed outside the project, using the
DllImport
The imported method must use the
"extern"
modifier; last
GetWindowsDirectory
The function contains two parameters, one for
StringBuilder
Type, and the other is
Int
Type, the content returned by the method exists
In
StringBuilder
The parameters of the type.
While
Because we're using it here.
StringBuilder
Class
So at the beginning of the program,
We also have to add
System.Text
This namespace is the same as the method.

Declare other applications that you want to use in your program.
Api
Function:

[DllImport ("user32")]
public static extern long ExitWindowsEx (long uflags, long dwreserved)
[DllImport ("Shell32")]
public static extern long Shellabout (long uflags, long dwreserved)

    1. Add a variable to a form class

Long dwreserved
const int SHUTDOWN = 1
const int REBOOT = 2
const int LOGOFF = 0
Long sh
int counter, n

    1. Methods for writing form classes

In the form's
Load (
The following code is written in the event procedure:

private void Frmmain1_load (object sender, System.EventArgs e)
{
file://
Initializing components with system time

Time.Text = System.DateTime.Today.ToShortDateString () + "" + System.DateTime.Today.ToLongTimeString ()
}

In the component
Timer1
Of
OnTimer
The following code is written in the event procedure:

/ /
In the component
Timer1
Of
OnTimer
The following code is written in the event procedure:

private void Timer1_timer (object sender, System.EventArgs e)
{
file://
Receive the current date and time for immediate display

String currdate=system.datetime.today.toshortdatestring ()
String currtime=system.datetime.today.toshorttimestring ()
file://
Detects if the set shutdown date and time are valid

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)}
Two
.
Design to obtain system information
Windows
Form

    1. Design of the interface

Add one to the project
Windows
Form and add the following controls to the form:

    1. Referencing in a form class
      Api
      Function

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)

Above several
Api
The function is to obtain the system path, obtain
Cpu
Related information, obtain the relevant information of memory, obtain the system time and so on.

    1. Define the following structures

After declaring all the
Api
function, we found that the following three functions were used
Cpu_info

Memory_info

Systemtime_info
and other structures, which
Some structures are not
. Net
Inside, where do they come from? In fact, we are using the above
Api
Call, we use the above structure, we will get the function call
The information is stored in the structure above and finally returned to the program output. These structures are more complex, but if the developers are able to use them skillfully, the entire
Api
The world will be in the hands of developers. The following is the declaration of the above structure:

//
Defined
Cpu
The 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://
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://
Information structure defining 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
}

    1. Methods for writing form classes

private void Button1_Click (object sender, System.EventArgs e)
{
file://
Call
GetWindowsDirectory
And
GetSystemDirectory
functions are respectively obtained
Windows
Path and system path

const int nchars = 128
StringBuilder Buff = new StringBuilder (nchars)
GetWindowsDirectory (Buff,nchars)
Windowsdirectory.text = "Windows
Path:
"+buff.tostring ()
GetSystemDirectory (Buff,nchars)
Systemdirectory.text = "
System path:
"+buff.tostring ()

file://
Call
GetSystemInfo
function gets
Cpu
The relevant information

Cpu_info CpuInfo
CpuInfo = new Cpu_info ()
GetSystemInfo (ref CpuInfo)
Numberofprocessors.text = "
This computer has
"+cpuinfo.dwnumberofprocessors.tostring () +"
A
CPU ";
Processortype.text = "CPU
is of type
"+cpuinfo.dwprocessortype.tostring ()
Processorlevel.text = "CPU
Level is
"+cpuinfo.dwprocessorlevel.tostring ()
Oemid.text = "CPU
Of
OEM ID
For
"+cpuinfo.dwoemid.tostring ()
Pagesize.text = "CPU
The page size in
"+cpuinfo.dwpagesize.tostring ()

file://
Call
GlobalMemoryStatus
Function gets information about the memory

Memory_info Meminfo
Meminfo = new Memory_info ()
GlobalMemoryStatus (ref Meminfo)
Memoryload.text = MemInfo.dwMemoryLoad.ToString () + "%
The memory is in use

Totalphys.text = "
Total Physical Memory
"+meminfo.dwtotalphys.tostring () +"
Bytes

Availphys.text = "
Physical entities that can be used
"+meminfo.dwavailphys.tostring () +"
Bytes

Totalpagefile.text = "
Exchange
Total file size is
"+meminfo.dwtotalpagefile.tostring () +"
Bytes

Availpagefile.text = "
Fair
Exchange
File size is
"+meminfo.dwavailpagefile.tostring () +"
Bytes

Totalvirtual.text = "
Total Virtual Internal existence
"+meminfo.dwtotalvirtual.tostring () +"
Bytes

Availvirtual.text = "
No virtual entities in use
"+meminfo.dwavailvirtual.tostring () +"
Bytes

file://
Call
GetSystemTime
Function gets system time information

Systemtime_info Stinfo
Stinfo = new Systemtime_info ()
GetSystemTime (ref Stinfo)
Date.text = StInfo.wYear.ToString () + "
Years
"+stinfo.wmonth.tostring () +"
Month
"+stinfo.wday.tostring () +"
Day

Time.Text = (stinfo.whour+8). ToString () + "
Point
"+stinfo.wminute.tostring () +"
Score of
"+stinfo.wsecond.tostring ()
+
Seconds

}

Three
.
Conclusion.

The above describes
Visual C #
The whole process of developing multi-functional shutdown program has some practical value. Through the study of this article, I believe that slightly
Api
Developers using the basics can immediately comprehend by analogy and quickly master
Visual
C#
In the
Api
The operation. The example given above is just a simple program, but
Interested readers can further refine their functions. Make a more perfect system application

Multifunctional shutdown program designed by Visual C #

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.