类y (比如说是Form 窗体),类x(某实现类)
类x 在某个逻辑需要传递消息给类y,但是类x没有声明 类y的引用.当x需要传递消息给y时 y要立刻做出反应.y 里面不想有 Timer 类似的循环监听能否给个良好的架构!!!!
类x 在某个逻辑需要传递消息给类y,但是类x没有声明 类y的引用.当x需要传递消息给y时 y要立刻做出反应.y 里面不想有 Timer 类似的循环监听能否给个良好的架构!!!!
调试欢乐多
也可能使用delegate\event
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Text;namespace WindowsFormsApplication1
{
public class ClassX
{
public delegate void MessageDelegate(string strMsg);
public event MessageDelegate MessageEvent; public void Test()
{
if (MessageEvent != null)
{
MessageEvent("类X for循环开始.");
} for (int i = 0; i < 100000;i++ )
{
Application.DoEvents();
} if (MessageEvent != null)
{
MessageEvent("类X for循环结束.");
}
}
}
}Form:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private ClassX cls = new ClassX();
private void Form1_Load(object sender, EventArgs e)
{
cls.MessageEvent += MessageFunction;
} private void MessageFunction(string strMsg)
{
MessageBox.Show(strMsg);
} private void button1_Click(object sender, EventArgs e)
{
cls.Test();
}
}
}
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Text;
using System.Runtime.InteropServices;namespace WindowsFormsApplication1
{
public class ClassX
{
private int m_hWnd = 0; public ClassX(int hWnd)
{
m_hWnd = hWnd;
} private const int WM_USER = 0x0400;
public static int MY_MSG_BEGIN = WM_USER + 100;
public static int MY_MSG_END = WM_USER + 101;
[DllImport( "User32.DLL")]
public static extern int SendMessage(int hWnd, int Msg, int wParam, int lParam); public void Test()
{
SendMessage(m_hWnd,MY_MSG_BEGIN,0,0); for (int i = 0; i < 100000;i++ )
{
Application.DoEvents();
}
SendMessage(m_hWnd, MY_MSG_END, 0, 0);
}
}
}Form:using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private ClassX cls = null;
private void Form1_Load(object sender, EventArgs e)
{
cls = new ClassX(this.Handle.ToInt32());
} private void button1_Click(object sender, EventArgs e)
{
cls.Test();
} protected override void WndProc(ref Message m)
{
if(m.Msg==ClassX.MY_MSG_BEGIN)
{
MessageBox.Show("类X for循环开始.");
}
else if (m.Msg == ClassX.MY_MSG_END)
{
MessageBox.Show("类X for循环结束.");
} base.WndProc(ref m);
}
}
}
public static int MY_MSG_BEGIN = WM_USER + 100;
public static int MY_MSG_END = WM_USER + 101;
这两句是什么意思啊,那位帮忙解释下啊,谢谢
只要WndProc能截获到这个消息值就行判断
if(m.Msg==ClassX.MY_MSG_BEGIN)
{
MessageBox.Show("类X for循环开始.");
}
就可以执行到MessageBox.Show("类X for循环开始.");这句了,是吧?这样是实现了,WndProc在里面起到什么作用,为什么这样就可以呢,很迷糊哦
头脑一片混乱
/// 消息传递事件数据包含类
/// </summary>
class MessageEventArgs : System.EventArgs {/**
* 待添加:
* 定义消息的级别
* 1 : 警告
* 2 : 一般
* 3 : 严重
* 4 : 无法预料
*
* 用结构? 我想显示 级别数 和级别数代表的意义
*/private readonly string message; //只能赋值一次,代表消息的唯一性/// <summary>
/// 消息
/// </summary>
public string Message { get { return message; } }
public MessageEventArgs(string _message) { message = _message; }
}
/// <summary>
/// 消息传递
/// </summary>
class PassMessage {
/// <summary>
/// 消息传递 事件委托
/// </summary>
/// <param name="strMsg"></param>
public delegate void MessageEventHandler(object sender, MessageEventArgs e);/// <summary>
/// 消息传递 事件
/// </summary>
public event MessageEventHandler Message;/// <summary>
/// 消息传递 虚拟引发事件的保护方法
/// </summary>
protected void OnMessage(string _strMsg) {
MessageEventHandler TestHandler = Message;
if (TestHandler != null) {
TestHandler(this, new MessageEventArgs(_strMsg));
}
}/// <summary>
/// 自定义逻辑方法,如果在派生类中,这个方法直接删掉. 直接用 OnMessage("Message"); 来处理消息传递
/// </summary>
public void StarWork() { // ToDO... //......
for (int i = 0; i < 100000000; i++) { } OnMessage("For 循环结束"); //......
}
}
/**
* 调用方法:
*
* 1(初始化)
* 在Windows应用程序 可以使用 Form1_Load 事件,也可以使用 Form1 的 构造函数
* 控制台应用程序 使用 构造函数
* Web 不知道.还没接触
* private PassMessage ObjPassMessage = new PassMessage(); // 声明消息传递
* ObjPassMessage.Message += new PassMessage.MessageEventHandler(ObjPassMessage_Message); // 订阅事件
*
* 2(订阅者的处理)
* 2.1 在某位置 ObjPassMessage.StarWork();
* 2.2 void ObjPassMessage_Message(object sender, MessageEventArgs e) { MessageBox.Show(e.Message); }
*
* 不知道要怎么表达意识,大概表达下!
*/
// 代码抄袭于:http://topic.csdn.net/u/20080920/01/9e60c60d-6384-4db5-923d-13a05f984f23.html/// <summary>
/// 消息传递事件数据包含类
/// </summary>
class MessageEventArgs : System.EventArgs { /**
* 待添加:
* 定义消息的级别
* 1 : 警告
* 2 : 一般
* 3 : 严重
* 4 : 无法预料
*
* 用结构? 我想显示 级别数 和级别数代表的意义
*/ private readonly string message; //只能赋值一次,代表消息的唯一性 /// <summary>
/// 消息
/// </summary>
public string Message { get { return message; } }
public MessageEventArgs(string _message) { message = _message; }
}
/// <summary>
/// 消息传递
/// </summary>
class PassMessage {
/// <summary>
/// 消息传递 事件委托
/// </summary>
/// <param name="strMsg"></param>
public delegate void MessageEventHandler(object sender, MessageEventArgs e); /// <summary>
/// 消息传递 事件
/// </summary>
public event MessageEventHandler Message; /// <summary>
/// 消息传递 虚拟引发事件的保护方法
/// </summary>
protected void OnMessage(string _strMsg) {
MessageEventHandler TestHandler = Message;
if (TestHandler != null) {
TestHandler(this, new MessageEventArgs(_strMsg));
}
} /// <summary>
/// 自定义逻辑方法,如果在派生类中,这个方法直接删掉. 直接用 OnMessage("Message"); 来处理消息传递
/// </summary>
public void StarWork() { // ToDO... //......
for (int i = 0; i < 100000000; i++) { } OnMessage("For 循环结束"); //......
}
}
/**
* 调用方法:
*
* 1(初始化)
* 在Windows应用程序 可以使用 Form1_Load 事件,也可以使用 Form1 的 构造函数
* 控制台应用程序 使用 构造函数
* Web 不知道.还没接触
* private PassMessage ObjPassMessage = new PassMessage(); // 声明消息传递
* ObjPassMessage.Message += new PassMessage.MessageEventHandler(ObjPassMessage_Message); // 订阅事件
*
* 2(订阅者的处理)
* 2.1 在某位置 ObjPassMessage.StarWork();
* 2.2 void ObjPassMessage_Message(object sender, MessageEventArgs e) { MessageBox.Show(e.Message); }
*
* 不知道要怎么表达意识,大概表达下!
*/