怎么实现用C#禁用鼠标

列如;在win窗体拖个控件然后调试过程中一打开控件就让键盘用不了,除非停止调试或者重启让鼠标恢复正常,在那个控件中怎么写代码?... 列如;在win窗体拖个控件然后调试过程中一打开控件就让键盘用不了,除非停止调试或者重启让鼠标恢复正常,在那个控件中怎么写代码? 展开
 我来答
志当存高远389
2015-06-05 · 知道合伙人互联网行家
志当存高远389
知道合伙人互联网行家
采纳数:11237 获赞数:20404
08年毕业,一直从事计算机行业,从基层做起。有较强的实践操作能力。

向TA提问 私信TA
展开全部
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 ) );}}
追问
抱歉,本人完全新手,可以发图片来吗?
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式