C#实现常见系统控制

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();
            }
        }
    }
}
一、系统说明 | ----------------------------------- 本系统是在Microsoft Visual Studio 2003环境下用C#语言编写的个人信息管理系统。 其主要功能有: ------------- 1.文章管理模块  (1)用户发表新文章,可以将文章设为公开或不公开,如果设置为公开的,则所 有使用本系统的用户都可以浏览到你的文章。 (2)用户可以新建文章分类,在文章查询时,文章分类可以做为查询的一个依据。 (3)用户可以查询本人的所有文章,也可以查询其它用户公开的文章,查询条件可以为: 根据文章标题、内容、文章类别、发表时间、修改时间、用户名。 (4)用户可以删除、修改、改变文章是否公开、导出为WORD,但对于其它用户的文章,这些功能有所限制。 -------------- 2.日记管理模块 (1)用户发表新日记,可以将日记设为公开或不公开,如果设置为公开的,则所 有使用本系统的用户都可以浏览到你的日记。 (2)用户可以新建日记分类。 (3)用户可以查询本人的所有日记,也可以查询其它用户公开的日记,查询条件可以为: 根据日记标题、内容、类别、发表时间、修改时间、用户名、心情、天气。 (4)用户可以删除、修改、改变日记是否公开、导出为WORD,但对于其它用户的文章,这些功能有所限制。 -------------- 3.相册管理模块 (1)用户可以上传新图片,可以将图片设为公开或不公开,如果设置为公开的,则所 有使用本系统的用户都可以浏览到你的图片。 (2)用户可以新建相册分类。 (3)用户可以浏览本人的所有图片,以及其它用户公开的图片。 (4)用户可以删除、更新、导出图片,但对于其它用户的图片,这些功能有所限制。 -------------- 4.备忘管理模块 (1)用户可以写新备忘,可以设置备忘提醒时间、是否提醒、备忘分类、紧急程度、查看阴阳历。 (2)用户可以新建备忘分类。 (3)用户可以查询本人的所有备忘,查询条件有:备忘标题、内容、提醒时间、是否完成、紧急程度、备忘分类。 (4)用户可以删除、更新备忘。 -------------- 5.费用管理模块 (1)用户可以写新费用,可以填写费用名称,说明,数量,单价。 (2)用户可以查询本人的所有费用,可以统计查询到的费用情况,并可以将查询记录导出到EXCEL中保存。 (3)用户可以删除、修改费用。 ---------------------------- 6.信息管理模块 (1) 用户可以更改密码。 (2) 提供管理日常联系人的信息功能,可以批量从EXCEL中导入联系人,也可以批量导出联系人。 ---------------------------- |
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值