winform中屏幕锁定功能实现

winform中屏幕锁定功能实现

效果图:

技术实现:

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace HHLInfoManage.Common
{
    internal class HotKey
    {
        //如果函数执行成功,返回值不为0。
        //如果函数执行失败,返回值为0。要得到扩展错误信息,调用GetLastError。
        //组合热键 用 "|"分隔
        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool RegisterHotKey(
            IntPtr hWnd,                //要定义热键的窗口的句柄
            int id,                     //定义热键ID(不能与其它ID重复)          
            KeyModifiers fsModifiers,   //标识热键是否在按Alt、Ctrl、Shift、Windows等键时才会生效
            Keys vk                     //定义热键的内容
            );

        [DllImport("user32.dll", SetLastError = true)]
        public static extern bool UnregisterHotKey(
            IntPtr hWnd,                //要取消热键的窗口的句柄
            int id                      //要取消热键的ID
            );

        //定义了辅助键的名称(将数字转变为字符以便于记忆,也可去除此枚举而直接使用数值)
        [Flags()]
        public enum KeyModifiers
        {
            None = 0,
            Alt = 1,
            Ctrl = 2,
            Shift = 4,
            WindowsKey = 8
        }
    }
}


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace HHLInfoManage
{
    public partial class FrmPinMSD : Form
    {
        public FrmPinMSD()
        {
            InitializeComponent();
        }

        private void QueDbuttonX_Click(object sender, EventArgs e)
        {
            //解除锁定
            if (frmLogin.str_PassWord.Equals(this.PassWordtextBoxX.Text.Trim()))
            {
                frmInfoMain.Info.Opacity = 1;
                frmInfoMain.PinMSD = null;
                this.Close();
            }
            else
            {
                MessageBox.Show("密码错误!","提示");
            }
        }

        private void FrmPinMSD_Activated(object sender, EventArgs e)
        {
            Common.HotKey.RegisterHotKey(Handle, 100, Common.HotKey.KeyModifiers.Alt, Keys.O);
        }

        private void FrmPinMSD_Leave(object sender, EventArgs e)
        {
            Common.HotKey.UnregisterHotKey(Handle, 100);
        }

        private void FrmPinMSD_KeyDown(object sender, KeyEventArgs e)
        {
            if ((e.KeyCode == Keys.F4) && (e.Alt == true))
            {
                e.Handled = true;
            }
        }
    }
}


posted @ 2009-09-05 10:42  龙宫  阅读(1150)  评论(0编辑  收藏  举报