永发信息网

怎么实现用C#禁用鼠标

答案:1  悬赏:70  手机版
解决时间 2021-01-23 16:53
  • 提问者网友:你独家记忆
  • 2021-01-22 22:24
怎么实现用C#禁用鼠标
最佳答案
  • 五星知识达人网友:胯下狙击手
  • 2021-01-22 23:17
1.要实现锁定系统不让别人用,可以调用系统锁定API函数来实现

//引入API函数
[DllImport("user32 ")]
public static extern bool LockWorkStation();//这个是调用windows的系统锁定

在需要的时候直接写LockWorkStation();就可以啦!不信试试看!

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

[DllImport("user32.dll")]
static extern void BlockInput(bool Block);

需要的时候就直接写:

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

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

但是这种方式还是不能锁定ctrl+alt+delete,也就是还可以打开任务管理器,怎么办呢?

请看下面的方法:

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(); //用文件流打开任务管理器应用程序而不关闭文件流就会阻止打开任务管理器

利用windows锁屏API:LockWorkStation。
如下代码片段:
public Form1( bool aLock ) { if (aLock) { //锁屏+关屏LockWorkStation();SendMessage( this.Handle, (uint)0x0112, (IntPtr)0xF170, (IntPtr)2 );} else { //禁止鼠标键盘动作+关屏BlockInput( true );System.Threading.Thread.Sleep( 10 );SendMessage( this.Handle, (uint)0x0112, (IntPtr)0xF170, (IntPtr)2 );BlockInput( false );} this.Close(); //Application.Exit();Environment.Exit( 0 );} //关屏[DllImport( "user32.dll", CharSet = CharSet.Auto )] static extern IntPtr SendMessage( IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam ); //禁止鼠标键盘动作[return: MarshalAs( UnmanagedType.Bool )][DllImport( "user32.dll", CharSet = CharSet.Auto, ExactSpelling = true )] public static extern bool BlockInput( [In, MarshalAs( UnmanagedType.Bool )] bool fBlockIt ); //锁屏[DllImport( "user32.dll" )] public static extern bool LockWorkStation();需要指出的是,在退出程序时使用Environment.Exit( 0 );而非Application.Exit();否则会出错哦~~提示类似:“***遇到错误,需要关闭”。还有就是修改一下Main:static void Main(string[] args) { //Application.EnableVisualStyles(); //Application.SetCompatibleTextRenderingDefault( false ); if (args == null || args.Length == 0) { //禁止鼠标键盘动作+关屏Application.Run( new Form1( false ) );} else { //锁屏+关屏Application.Run( new Form1( true ) );}}追问抱歉,本人完全新手,可以发图片来吗?
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯