程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> C# .NET鎖屏程序(順帶屏蔽任務管理器)

C# .NET鎖屏程序(順帶屏蔽任務管理器)

編輯:C#入門知識

第一步:將窗體的FormBorderStyle設置為none,WindowState設為Maximized

占據整個屏幕。

第二步:使用鉤子監控全局鍵盤事件。即屏蔽掉大部分系統熱鍵。但是屏蔽ctrl+alt+del 任務管理器則較復雜,這個特例後面討論。

使用全局鉤子應該注意的地方:將代碼放到一個獨立的類庫裡面(只有dll才能被注射到其他進程中)。

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.IO;
using System.Reflection;

namespace KeyboardHookDLL
{
    public class KeyboardHook
    {
        public delegate int KeyboardProc(int nCode, IntPtr wParam, IntPtr lParam);

        static int hKeyboardHook = 0;
        KeyboardProc KeyboardHookProcedure;

        /// <summary>
        /// 鉤子函數,需要引用空間(using System.Reflection;)
        /// 線程鉤子監聽鍵盤消息設為2,全局鉤子監聽鍵盤消息設為13
        /// 線程鉤子監聽鼠標消息設為7,全局鉤子監聽鼠標消息設為14
        /// </summary>

        public const int WH_KEYBOARD = 13;
        public const int WH_MOUSE_LL = 14;

        public struct KeyboardMSG
        {
            public int vkCode;
            public int scanCode;
            public int flags;
            public int time;
            public int dwExtraInfo;

        }
        //private FileStream MyFs;
//各種鍵位的ASC碼
        private const byte LLKHF_ALTDOWN = 0x20;
        private const byte VK_CAPITAL = 0x14;
        private const byte VK_ESCAPE = 0x1B;
        private const byte VK_F4 = 0x73;
        private const byte VK_LCONTROL = 0xA2;
        private const byte VK_NUMLOCK = 0x90;
        private const byte VK_RCONTROL = 0xA3;
        private const byte VK_SHIFT = 0x10;
        private const byte VK_TAB = 0x09;
        //public const int WH_KEYBOARD = 13;
        private const int WH_KEYBOARD_LL = 13;
        private const int WH_MOUSE = 7;
        //private const int WH_MOUSE_LL = 14;
        private const int WM_KEYDOWN = 0x100;
        private const int WM_KEYUP = 0x101;
        private const int WM_LBUTTONDBLCLK = 0x203;
        private const int WM_LBUTTONDOWN = 0x201;
        private const int WM_LBUTTONUP = 0x202;
        private const int WM_MBUTTONDBLCLK = 0x209;
        private const int WM_MBUTTONDOWN = 0x207;
        private const int WM_MBUTTONUP = 0x208;
        private const int WM_MOUSEMOVE = 0x200;
        private const int WM_MOUSEWHEEL = 0x020A;
        private const int WM_RBUTTONDBLCLK = 0x206;
        private const int WM_RBUTTONDOWN = 0x204;
        private const int WM_RBUTTONUP = 0x205;
        private const int WM_SYSKEYDOWN = 0x104;
        private const int WM_SYSKEYUP = 0x105;
        //private static int hKeyboardHook = 0;

        /// <summary>
        /// vs2008中的聲明方法,在vs2010中略有不同

        /// </summary>
        /// <returns></returns>
        [DllImport("kernel32")]
        public static extern int GetCurrentThreadId();

        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention =

CallingConvention.StdCall)]
        public static extern int SetWindowsHookEx(int idHook, KeyboardProc lpfn, IntPtr

hInstance, int threadId);

        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention =

CallingConvention.StdCall)]
        public static extern bool UnhookWindowsHookEx(int idHook);

        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
        public static extern int CallNextHookEx(int idHook, int nCode, IntPtr wParam, IntPtr lParam);

        [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.Winapi)]
        public static extern short GetKeyState(int keycode);

//在這裡你可以自己定義要攔截的鍵。
        private int KeyboardHookProc(int nCode, IntPtr wParam, IntPtr lParam)
        {
            KeyboardMSG m = (KeyboardMSG)Marshal.PtrToStructure(lParam, typeof(KeyboardMSG));
            if (
            ((int)m.vkCode == 91) || ((int)m.vkCode == 92) ||
            ((m.vkCode == VK_TAB) && ((m.flags & LLKHF_ALTDOWN) != 0)) ||
            ((m.vkCode == VK_ESCAPE) && ((m.flags & LLKHF_ALTDOWN) != 0)) ||
            ((m.vkCode == VK_F4) && ((m.flags & LLKHF_ALTDOWN) != 0)) ||
            (m.vkCode == VK_ESCAPE) && ((GetKeyState(VK_LCONTROL) & 0x8000) != 0) ||
            (m.vkCode == VK_ESCAPE) && ((GetKeyState(VK_RCONTROL) & 0x8000) != 0)
            )
            {
         return 1;
            }
            return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);
   &nb

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved