1 实现电脑休眠
2 实现禁止关机
3 注销计算机
4 实现计算机关机
5 实现计算机重启
6 打开鼠标设置
7 打开桌面设置
8 打开网络连接
9 程序在任务栏隐藏
10 实现屏幕保护
11 调用EXE文件
12 关闭右键功能
13 实现截图功能
14 实现程序只能运行一次
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace 系统控制应用
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Application.SetSuspendState(PowerState.Hibernate, true, false);//设置电脑休眠
}
int isClose = 0;
const int CLOSE_CMD = 0x0011;//系统发送的关机命令
protected override void WndProc(ref Message m)//重写windos消息处理
{
switch(m.Msg)
{
case CLOSE_CMD:
m.Result = (IntPtr)isClose;
break;
default:
base.WndProc(ref m);
break;
}
}
private void button2_Click(object sender, EventArgs e)
{
if(isClose==1)
{
isClose = 0;
MessageBox.Show("禁止关机");
}
else
{
isClose = 1;
MessageBox.Show("允许关机");
}
}
[DllImport("user32.dll", EntryPoint = "ExitWindowsEx", CharSet = CharSet.Ansi)]
private static extern int ExitWindowsEx(int uFlags, int dwReserved);
private void button3_Click(object sender, EventArgs e)
{
ExitWindowsEx(0, 0);//注销计算机
}
private void button4_Click(object sender, EventArgs e)//执行关机
{
System.Diagnostics.Process myProcess = new System.Diagnostics.Process();
myProcess.StartInfo.FileName = "cmd.exe";
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.RedirectStandardError = true;//是否将错误信息写入流
myProcess.StartInfo.RedirectStandardInput = true;//是否从流中读取
myProcess.StartInfo.RedirectStandardOutput = true;//是否写入流
myProcess.StartInfo.CreateNoWindow = true;//是否在新窗口中启动进程
myProcess.Start();
myProcess.StandardInput.WriteLine("shutdown -s -t 0");
}
private void button5_Click(object sender, EventArgs e)//重启计算机
{
System.Diagnostics.Process myProcess = new System.Diagnostics.Process();
myProcess.StartInfo.FileName = "cmd.exe";
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.RedirectStandardError = true;//是否将错误信息写入流
myProcess.StartInfo.RedirectStandardInput = true;//是否从流中读取
myProcess.StartInfo.RedirectStandardOutput = true;//是否写入流
myProcess.StartInfo.CreateNoWindow = true;//是否在新窗口中启动进程
myProcess.Start();
myProcess.StandardInput.WriteLine("shutdown -r -t 0");
}
private void button6_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("main.cpl");//打开鼠标设置
}
private void button7_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("desk.cpl");//打开桌面设置
}
private void button8_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("ncpa.cpl");//打开网络连接
}
private void button9_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("mmsys.cpl");//打开声音连接
}
private void button10_Click(object sender, EventArgs e)
{
this.ShowInTaskbar = false;//控制程序不在任务栏显示
}
private const int WM_CMD = 0x0112;
private const int SC_SCREEN = 0xf140;
[DllImport("user32.dll")]
private static extern bool SendMessage(IntPtr hWnd, int wMsg, int wPararm, int lParam);
private void button11_Click(object sender, EventArgs e)
{
SendMessage(this.Handle, WM_CMD, SC_SCREEN, 0);//设置屏幕保护
}
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Enter)
textBox2.Focus();//跳转到文本框
}
private void textBox2_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Enter)
button12.Focus();//跳转到按钮
}
private void button13_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "exe 文件(*.exe)|*.exe";//控制文件类型exe
if (openFileDialog1.ShowDialog() == DialogResult.OK)
System.Diagnostics.Process.Start(openFileDialog1.FileName);//启动程序
}
private void textBox3_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
textBox3.ContextMenu = new ContextMenu();//屏蔽右键功能
}
[DllImport("user32.dll", EntryPoint = "GetSystemMetrics")]
private static extern int GetSystemMetrics(int mVal);
[DllImport("user32.dll", EntryPoint = "CopyIcon")]
private static extern int CopyIcon(IntPtr hIcon);
/// <summary>
/// 不用鼠标截全屏
/// </summary>
/// <returns></returns>
private Bitmap CaptureNoCursor()
{
Bitmap Sourse = new Bitmap(GetSystemMetrics(0), GetSystemMetrics(1));
using (Graphics g = Graphics.FromImage(Sourse))
{
g.CopyFromScreen(0, 0, 0, 0, Sourse.Size);
g.Dispose();
}
return Sourse;
}
//private Bitmap CaptureDesktop()
//{
// try
// {
// int _CX = 0, _CY = 0;
// Bitmap Sourse = new Bitmap(GetSystemMetrics(0), GetSystemMetrics(1));
// using (Graphics g = Graphics.FromImage(Sourse))
// {
// g.CopyFromScreen(0, 0, 0, 0, Sourse.Size);
// g.DrawImage(captu)
// g.Dispose();
// }
// }
//}
//private Bitmap CaptureCursor(ref _CX,ref _CY)
//{
// IntPtr _Icon;
// CURSORINFO
//}
//{
//}
private void button14_Click(object sender, EventArgs e)
{
}
private void button15_Click(object sender, EventArgs e)
{
//查看form_load内容
}
private void Form1_LocationChanged(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
bool Exist;
System.Threading.Mutex NewMutes = new System.Threading.Mutex(true, "仅一次",out Exist);//创建Mutex互斥对象
if(Exist)
{
NewMutes.ReleaseMutex();//运行新窗体
}
else
{
MessageBox.Show("本程序只能运行一个");
this.Close();
}
}
}
}