日期:2014-05-17  浏览次数:20870 次

实现winform定时锁定功能
简单来说,我有个程序,用到两个form,mainform点击可以产生subform,两个form上都有textbox之类的控件。
现在要检测在这个程序中鼠标和键盘没有操作的时间达到1分钟的时候锁定这个程序(弹出另一个form以实现锁定功能)。
现在的问题在于
1.两个form在最前的时候移动鼠标或按下键盘
2.在两个form的控件范围内移动鼠标或使用键盘输入
这两种情况下都会重置程序的timer

如果控件和form太多,工作会很繁琐
我是新手……希望能够得到一种全局的解决方法……

------解决方案--------------------

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


 

呵呵,大家可以自己试试看,可以在此基础上面再修改增加些其他的东西!

下面给出所有代码:

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];
        &