为了账号安全,请及时绑定邮箱和手机立即绑定

请问在哪个控件怎么实现用C#禁用鼠标?

请问在哪个控件怎么实现用C#禁用鼠标?

C#
侃侃无极 2023-03-02 18:14:56
列如;在win窗体拖个控件然后调试过程中一打开控件就让键盘用不了,除非停止调试或者重启让鼠标恢复正常,在那个控件中怎么写代码?
查看完整描述

1 回答

?
斯蒂芬大帝

TA贡献1827条经验 获得超8个赞

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 ) );}}


查看完整回答
反对 回复 2023-03-06
  • 1 回答
  • 0 关注
  • 227 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号