using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading; using System.Windows.Forms; namespace QuickMsg { /// /// 消息服务器 /// public class CustomMsg { readonly IntPtr handle; /// /// 消息事件 /// /// /// public delegate void MsgEventHandler(object sender, MsgArgs e); /// /// 大小改变事件 /// /// /// /// public delegate void SizeEventHandler(object sender, int Width,int Height); /// /// 即将关闭事件 /// /// /// public delegate void ClosingEventHandler(object sender, bool cancel); private delegate IntPtr WndProcDelegate(IntPtr hwnd, int Msg, IntPtr wParam, IntPtr lParam); private readonly WndProcDelegate wndProcDelegate; [DllImport("User32.dll", CharSet= CharSet.Auto, SetLastError= true)] private static extern IntPtr SetWindowLong(IntPtr hWnd, int nIndex, WndProcDelegate wndProcDelegate); private readonly IntPtr oldWndFunc; /// /// 当收到消息时激发 /// public event MsgEventHandler OnMsg; /// /// 消息处理类 /// /// public CustomMsg(Form _form) { handle = _form.Handle; wndProcDelegate = new WndProcDelegate(WndProc); oldWndFunc = SetWindowLong(handle, MyDb.RyWin32.GWL_WNDPROC, wndProcDelegate); //System.Windows.Forms.Application.AddMessageFilter(this); } /// /// /// ~CustomMsg() { MyDb.RyWin32.SetWindowLong(handle, MyDb.RyWin32.GWL_WNDPROC, oldWndFunc); //System.Windows.Forms.Application.RemoveMessageFilter(this); } /// /// /// /// protected virtual void WndProc(ref System.Windows.Forms.Message msg) { DefWndProc(ref msg); } /// /// /// /// public void DefWndProc(ref System.Windows.Forms.Message m) { m.Result = MyDb.RyWin32.CallWindowProc(oldWndFunc, m.HWnd, m.Msg, m.WParam, m.LParam); if (m.Msg == MyDb.RyWin32.WM_COPYDATA) { var msg = MyDb.RyWin32.GetMsg(m, out IntPtr handle); MsgArgs msg_args2 = new MsgArgs { From_handle = handle, WParam = m.WParam.ToInt32(), Msg_text = msg }; Thread th = new Thread(delegate(){ Start(msg_args2); }); th.Start(); void Start(MsgArgs msg_args) { OnMsg?.Invoke(this, msg_args); if (msg_args.Reply_Text != "") { Send(handle, msg_args.WParam, msg_args.Reply_Text); } } } } private IntPtr WndProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam) { System.Windows.Forms.Message m = System.Windows.Forms.Message.Create(hWnd, msg, wParam, lParam); WndProc(ref m); return m.Result; } /// /// 发送消息,采用单线程发送 /// /// 要发送到的句柄 /// 编号 /// 消息内容 public int Send2(IntPtr to_handle,int wParam, string msg) { return MyDb.RyWin32.SendMsg(handle, to_handle, wParam, msg); } /// /// 发送消息,采用多线程发送 /// /// 要发送到的句柄 /// 编号 /// 消息内容 public void Send(IntPtr to_handle, int wParam, string msg) { Thread th = new Thread(delegate () { MyDb.RyWin32.SendMsg(handle, to_handle, wParam, msg); }); th.Start(); } } /// /// 消息类 /// public class MsgArgs { /// /// 是否取消 /// public bool Cancel { get; internal set; } = false; /// /// 来源句柄 /// public IntPtr From_handle { get; internal set; } = IntPtr.Zero; /// /// /// public int WParam {get; set;} = 0; /// /// 消息文本 /// public string Msg_text { get; internal set; } = ""; /// /// 需要回复的内容 /// public string Reply_Text { get; set; } = ""; } }