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();
            }
        }
    }
}
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
课程通过实际项目融入常用开发技术架构,讲授风格独特,提供详细上课日志及答疑,赠送配套的项目架构源码注释详细清晰且表达通俗,均能直接在实际项目中应用,正真的物超所值,价格实惠任务作业:综合运用《C#/.Net企业级系统架构设计实战精讲教程》课程所学知识技能设计一个学生成绩管理系统的架构。要求:1.系统基于MVC的三层架构,各层单独建不同的解决方案文件夹。2.采用Model First开发方式,设计架构时只需要设计学生表(TbStudent)和课程表(TbCourse)。学生表必须有的字段是ID、stuName、age;课程表必须有的字段是ID、courseName、content。3.数据访问层采用Entity Framework或NHibernate来实现,必须封装对上述表的增删改查方法。4.必须依赖接口编程,也就是必须要有数据访问层的接口层、业务逻辑层的接口层等接口层。层层之间必须减少依赖,可以通过简单工厂或抽象工厂。5.至少采用简单工厂、抽象工厂、Spring.Net等技术中的2种来减少层与层之间的依赖等。6.封装出DbSession类,让它拥有所有Dal层实例和SaveChanges方法。7.设计出数据访问层及业务逻辑层主要类的T4模板,以便实体增加时自动生成相应的类。8.表现层要设计相关的控制器和视图来验证设计的系统架构代码的正确性,必须含有验证增删改查的方法。9.开发平台一定要是Visual Studio平台,采用C#开发语言,数据库为SQL Server。10.提交整个系统架构的源文件及生成的数据库文件。(注意: 作业需写在CSDN博客中,请把作业链接贴在评论区,老师会定期逐个批改~~)

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值