一、消息概述
Windows下应用程序的执行是通过消息驱动的。消息是整个应用程序的工作引擎,我们需要理解掌握编程语言是如何封装消息的原理。
1 什么是消息(Message)
消息就是通知和命令。在.NET框架类库中的System.Windows.Forms命名空间中微软采用面对对象的方式重新定义了Message。新的消息(Message)结构的公共部分属性基本与早期的一样,不过它是面对对象的。
公共属性:
HWnd 获取或设定消息的处理函数
Msg 获取或设定消息的ID号
Lparam 指定消息的LParam字段
Wparam 指定消息的WParam字段
Result 指定为响应消息处理函数而向OS系统返回的值
2 消息驱动的过程
所有的外部事件,如键盘输入、鼠标移动、按动鼠标都由OS系统转换成相应的消息发送到应用程序的消息队列。每个应用程序都有一段相应的程序代码来检索、分发这些消息到对应的窗体,然后由窗体的处理函数来处理。
二、C#中的消息的封装
C#对消息重新进行了面对对象的封装,在C#中消息被封装成了事件。
System.Windows.Forms.Application类具有用于启动和停止应用程序和线程以及处理Windows消息的方法。
调用Run以启动当前线程上的应用程序消息循环,并可以选择使其窗体可见。
调用Exit或ExitThread来停止消息循环。
C#中用Application类来处理消息的接收和发送的。消息的循环是由它负责的。
从本质上来讲,每个窗体一般都对应一个窗体过程处理函数。那么,C#的一个Form实例(相当于一个窗体)收到消息后是如何处理消息的?其实,这个问题的分析也就是展示了C#的消息封装原理。
三、C#中消息的工作流程:
1、 Application类有一个AddMessageFilter的静态方法,通过它我们可以添加消息筛选器,以便在向目标传递Windows消息时,检视这些消息。
使用消息筛选器来防止引发特定事件,或在将某事件传递给事件处理程序之前使用消息筛选器对其执行特殊操作。
我们必须提供IMessageFilter接口的一个实现,然后才可以使用消息筛选器。
2、 C#中的消息被Application类从应用程序消息队列中取出,如果有消息筛选器,先执行消息筛选, 然后分发到消息对应的窗体。
1)窗体对象的第一个响应函数是窗口过程函数,即对象中的protected override void WndProc(ref System.Windows.Forms.Message e)方法。
2)再根据消息的类型调用默认的消息响应函数(如OnMouseDown)
3)再根据用户自定义的事件处理函数,按订阅顺序分别执行(如Form1_MouseDown1)。
四、示例:
namespace WindowsApplication27
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.SuspendLayout();
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 266);
this.Name = "Form1";
this.Text = "Form1";
//这里订阅了事件
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);
this.ResumeLayout(false);
}
#endregion
}
}
using System;
using System.Collections.Generic;using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication27
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Application.AddMessageFilter((new msgFilter()));
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
MessageBox.Show("3");
}
protected override void OnMouseDown(MouseEventArgs e)
{
MessageBox.Show("2");
base.OnMouseDown(e);
}
protected override void WndProc(ref Message m)
{
//MouseDown Msg Id
if (m.Msg==0x0201)
MessageBox.Show("1");
base.WndProc(ref m);
}
}
public class msgFilter : IMessageFilter
{
public bool PreFilterMessage(ref Message m)
{
if (m.Msg == 0x0201)
MessageBox.Show("0");
return false;
}
}
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}