WinForm完成跨過程通訊的辦法。本站提示廣大學習愛好者:(WinForm完成跨過程通訊的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是WinForm完成跨過程通訊的辦法正文
本文實例展現了WinForm完成跨過程通訊的辦法,分享給年夜家供年夜家參考之用。詳細辦法以下:
重要功效代碼以下:
public class WinMessageHelper { private struct COPYDATASTRUCT { public IntPtr dwData; public int cbData; [MarshalAs(UnmanagedType.LPStr)] public string lpData; } //應用COPYDATA停止跨過程通訊 public const int WM_COPYDATA = 0x004A; [DllImport("User32.dll", EntryPoint = "SendMessage")] private static extern int SendMessage( int hWnd, // handle to destination window int Msg, // message int wParam, // first message parameter ref COPYDATASTRUCT lParam // second message parameter ); [DllImport("User32.dll", EntryPoint = "FindWindow")] private static extern int FindWindow(string lpClassName, string lpWindowName); /// <summary> /// 發送新聞 /// </summary> /// <param name="windowReceiveTitle">吸收方窗體題目稱號</param> /// <param name="strData">要發送的數據</param> public static void Send(string windowReceiveTitle, string strData) { int winHandler = FindWindow(null,windowReceiveTitle); if (winHandler != 0) { byte[] sarr = System.Text.Encoding.Default.GetBytes(strData); int len = sarr.Length + 1; COPYDATASTRUCT cds; cds.dwData = (IntPtr)100; cds.lpData = strData; cds.cbData = len; SendMessage(winHandler, WM_COPYDATA, 0, ref cds); } } /// <summary> /// 吸收新聞 /// </summary> /// <example> /// 在窗體中籠罩吸收新聞函數 /// protected override void DefWndProc(ref System.Windows.Forms.Message m) /// { /// switch(m.Msg) /// { /// case WinMessageHelper.WM_COPYDATA: /// string str = WinMessageHelper.Receive(ref m); /// break; /// default: /// base.DefWndProc(ref m); /// break; /// /// } /// } /// </example> /// <returns>吸收的到數據</returns> public static string Receive(ref System.Windows.Forms.Message m) { COPYDATASTRUCT cds = new COPYDATASTRUCT(); Type cdsType = cds.GetType(); cds = (COPYDATASTRUCT)m.GetLParam(cdsType); return cds.lpData; } }
願望本文所述實例對年夜家C#法式設計有所贊助。