发送窗口的代码
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Runtime.InteropServices; namespace WpfSendMsg { /// <summary> /// Interaction logic for Window1.xaml /// </summary> public partial class Window1 : Window { const int WM_COPYDATA = 0x004A; [DllImport("User32.dll", EntryPoint = "SendMessage")] private static extern int SendMessage( int hWnd, // handle to destination BR> int Msg, // message int wParam, // first message parameter ref COPYDATASTRUCT lParam // second message parameter ); [DllImport("User32.dll", EntryPoint = "FindWindow")] private static extern int Find(string lpClassName, string lpame); public Window1() { InitializeComponent(); this.Title = "SentWindow"; } private void btn1_Click(object sender, RoutedEventArgs e) { //int HANDLER = Findnull, @"欲发送程序窗口的标题"); int HANDLER = Find(null, @"WindowGet"); if (HANDLER != 0) { byte[] sarr = System.Text.Encoding.Default.GetBytes(tb1.Text); int len = sarr.Length; COPYDATASTRUCT cds; cds.dwData = (IntPtr)100; cds.lpData = tb1.Text; cds.cbData = len + 1; SendMessage(HANDLER, WM_COPYDATA, 0, ref cds); } } } public struct COPYDATASTRUCT { public IntPtr dwData; public int cbData; [MarshalAs(UnmanagedType.LPStr)] public string lpData; } }
如果接收窗口是wpf的窗口,代码如下:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Runtime.InteropServices; using System.Windows.Interop; namespace GetMsg { /// <summary> /// Interaction logic for Window1.xaml /// </summary> public partial class Window1 : Window { const int WM_COPYDATA = 0x004A; public Window1() { InitializeComponent(); this.Title = "WindowGet"; SourceInitialized += AppWindow_SourceInitialized; } private void AppWindow_SourceInitialized(object sender, EventArgs e) { WindowInteropHelper helper = new WindowInteropHelper(this); HwndSource hwndSource = HwndSource.FromHwnd(helper.Handle); hwndSource.AddHook(new HwndSourceHook(WndProc)); } IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled) { int retVal = 0; handled = false; switch ((int)msg) { case WM_COPYDATA: { try { COPYDATASTRUCT cp = (COPYDATASTRUCT)Marshal.PtrToStructure(lParam, typeof(COPYDATASTRUCT)); tb1.Text = cp.lpData; } catch (Exception e) { System.Diagnostics.Debug.WriteLine(e.ToString()); } handled = true; retVal = 1; break; } default: break; } return (IntPtr)retVal; } public struct COPYDATASTRUCT { public IntPtr dwData; public int cbData; [MarshalAs(UnmanagedType.LPStr)] public string lpData; } } }
如果接收窗口是winform的窗口,代码如下:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Runtime.InteropServices; namespace WpfSendMsg { /// <summary> /// Interaction logic for Window1.xaml /// </summary> public partial class Window1 : Window { const int WM_COPYDATA = 0x004A; [DllImport("User32.dll", EntryPoint = "SendMessage")] private static extern int SendMessage( int hWnd, // handle to destination BR> int Msg, // message int wParam, // first message parameter ref COPYDATASTRUCT lParam // second message parameter ); [DllImport("User32.dll", EntryPoint = "FindWindow")] private static extern int Find(string lpClassName, string lpame); public Window1() { InitializeComponent(); this.Title = "SentWindow"; } private void btn1_Click(object sender, RoutedEventArgs e) { //int HANDLER = Findnull, @"欲发送程序窗口的标题"); int HANDLER = Find(null, @"WindowGet"); if (HANDLER != 0) { byte[] sarr = System.Text.Encoding.Default.GetBytes(tb1.Text); int len = sarr.Length; COPYDATASTRUCT cds; cds.dwData = (IntPtr)100; cds.lpData = tb1.Text; cds.cbData = len + 1; SendMessage(HANDLER, WM_COPYDATA, 0, ref cds); } } } public struct COPYDATASTRUCT { public IntPtr dwData; public int cbData; [MarshalAs(UnmanagedType.LPStr)] public string lpData; } }