1、消息概述html
Windows下应用程序的执行是经过消息驱动的。消息是整个应用程序的工做引擎,咱们须要理解掌握咱们使用的编程语言是如何封装消息的原理。C#自定义消息通讯每每采用事件驱动的方式实现,但有时候咱们不得不采用操做系统的消息通讯机制,例如在和底层语言开发的DLL交互时,是比较方便的。下面列举了一些实现方式,供你们参考.编程
1 什么是消息(Message)安全
消息就是通知和命令。在.NET框架类库中的System.Windows.Forms命名空间中微软采用面对对象的方式从新定义了Message。新的消息(Message)结构的公共部分属性基本与早期的同样,不过它是面对对象的。框架
公共属性:编程语言
HWnd 获取或设定消息的处理函数ide
Msg 获取或设定消息的ID号函数
Lparam 指定消息的LParam字段post
Wparam 指定消息的WParam字段ui
Result 指定为响应消息处理函数而向OS系统返回的值this
2 消息驱动的过程
全部的外部事件,如键盘输入、鼠标移动、按动鼠标都由OS系统转换成相应的消息发送到应用程序的消息队列。每一个应用程序都有一段相应的程序代码来检索、分发这些消息到对应的窗体,而后由窗体的处理函数来处理。
2、C#中的消息的封装
C#对消息从新进行了面对对象的封装,在C#中消息被封装成了事件。
System.Windows.Forms.Application类具备用于启动和中止应用程序和线程以及处理Windows消息的方法。
调用Run以启动当前线程上的应用程序消息循环,并能够选择使其窗体可见。
调用Exit或ExitThread来中止消息循环。
C#中用Application类来处理消息的接收和发送的。消息的循环是由它负责的。
从本质上来说,每一个窗体通常都对应一个窗体过程处理函数。那么,C#的一个Form实例(至关于一个窗体)收到消息后是如何处理消息的?其实,这个问题的分析也就是展现了C#的消息封装原理。
实现鼠标左键按下的消息的响应(WM_LBUTTONDOWN)
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown1);
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown2);
private void Form1_MouseDown1(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(e.Button==System.Windows.Forms.MouseButtons.Left)
System.Windows.Forms.MessageBox.Show("消息被Form1_MouseDown1函数响应");
}
private void Form1_MouseDown2(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(e.Button==System.Windows.Forms.MouseButtons.Left)
System.Windows.Forms.MessageBox.Show("消息被Form1_MouseDown2函数响应");
}
上面this.MouseDown是C#中的一个事件。它的定义以下:
public event MouseEventHandler MouseDown;
而MouseEventHandler的定义为:
public delegate void MouseEventHandler( object sender,MouseEventArgs e);
实际上,上面定义了一个委托类型MouseEventHandler。委托了启用了其它编程语言中的函数指针的解决方案。与C++的函数指针不一样,委托是彻底面向对象的,同时封装了对象实例和方法。本质上,委托把一个实例和该实例上的方法函数封装成一个可调用的实体,它是面对对象的、安全的。
咱们能够把
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown1);
这条语句当作向this.MouseDown添加一个函数指针。
事件是对象发送的消息,以发送信号通知操做的发生。引起(触发)事件的对象叫作事件发送方。捕获事件并对事件做出响应的对象叫作事件接收方。在事件通信中,事件发送方类并不知道哪一个对象或方法将接收到(处理)它引起的事件。所须要的是在发送方和接收方之间存在一个媒介(相似指针的机制)。.NET框架定义了一个特殊的类型(Delegate委托),该类型提供函数指针的功能。这样,委托就等效于一个类型安全的函数指针或一个回调函数。
前面咱们向this.MouseDown事件添加了两个委托。
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown1);
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown2);
结果,咱们的两个函数Form1_MouseDown一、Form1_MouseDown2在咱们单击鼠标左键的时候都会被调用,并且调用的顺序和咱们添加委托的顺序一致。
WM_LBUTTONDOWN消息首先被Application类从应用程序消息队列中取出,而后分发到相应的窗体。窗体使用MouseDown事件中的函数指针调用已经添加的响应函数。因此C#中的事件字段实质上是一个函数指针列表,用来维护一些消息到达时的响应函数的地址。
3、结论
C#中消息的工做流程:
C#中的消息被Application类从应用程序消息队列中取出,而后分发到消息对应的窗体,窗体对象的第一个响应函数是对象中的protected override void WndProc(ref System.Windows.Forms.Message e)方法。
它再根据消息的类型调用默认的消息响应函数(如OnMouseDown),默认的响应函数而后根据对象的事件字段(如this.MouseDown )中的函数指针列表,调用用户所加入的响应函数(如Form1_MouseDown1和Form1_MouseDown2),并且调用顺序和用户添加顺序一致。
4、再回首Application类
Application类有一个AddMessageFilter的静态方法,经过它咱们能够添加消息筛选器,以便在向目标传递Windows消息时,检视这些消息。
使用消息筛选器来防止引起特定事件,或在将某事件传递给事件处理程序以前使用消息筛选器对其执行特殊操做。咱们必须提供IMessageFilter接口的一个实现,而后才可使用消息筛选器。如下的示范代码将演示在消息发往窗体前咱们如何拦截它。咱们拦截的一样是WM_LBUTTONDOWN消息。
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace MessageMech3
{
//实现消息过滤器接口
public class CLButtonDownFilter : IMessageFilter
{
public bool PreFilterMessage(ref Message m)
{
if (m.Msg==0x0201)// WM_LBUTTONDOWN
{
System.Windows.Forms.MessageBox.Show("App中鼠标左键按下");
//返回值为true, 表示消息已被处理,不要再日后传递,所以消息被截获
//返回值为false,表示消息未被处理,须要再日后传递,所以消息未被截获
return true;
}
return false;
}
}
/// <summary>
/// Summary description for WinForm.
/// </summary>
public class WinForm : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.Windows.Forms.Label label1;
private System.ComponentModel.Container components = null;
public WinForm()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
//安装本身的过滤器
CLButtonDownFilter MyFilter=new CLButtonDownFilter();
System.Windows.Forms.Application.AddMessageFilter(MyFilter);
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose (bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.BackColor = System.Drawing.Color.Transparent;
this.label1.Dock = System.Windows.Forms.DockStyle.Top;
this.label1.ForeColor = System.Drawing.Color.DarkViolet;
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(440, 32);
this.label1.TabIndex = 0;
this.label1.Text = "演示如何在App对象中处理消息,请点鼠标左键";
this.label1.TextAlign = System.Drawing.ContentAlignment.BottomCenter;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(7, 22);
this.BackColor = System.Drawing.Color.WhiteSmoke;
this.ClientSize = new System.Drawing.Size(440, 273);
this.Controls.AddRange(new System.Windows.Forms.Control[] {this.label1});
this.Font = new System.Drawing.Font("华文行楷", 15F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.Name = "WinForm";
this.Text = "WinForm";
//消息响应函数的调用顺序和添加委托的顺序一致
//即:如下命令将先调用Form1_MouseDown1再调用Form1_MouseDown2
//经过委托添加本身的鼠标按键消息响应函数1
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown1);
//经过委托添加本身的鼠标按键消息响应函数2
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown2);
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new WinForm()); //启动当前Form线程上的应用程序消息循环
}
//要点1
// 经过C#提供的事件接口添加本身的鼠标按键事件的响应函数
//
private void Form1_MouseDown1(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(e.Button==System.Windows.Forms.MouseButtons.Left)
System.Windows.Forms.MessageBox.Show("消息被Form1_MouseDown1函数响应");
}
private void Form1_MouseDown2(object sender, System.Windows.Forms.MouseEventArgs e)
{
if(e.Button==System.Windows.Forms.MouseButtons.Left)
System.Windows.Forms.MessageBox.Show("消息被Form1_MouseDown2函数响应");
}
//要点2
//经过覆盖基类的事件引起函数拦截消息
//
protected override void OnMouseDown( MouseEventArgs e)
{
if(e.Button==System.Windows.Forms.MouseButtons.Left)
System.Windows.Forms.MessageBox.Show("消息被OnMouseDown函数响应");
//若是须要截获消息,可将base.OnMouseDown(e);语句注释掉
base.OnMouseDown(e);
}
//要点3
//经过覆盖基类的窗体函数拦截消息
//
protected override void WndProc(ref System.Windows.Forms.Message e)
{
//若是须要截获消息,
//if(e.Msg==0x0201)// WM_LBUTTONDOWN
// System.Windows.Forms.MessageBox.Show("消息被WndProc函数响应");
//else
// base.WndProc(ref e);
//不须要截获消息则为
if(e.Msg==0x0201)// WM_LBUTTONDOWN
System.Windows.Forms.MessageBox.Show("消息被WndProc函数响应");
base.WndProc(ref e);
}
}
}
以上代码咱们首先用类CLButtonDownFilter实现了IMessageFilter接口,在WinForm初始化的时候咱们安装了消息筛选器。程序实际执行的时候,在点击鼠标左键的时候,程序仅仅会弹出一个"App中鼠标左键按下"的消息框。由于咱们在消息发往窗体前拦截了它,因此窗体将接收不到WM_LBUTTONDOWN消息。
若是咱们把
if (m.Msg==0x0201)// WM_LBUTTONDOWN
{
System.Windows.Forms.MessageBox.Show("App中鼠标左键按下");
return true;
}
改为
if (m.Msg==0x0201)// WM_LBUTTONDOWN
{
System.Windows.Forms.MessageBox.Show("App中鼠标左键按下");
return false;
}
那么,咱们在Application类处理消息后,消息将继续发往窗体。窗体的函数将能够处理此消息。程序执行效果是顺序弹出5个消息框。
1:<<App中鼠标左键按下>>
2:<<消息被WndProc函数响应>>
3:<<消息被OnMouseDown函数响应>>
4:<<消息被Form1_MouseDown1函数响应>>
5:<<消息被Form1_MouseDown2函数响应>>
其实本文中已经说的挺详细的.弹出的对话框只是为了让你更直观的看出致使的结果.
先定义没过滤时的效果.
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown1);
private void Form1_MouseDown1(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
MessageBox.Show("消息被Form1_MouseDown1函数响应");
}
主要有两种方法过滤实现过滤
第一种:
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x0201)
return;
else
base.WndProc(ref m);
}
第二种
不重写WndProc
//实现消息过滤器接口
public class CLButtonDownFilter : IMessageFilter
{
public bool PreFilterMessage(ref Message m)
{
if (m.Msg == 0x0201)// WM_LBUTTONDOWN
{
//返回值为true,表示消息已被处理,不要再日后传递,所以消息被截获
//返回值为false,表示消息未被处理,须要再日后传递,所以消息未被截获
return true;
}
return false;
}
}
CLButtonDownFilter MyFilter = new CLButtonDownFilter();
System.Windows.Forms.Application.AddMessageFilter(MyFilter);
参考出处:
http://blog.sina.com.cn/s/blog_3f39ffb50100dyee.html
http://www.cnblogs.com/mymhj/archive/2012/11/14/2770552.html
http://blog.csdn.net/yuan_hs_hf/article/details/16891585
上面的对事件的编号(就是Message.Msg属性)不清楚的,能够查看个人另一篇文章。Message类的属性Msg所关联的消息ID
==================================================================================
01. PreTranslateMessage函数,经常使用于屏蔽MFC对话框中默认的Enter和ESC消息
函数原型:BOOL PreTranslateMessage(MSG* pMsg)
用法举例:
BOOL CTestDlg::PreTranslateMessage(MSG* pMsg)
{
if(pMsg->message == WM_KEYDOWN){
if(pMsg->wParam == VK_ESCAPE){
return TRUE;
}
if(pMsg->wParam == VK_RETURN){
return TRUE; // 对话框内部控件不能够接收到回车消息!!
}
}
return CDialog::PreTranslateMessage(pMsg);
}
02.响应系统按键
if(pMsg->message==WM_SYSKEYDOWN)
{
if(pMsg->wParam==VK_MENU)
MessageBox("alt");
}
if((pMsg->wParam==VK_F9) && (GetAsyncKeyState(VK_MENU)<0))
{
MessageBox(_T("同时按下了Alt键和F9键"));
}
函数:GetAsyncKeyState()
功能:肯定用户当前是否按下了键盘上的一个键
原型:SHORT GetAsyncKeyState(int vKey);
参数:nVirtKey指出要检查键的虚键代码。结果的高位指出该键当前是否被按下(是为1,否为0)。
经常使用键的VK值:
VK_SHIFT Shift键
VK_LSHIFT 左Shift键
VK_RSHIFT 右Shift键
VK_CONTROL Ctrl键
VK_LCONTROL 左Ctrl键
VK_RCONTROL 右Ctril键
VK_MENU Alt键
VK_LMENU 左Alt键
VK_RMENU 右Alt键
VK_LBUTTON 鼠标左键
VK_RBUTTON 鼠标右键
另外一个函数GetKeyState与GetAsyncKeyState函数不一样。GetAsyncKeyState在按下某键的同时调用,判断正在按下某键。
GetKeyState则在按过某键以后再调用,它返回最近的键盘消息从线程的队列中移出时的键盘状态,判断刚按过了某键。
与RegisterHotKey()相比,GetAsyncKeyState()的优势在于能够监控鼠标按键,缺点是须要使用定时器。
==========================================================================
出处:http://blog.csdn.net/bookish_2010_prj/article/details/5873896