因为在关闭窗口时消息的顺序是倒过来的,肯定是窗口自己的消息循环先捕获到这个消息么,要抛出来才能被外面的application捕获到 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms;namespace WindowsFormsApplication11 { public partial class Form1 : Form, IMessageFilter { int WM_CLOSE = 0x0010; public Form1() { InitializeComponent(); Application.AddMessageFilter(this); this.KeyPreview = true; } public bool PreFilterMessage(ref Message m) { if (m.Msg == WM_CLOSE) { MessageBox.Show("不许关闭。"); return true; } else return false; } protected override void WndProc(ref Message m) { if (m.Msg == WM_CLOSE) Application.FilterMessage(ref m); else base.WndProc(ref m); } } }
补充一下,准确的说,wm_create,wm_close之类窗口消息都被wndproc吃掉了 而鼠标左键你会看到是imessagefilter先捕获到 现在居然不能连续发贴3次以上 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms;namespace WindowsFormsApplication11 { public partial class Form1 : Form, IMessageFilter { int WM_LBUTTONDOWN = 0x0201; public Form1() { InitializeComponent(); Application.AddMessageFilter(this); } public bool PreFilterMessage(ref Message m) { if (m.Msg == WM_LBUTTONDOWN) MessageBox.Show("PreFilterMessage - WM_LBUTTONDOWN"); return false; } protected override void WndProc(ref Message m) { if (m.Msg == WM_LBUTTONDOWN) MessageBox.Show("WndProc - WM_LBUTTONDOWN"); base.WndProc(ref m); } } }在form上点击 先弹出 PreFilterMessage - WM_LBUTTONDOWN 再弹出 WndProc - WM_LBUTTONDOWN
{
public Form1()
{
InitializeComponent(); Application.AddMessageFilter(this);
} public bool PreFilterMessage(ref Message m)
{
if (m.Msg ==...)
{
...
}
}
}
IMessageFilter 拦截应用程序所有消息
WndProc 拦截本窗口消息
WndProc 拦截本窗口消息
我发现IMessageFilter不能拦截wm_close
而WndProc 可以
为什么呀
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace WindowsFormsApplication11
{
public partial class Form1 : Form, IMessageFilter
{
int WM_CLOSE = 0x0010;
public Form1()
{
InitializeComponent(); Application.AddMessageFilter(this); this.KeyPreview = true;
} public bool PreFilterMessage(ref Message m)
{
if (m.Msg == WM_CLOSE)
{
MessageBox.Show("不许关闭。");
return true;
}
else
return false;
} protected override void WndProc(ref Message m)
{
if (m.Msg == WM_CLOSE)
Application.FilterMessage(ref m);
else
base.WndProc(ref m);
}
}
}
是多余的
WndProc 继承自Control,收到针对本窗口自身的消息
使用参考
http://social.msdn.microsoft.com/forums/en-US/winforms/thread/93283ae1-6673-4470-ba46-8313215b92cb/
而鼠标左键你会看到是imessagefilter先捕获到
现在居然不能连续发贴3次以上
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;namespace WindowsFormsApplication11
{
public partial class Form1 : Form, IMessageFilter
{
int WM_LBUTTONDOWN = 0x0201; public Form1()
{
InitializeComponent(); Application.AddMessageFilter(this);
} public bool PreFilterMessage(ref Message m)
{
if (m.Msg == WM_LBUTTONDOWN)
MessageBox.Show("PreFilterMessage - WM_LBUTTONDOWN");
return false;
} protected override void WndProc(ref Message m)
{
if (m.Msg == WM_LBUTTONDOWN)
MessageBox.Show("WndProc - WM_LBUTTONDOWN");
base.WndProc(ref m);
}
}
}在form上点击
先弹出 PreFilterMessage - WM_LBUTTONDOWN
再弹出 WndProc - WM_LBUTTONDOWN