×

suspendlayout

C#中什么是消息,解释越详细越多越好?如何让C#程序发声

admin admin 发表于2022-05-16 06:56:34 浏览125 评论0

抢沙发发表评论

C#中什么是消息,解释越详细越多越好


消息就是通知和命令。
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#的消息封装原理。
实现鼠标左键按下的消息的响应(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_MouseDown1、Form1_MouseDown2在我们单击鼠标左键的时候都会被调用,而且调用的顺序和我们添加委托的顺序一致。
WM_LBUTTONDOWN消息首先被Application类从应用程序消息队列中取出,然后分发到相应的窗体。窗体使用MouseDown事件中的函数指针调用已经添加的响应函数。所以C#中的事件字段实质上是一个函数指针列表,用来维护一些消息到达时的响应函数的地址。
三、结论
C#中消息的工作流程:
C#中的消息被Application类从应用程序消息队列中取出,然后分发到消息对应的窗体,窗体对象的第一个响应函数是对象中的protected override void WndProc(ref System.Windows.Forms.Message e)方法。
它再根据消息的类型调用默认的消息响应函数(如OnMouseDown),默认的响应函数然后根据对象的事件字段(如this.MouseDown )中的函数指针列表,调用用户所加入的响应函数(如Form1_MouseDown1和Form1_MouseDown2),而且调用顺序和用户添加顺序一致。
四、再回首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);

如何让C#程序发声


不知道就这一个行不行,如果不行的话,可以给我发邮件到plmmmmlq@163.com
我把整个解决方案都给你

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Management;
using Microsoft.Win32;

namespace XKTSys
{
/// 《summary》
/// frmSplash 的摘要说明。
/// 《/summary》
public class frmSplash : System.Windows.Forms.Form
{

public const string APP_TITLE=“习考通“;
private System.Windows.Forms.PictureBox picHelp;
private System.Windows.Forms.PictureBox picExit;
private System.Windows.Forms.PictureBox picReview;
private System.Windows.Forms.PictureBox picTest;
private System.Windows.Forms.PictureBox picExercise;
private string strDBPath=““;

[DllImport(“winmm.dll“)]
public static extern bool PlaySound(string pszSound,int hmod,int fdwSound);
public const int SND_FILENAME=0x00020000;
public const int SND_ASYNC=0x0001;
private frmExercise frmExercise1;
private frmTest frmTest1;
private frmReview frmReview1;

private System.ComponentModel.IContainer components=null;

public frmSplash()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
this.strDBPath=Environment.CurrentDirectory;
this.Text=APP_TITLE;
}
/// 《summary》
/// 应用程序的主入口点。
/// 《/summary》
[STAThread]
static void Main()
{
Application.Run(new frmSplash());
}

/// 《summary》
/// 清理所有正在使用的资源。
/// 《/summary》
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// 《summary》
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// 《/summary》
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(frmSplash));
this.picExercise = new System.Windows.Forms.PictureBox();
this.picReview = new System.Windows.Forms.PictureBox();
this.picTest = new System.Windows.Forms.PictureBox();
this.picExit = new System.Windows.Forms.PictureBox();
this.picHelp = new System.Windows.Forms.PictureBox();
this.SuspendLayout();
//
// picExercise
//
this.picExercise.Cursor = System.Windows.Forms.Cursors.Hand;
this.picExercise.Image = ((System.Drawing.Image)(resources.GetObject(“picExercise.Image“)));
this.picExercise.Location = new System.Drawing.Point(386, 324);
this.picExercise.Name = “picExercise“;
this.picExercise.Size = new System.Drawing.Size(134, 30);
this.picExercise.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.picExercise.TabIndex = 7;
this.picExercise.TabStop = false;
this.picExercise.Click += new System.EventHandler(this.picExercise_Click);
this.picExercise.MouseEnter += new System.EventHandler(this.picExercise_MouseEnter);
this.picExercise.MouseLeave += new System.EventHandler(this.picExercise_MouseLeave);
//
// picReview
//
this.picReview.Cursor = System.Windows.Forms.Cursors.Hand;
this.picReview.Image = ((System.Drawing.Image)(resources.GetObject(“picReview.Image“)));
this.picReview.Location = new System.Drawing.Point(225, 216);
this.picReview.Name = “picReview“;
this.picReview.Size = new System.Drawing.Size(134, 30);
this.picReview.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.picReview.TabIndex = 8;
this.picReview.TabStop = false;
this.picReview.Click += new System.EventHandler(this.picReview_Click);
this.picReview.MouseEnter += new System.EventHandler(this.picReview_MouseEnter);
this.picReview.MouseLeave += new System.EventHandler(this.picReview_MouseLeave);
//
// picTest
//
this.picTest.Cursor = System.Windows.Forms.Cursors.Hand;
this.picTest.Image = ((System.Drawing.Image)(resources.GetObject(“picTest.Image“)));
this.picTest.Location = new System.Drawing.Point(308, 269);
this.picTest.Name = “picTest“;
this.picTest.Size = new System.Drawing.Size(134, 30);
this.picTest.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.picTest.TabIndex = 9;
this.picTest.TabStop = false;
this.picTest.Click += new System.EventHandler(this.picTest_Click);
this.picTest.MouseEnter += new System.EventHandler(this.picTest_MouseEnter);
this.picTest.MouseLeave += new System.EventHandler(this.picTest_MouseLeave);
//
// picExit
//
this.picExit.Cursor = System.Windows.Forms.Cursors.Hand;
this.picExit.Image = ((System.Drawing.Image)(resources.GetObject(“picExit.Image“)));
this.picExit.Location = new System.Drawing.Point(512, 404);
this.picExit.Name = “picExit“;
this.picExit.Size = new System.Drawing.Size(78, 30);
this.picExit.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.picExit.TabIndex = 10;
this.picExit.TabStop = false;
this.picExit.Click += new System.EventHandler(this.picExit_Click);
this.picExit.MouseEnter += new System.EventHandler(this.picExit_MouseEnter);
this.picExit.MouseLeave += new System.EventHandler(this.picExit_MouseLeave);
//
// picHelp
//
this.picHelp.Cursor = System.Windows.Forms.Cursors.Hand;
this.picHelp.Image = ((System.Drawing.Image)(resources.GetObject(“picHelp.Image“)));
this.picHelp.Location = new System.Drawing.Point(158, 164);
this.picHelp.Name = “picHelp“;
this.picHelp.Size = new System.Drawing.Size(134, 30);
this.picHelp.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.picHelp.TabIndex = 11;
this.picHelp.TabStop = false;
this.picHelp.Click += new System.EventHandler(this.picHelp_Click);
this.picHelp.MouseEnter += new System.EventHandler(this.picHelp_MouseEnter);
this.picHelp.MouseLeave += new System.EventHandler(this.picHelp_MouseLeave);
//
// frmSplash
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject(“$this.BackgroundImage“)));
this.ClientSize = new System.Drawing.Size(602, 447);
this.Controls.Add(this.picHelp);
this.Controls.Add(this.picReview);
this.Controls.Add(this.picExercise);
this.Controls.Add(this.picTest);
this.Controls.Add(this.picExit);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Icon = ((System.Drawing.Icon)(resources.GetObject(“$this.Icon“)));
this.MaximizeBox = false;
this.Name = “frmSplash“;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = “习考通 2006“;
this.ResumeLayout(false);

}
#endregion

private void frm_Closed(object sender, System.EventArgs e)
{
this.Show();
}
//重点复习
private void picReview_MouseEnter(object sender, System.EventArgs e)
{
PlaySound(“Sound\\sel.wav“,0,SND_FILENAME|SND_ASYNC);
this.picReview.Top=218;
}

private void picReview_MouseLeave(object sender, System.EventArgs e)
{
this.picReview.Top=216;
}

private void picReview_Click(object sender, System.EventArgs e)
{
this.Cursor=Cursors.WaitCursor;
frmReview1=new frmReview(this.strDBPath);
frmReview1.Closed+=new EventHandler(frm_Closed);
frmReview1.Show();
this.Hide();
this.Cursor=Cursors.Default;
}
//模拟考试
private void picTest_MouseEnter(object sender, System.EventArgs e)
{
PlaySound(“Sound\\sel.wav“,0,SND_FILENAME|SND_ASYNC);
this.picTest.Top=271;
}

private void picTest_MouseLeave(object sender, System.EventArgs e)
{
this.picTest.Top=269;
}

private void picTest_Click(object sender, System.EventArgs e)
{
this.Cursor=Cursors.WaitCursor;
frmTest1=new frmTest(this.strDBPath);
frmTest1.Closed+=new EventHandler(frm_Closed);
frmTest1.Show();
this.Hide();
this.Cursor=Cursors.Default;
}
//集中训练
private void picExercise_MouseEnter(object sender, System.EventArgs e)
{
PlaySound(“Sound\\sel.wav“,0,SND_FILENAME|SND_ASYNC);
this.picExercise.Top=326;
}

private void picExercise_MouseLeave(object sender, System.EventArgs e)
{
this.picExercise.Top=324;
}

private void picExercise_Click(object sender, System.EventArgs e)
{
this.Cursor=Cursors.WaitCursor;
frmExercise1=new frmExercise(this.strDBPath);
frmExercise1.Closed+=new EventHandler(frm_Closed);
frmExercise1.Show();
this.Hide();
this.Cursor=Cursors.Default;
}

//使用说明
private void picHelp_Click(object sender, System.EventArgs e)
{
try
{
this.Cursor=Cursors.WaitCursor;
System.Diagnostics.Process.Start(“readme.txt“);
this.Cursor=Cursors.Default;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message,APP_TITLE,MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}

private void picHelp_MouseEnter(object sender, System.EventArgs e)
{
PlaySound(“Sound\\sel.wav“,0,SND_FILENAME|SND_ASYNC);
this.picHelp.Top=166;
}

private void picHelp_MouseLeave(object sender, System.EventArgs e)
{
this.picHelp.Top=164;
}

//退出系统
private void picExit_MouseEnter(object sender, System.EventArgs e)
{
PlaySound(“Sound\\sel.wav“,0,SND_FILENAME|SND_ASYNC);
this.picExit.Top=406;
}

private void picExit_MouseLeave(object sender, System.EventArgs e)
{
this.picExit.Top=404;
}

private void picExit_Click(object sender, System.EventArgs e)
{
this.Close();
}
}
}

C#的程序执行顺序


  第一:进入static void Main()
  第二:运行static void Main() 中的Application.Run(new Form1());在这中间对窗体进行构造。
  执行private System.ComponentModel.Container components = null;作用是Container 类是IContainer 接口的默认实现。容器是封装和跟踪零个或更多个组件的对象。在此上下文中,包容是指逻辑包荣, 而不是直观包容。可以在多种方案下使用组件和容器,包括可视化方案和非可视化方案。
  在一个先入先出列表中跟踪容器中的组件,该列表也定义了容器内组件的顺序。
  所添加的组件会附加到列表的结尾。
  执行窗体构造函数public Form1()初始化窗体
  执行构造函数中的InitializeComponent(); 在这个函数中对WINDOWS控件(System.Windows.Forms.Control)和窗体初始化
  执行ResumeLayout(false);ResumeLayout:当调整控件的多个属性时,将先后使用 SuspendLayout 和ResumeLayout 方法取消多个Layout 事件。例如,通常先调用 SuspendLayout 方法,然后设置控件的 Size、Location、Anchor 或 Dock 属性,最后调用 ResumeLayout(bool) 方法以使更改生效。默认是TRUE恢复正常的布局逻辑,FALSE改变布局。
  第三:执行后显示窗体,进行程序的运行,等待事件发生,并响应事件。
  第四:关闭窗体. exit Application.Run(可能相当于WINDOWS中的消息循环机制)函数, 程序结束.
-suspendlayout