C# 消息发送

一、结构声明 public struct RecBoxMessage { public int RecID; } 二、API声明 -------------- public class Win32API { ///

/// 发送消息 /// /// /// /// /// /// [DllImport("user32", EntryPoint = "SendMessage")] public static extern int SendMessage(IntPtr aHwnd, int aWMsg, int aWParam, IntPtr aLParam); /// /// 发送案卷箱子消息 /// /// /// /// /// /// [DllImport("user32", EntryPoint = "SendMessage")] public static extern int SendMessage(IntPtr aHwnd, int aWMsg, int aWParam, ref RecBoxMessage aLParam); /// /// 查找指定类名或窗体名称的窗口 /// /// /// /// [DllImport("user32", EntryPoint = "FindWindow")] public static extern IntPtr FindWindow(string aLpClassName, string aLpWindowName); } -------------------------------- 三、发送消息 #region SendBoxMessage 向所有打开的箱子发送消息 /// /// 向所有打开的箱子发送消息 /// public static void SendBoxMessage(List aRecIDList) { foreach (int vRecID in aRecIDList) { SendBoxMessage(vRecID); } } /// /// 向所有打开的箱子发送消息 /// public static void SendBoxMessage(int aRecID) { //局部变量定义 RecBoxMessage vRecBoxMessage; //初始局部变量 vRecBoxMessage.RecID = aRecID; RefreshMessage.SendBoxMessage(vRecBoxMessage); } #endregion -------------------------------------- public class RefreshMessage { public static bool SendBoxMessage(RecBoxMessage aRecBoxMessage) { foreach (IntPtr vTargetWindow in RecBoxHandles) { Win32API.SendMessage(vTargetWindow, WMConsts.WM_BOXMESSGE, WMConsts.WM_BOXMESSGE, ref aRecBoxMessage); } return true; } public static List RecBoxHandles = new List(); } ------------------------- 四、添加与删除句柄 private void frmEstateWorkFlow_FormClosed(object sender, FormClosedEventArgs e) { RefreshMessage.RecBoxHandles.Remove(this.Handle); } #endregion #region 关于箱子窗体句柄 private void SetHandle() { RefreshMessage.RecBoxHandles.Add(this.Handle); } #endregion 五、接收消息 #region WndProc 通过监视系统消息,来调用过程 protected override void WndProc(ref Message aMessage) { RecBoxMessage vRecBox; switch (aMessage.Msg) { case WMConsts.WM_BOXMESSGE: { if (aMessage.WParam.ToInt32() == WMConsts.WM_BOXMESSGE) { vRecBox = (RecBoxMessage)aMessage.GetLParam(typeof(RecBoxMessage)); this.ucEstateWorkFlowGrid1.ActionRecBoxMessge(vRecBox); } break; } } //将系统消息传递自父类的WndProc base.WndProc(ref aMessage); } #endregion  

你可能感兴趣的:(C#,c#,string,user,class,api,struct)