c# 实现锁屏及禁止键盘和鼠标

1.要实现锁定系统不让别人用,可以调用系统锁定API函数来实现

//引入API函数

        [DllImport("user32 ")]

        public static extern bool LockWorkStation();//这个是调用windows的系统锁定


2.API函数锁定键盘及鼠标


 [DllImport("user32.dll")]

        static extern void BlockInput(bool Block);

需要的时候就直接写:

 
 
BlockInput( true ); // 锁定鼠标及键盘 BlockInput( false ); // 解除键盘鼠标锁定


3.屏蔽ctrl+alt+delete

FileStream fs = 

    new FileStream(Environment.ExpandEnvironmentVariables(

        "%windir%\\system32\\taskmgr.exe"), FileMode.Open);

//byte[] Mybyte = new byte[(int)MyFs.Length];

 //MyFs.Write(Mybyte, 0, (int)MyFs.Length);

//用文件流打开任务管理器应用程序而不关闭文件流就会阻止打开任务管理器

 //MyFs.Close(); 

下面给出所有代码:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Runtime.InteropServices;

using Microsoft.Win32;

using System.IO;



namespace 锁屏

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

        //引入API函数

        [DllImport("user32 ")]

        //这个是调用windows的系统锁定

        public static extern bool LockWorkStation();

        [DllImport("user32.dll")]

        static extern void BlockInput(bool Block);



        private void lockTaskmgr()//锁定任务管理器

        {

            FileStream fs = 

                new FileStream(Environment.ExpandEnvironmentVariables(

                    "%windir%\\system32\\taskmgr.exe"), FileMode.Open);

            //byte[] Mybyte = new byte[(int)MyFs.Length];

            //MyFs.Write(Mybyte, 0, (int)MyFs.Length);

            //MyFs.Close(); 

            //用文件流打开任务管理器应用程序而不关闭文件流就会阻止打开任务管理器

        }



        private void lockAll()

        {

            BlockInput(true);//锁定鼠标及键盘

        }



        private void Form1_Load(object sender, EventArgs e)

        {

            //this.lockAll();

            this.lockTaskmgr();

        }



        private void btnUnlock_Click(object sender, EventArgs e)

        {

            if (txtPwd.Text == "19880210")

            {

                BlockInput(false);

                Application.Exit();

            }

            else

            {

                MessageBox.Show("密码错误!", "消息",

                    MessageBoxButtons.OK, MessageBoxIcon.Information);

                txtPwd.Text = "";

                txtPwd.Focus();

            }

        }

    }

}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值