Application.AddMessageFilter() 是截获本程序向系统发出的消息,和挂钩HOOK是不一样的
public Form1()
{
InitializeComponent();
MyMessager msg = new MyMessager();
Application.AddMessageFilter(msg);
timer1.Start();
}
static int iOperCount = 0;
internal class MyMessager : IMessageFilter
{
public bool PreFilterMessage(ref Message m)
{
//如果检测到有鼠标或则键盘的消息,则使计数为0.....
if (m.Msg == 0x0200 || m.Msg == 0x0201 || m.Msg == 0x0204 || m.Msg == 0x0207)
{
iOperCount = 0;
}
return false;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
iOperCount++;
if (iOperCount > 100)
{
Application.Exit();
}
}
public Form1()
{
InitializeComponent();
MyMessager msg = new MyMessager();
Application.AddMessageFilter(msg);
timer1.Start();
}
static int iOperCount = 0;
internal class MyMessager : IMessageFilter
{
public bool PreFilterMessage(ref Message m)
{
//如果检测到有鼠标或则键盘的消息,则使计数为0.....
if (m.Msg == 0x0200 || m.Msg == 0x0201 || m.Msg == 0x0204 || m.Msg == 0x0207)
{
iOperCount = 0;
}
return false;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
iOperCount++;
if (iOperCount > 100)
{
Application.Exit();
}
}
本文探讨了在程序中使用 Application.AddMessageFilter() 方法拦截消息并利用定时器实现计数功能,通过自定义消息过滤器处理鼠标和键盘事件,同时介绍了如何通过计数逻辑退出程序。
5583

被折叠的 条评论
为什么被折叠?



