您的位置:首页 > 编程语言 > C#

如何在C#.Net中获取、屏蔽鼠标键盘消息?

2010-09-29 19:45 501 查看
1

static class StartupClass
2





{
3



/**////程序开始时候设置一个消息筛选MessageFilter,意思就是只要属于本程序的消息
4

///都要经过处理以后再发给本程序 打个比方,程序起来以后再程序的text里输入一
5

///个 9,但是这个9是系统先获得,然后系统通过消息的方式发给应用程序。这里的
6

///MessageFilter的意思就是所有系统发给应用程序的消息都要处理一下。
7

[STAThread]
8

static void MyMain()//Main()
9





{
10

RButtonMessageFilter filter = new RButtonMessageFilter();
11

Application.AddMessageFilter(filter);
12


13

Application.EnableVisualStyles();
14

Application.SetCompatibleTextRenderingDefault(false);
15

Application.Run(new Form());
16

//移除
17

Application.RemoveMessageFilter(filter);
18

}//在程序开始的时候添加一个本程序的系统消息的监测。
19


20

//然后下面的类里处理所有的系统消息!
21

public class RButtonMessageFilter : IMessageFilter
22





{
23

public bool PreFilterMessage(ref Message m)
24





{
25

const int WM_RBUTTONDBLCLK = 0x206;
26

const int WM_RBUTTONDOWN = 0x204;
27

const int WM_RBUTTONUP = 0x205;
28


29

const int WM_KEYDOWN = 0x100;
30

const int WM_KEYUP = 0x101;
31


32

switch (m.Msg)
33





{
34

//过滤掉所有与右键有关的消息
35

case WM_RBUTTONDBLCLK:
36

case WM_RBUTTONDOWN:
37

case WM_RBUTTONUP:
38





{
39

return true;
40

}
41

case WM_KEYDOWN: //有键盘按下
43





{
45

int k = m.WParam.ToInt32();
46

if (k == 57)
47





{
48

//屏蔽按键9 9的 asicii吗是57
49

return true;
50

}
51

else
52





{
53

//按下的其他建不屏蔽
54

return false;
55

}
56

}
57

default:
58





{
59

return false;
60

}
61

}
62

}
63

}
64


65

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: