Breathe李

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

(摘自本人QQ空间)

     最近在项目中遇到一些问题,需要根据权限配置去控制用户对系统平台是否可以最小化和还原窗口大小的操作,并且屏蔽系统的一些快捷键,比如:alt+F4,windows键,任务管理器等一些操作,代码如下:

(禁止窗口最小化和还原(窗口启动之后为最大化))

  偷梁换柱(o(∩_∩)o 哈哈)

  重写WndProc方法:

 

View Code
1 constint SC_MINIMIZE =0xF020;//窗体最小化消息
2  constint SC_MAXIMIZE =0xF030;//窗体最大化消息
3  constint SC_NOMAL =0xF120;//窗体还原消息
4  protectedoverridevoid WndProc(ref Message m)
5 {
6
7 if (m.WParam.ToInt32() == SC_MINIMIZE)//点击最小化
8   {
9
10 //还可以做些其它的操作
11 m.WParam = (IntPtr)SC_MAXIMIZE;
12 }
13 if (m.WParam.ToInt32() == SC_NOMAL)//点击还原
14 {
15
16     //还可以做些点还原按钮时其它的操作
17
18 m.WParam = (IntPtr)SC_MAXIMIZE;
19 }
20 base.WndProc(ref m);
21
22 }

 下面是禁用快捷键的方法:

 先写一个钩子的公用类,代码如下:

View Code
1 using System;
2 using System.Collections.Generic;
3 using System.Text;
4 using System.Runtime.InteropServices;
5 using System.IO;
6 using System.Windows.Forms;
7 using System.Reflection;
8
9 namespace SISS.Client
10 {
11 publicclass KeyBoardHook
12 {
13 #region Delegates
14
15 publicdelegateint HookProc(int nCode, Int32 wParam, IntPtr lParam);
16
17 #endregion
18
19 #region 变量声明
20
21 private HookProc KeyboardHookProcedure;
22 private FileStream MyFs; // 用流来屏蔽ctrl alt delete
23
24 privateconstbyte LLKHF_ALTDOWN =0x20;//ALT
25 privateconstbyte VK_ESCAPE =0x1B;//ESC键
26 privateconstbyte VK_F4 =0x73;//F4健
27 privateconstbyte VK_NUMLOCK =0x90;//小键盘
28 privateconstbyte VK_SHIFT =0x10;//SHIFT键盘
29 privateconstbyte VK_TAB =0x09;//TAB键盘
30 publicconstint WH_KEYBOARD =13;//获得键盘
31 privateconstbyte VK_RCONTROL =0xA3;//CTRL键
32 privatestaticint hKeyboardHook =0;
33 #endregion
34
35 #region 对应COM的函数
36
37 [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
38 publicstaticexternint SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);
39
40 // 卸载钩子
41
42 [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
43 publicstaticexternbool UnhookWindowsHookEx(int idHook);
44
45 // 继续下一个钩子
46
47 [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
48 publicstaticexternint CallNextHookEx(int idHook, int nCode, Int32 wParam, IntPtr lParam);
49
50 #endregion
51
52 #region 方法
53 privateint KeyboardHookProc(int nCode, Int32 wParam, IntPtr lParam)
54 {
55 KeyMSG m = (KeyMSG)Marshal.PtrToStructure(lParam, typeof(KeyMSG));
56
57
58 if (((Keys)m.vkCode == Keys.LWin || (Keys)m.vkCode == Keys.RWin)
59 || ((m.vkCode == VK_TAB) && ((m.flags & LLKHF_ALTDOWN) !=0))//屏蔽alt+TAB
60 || (m.vkCode == VK_F4) && ((m.flags & LLKHF_ALTDOWN) !=0)//屏蔽alt+F4
61 || ((Keys)m.vkCode == Keys.LWin) && ((Keys)m.vkCode == Keys.D)//屏蔽windows+D键
62 || ((Keys)m.vkCode == Keys.RWin) && ((Keys)m.vkCode == Keys.D)
63 || (m.vkCode == VK_ESCAPE) && ((m.flags & LLKHF_ALTDOWN) !=0)//屏蔽alt+Esc
64 || (m.vkCode == VK_RCONTROL) && (m.vkCode == VK_ESCAPE)//屏蔽Ctrl+Esc
65
66
67 )
68 {
69 return1;
70 }
71
72 return CallNextHookEx(hKeyboardHook, nCode, wParam, lParam);
73 }
74 ///<summary>
75 /// 启动钩子
76 ///</summary>
77 publicvoid HookStart()
78 {
79 if (hKeyboardHook ==0)
80 {
81 // 创建HookProc实例
82
83 KeyboardHookProcedure =new HookProc(KeyboardHookProc);
84
85 hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD,
86 KeyboardHookProcedure,
87 Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]),
88 0);
89
90 // 如果设置钩子失败
91
92 if (hKeyboardHook ==0)
93 {
94 HookStop();
95 }
96
97 //用二进制流的方法打开任务管理器。而且不关闭流.这样任务管理器就打开不了
98 MyFs =new FileStream(Environment.ExpandEnvironmentVariables("%windir%\\system32\\taskmgr.exe"),
99 FileMode.Open);
100 byte[] MyByte =newbyte[(int)MyFs.Length];
101 MyFs.Write(MyByte, 0, (int)MyFs.Length);
102 }
103 }
104 ///<summary>
105 /// 卸载钩子
106 ///</summary>
107 publicvoid HookStop()
108 {
109 bool retKeyboard =true;
110
111 if (hKeyboardHook !=0)
112 {
113 retKeyboard = UnhookWindowsHookEx(hKeyboardHook);
114
115 hKeyboardHook =0;
116 }
117
118 if (null!= MyFs)
119 {
120 MyFs.Close();
121 }
122
123 if (!(retKeyboard))
124 {
125 thrownew Exception("卸载钩子失败");
126 }
127 }
128
129
130 #endregion
131
132 #region Nested type: KeyMSG
133
134 publicstruct KeyMSG
135 {
136 publicint vkCode;//获得键盘输入的直
137 publicint scanCode;
138 publicint flags;//键盘的状态
139 }
140
141 #endregion
142
143
144
145 }
146 }

再在窗口Load事件调用就OK了,关闭的时候一定要卸载钩子,要不键盘快捷键还是不能用!

还需了解一些windows消息机制,消息数值在winuser.h头文件中!

posted on 2011-05-16 14:51  LouisLee  阅读(1741)  评论(0编辑  收藏  举报