一、结构声明 public struct RecBoxMessage { public int RecID; } 二、API声明 -------------- public class Win32API { /// <summary> /// 发送消息 /// </summary> /// <param name="aHwnd"></param> /// <param name="aWMsg"></param> /// <param name="aWParam"></param> /// <param name="aLParam"></param> /// <returns></returns> [DllImport("user32", EntryPoint = "SendMessage")] public static extern int SendMessage(IntPtr aHwnd, int aWMsg, int aWParam, IntPtr aLParam); /// <summary> /// 发送案卷箱子消息 /// </summary> /// <param name="hwnd"></param> /// <param name="wMsg"></param> /// <param name="wParam"></param> /// <param name="lParam"></param> /// <returns></returns> [DllImport("user32", EntryPoint = "SendMessage")] public static extern int SendMessage(IntPtr aHwnd, int aWMsg, int aWParam, ref RecBoxMessage aLParam); /// <summary> /// 查找指定类名或窗体名称的窗口 /// </summary> /// <param name="lpClassName"></param> /// <param name="lpWindowName"></param> /// <returns></returns> [DllImport("user32", EntryPoint = "FindWindow")] public static extern IntPtr FindWindow(string aLpClassName, string aLpWindowName); } -------------------------------- 三、发送消息 #region SendBoxMessage 向所有打开的箱子发送消息 /// <summary> /// 向所有打开的箱子发送消息 /// </summary> public static void SendBoxMessage(List<int> aRecIDList) { foreach (int vRecID in aRecIDList) { SendBoxMessage(vRecID); } } /// <summary> /// 向所有打开的箱子发送消息 /// </summary> 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<IntPtr> RecBoxHandles = new List<IntPtr>(); } ------------------------- 四、添加与删除句柄 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