飞道的博客

C# Winfrom程序之间通讯

463人阅读  评论(0)

目录

目录

程序1

程序2

测试

 

程序1

界面:

代码:


  
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Windows.Forms;
  4. namespace SendDemo1st
  5. {
  6. public partial class Form1 : Form
  7. {
  8. public Form1()
  9. {
  10. InitializeComponent();
  11. }
  12. public struct CopyDataStruct
  13. {
  14. public IntPtr dwData;
  15. public int cbData;
  16. [ MarshalAs(UnmanagedType.LPStr)]
  17. public string lpData;
  18. }
  19. public const int WM_COPYDATA = 0x004A;
  20. //通过窗口标题来查找窗口句柄
  21. [ DllImport("User32.dll", EntryPoint = "FindWindow")]
  22. private static extern int FindWindow(string lpClassName, string lpWindowName);
  23. //在DLL库中的发送消息函数
  24. [ DllImport("User32.dll", EntryPoint = "SendMessage")]
  25. private static extern int SendMessage
  26. (
  27. int hWnd, // 目标窗口的句柄
  28. int Msg, // 在这里是WM_COPYDATA
  29. int wParam, // 第一个消息参数
  30. ref CopyDataStruct lParam // 第二个消息参数
  31. );
  32. //发送消息
  33. private void button1_Click(object sender, EventArgs e)
  34. {
  35. string strURL = textBox1.Text;
  36. CopyDataStruct cds;
  37. cds.dwData = (IntPtr) 1; //这里可以传入一些自定义的数据,但只能是4字节整数
  38. cds.lpData = strURL; //消息字符串
  39. cds.cbData = System.Text.Encoding.Default.GetBytes(strURL).Length + 1; //注意,这里的长度是按字节来算的
  40. SendMessage(FindWindow( null, "Form2"), WM_COPYDATA, 0, ref cds); // 窗口标题
  41. }
  42. //接收消息方法
  43. protected override void WndProc(ref System.Windows.Forms.Message e)
  44. {
  45. if (e.Msg == WM_COPYDATA)
  46. {
  47. CopyDataStruct cds = (CopyDataStruct)e.GetLParam( typeof(CopyDataStruct));
  48. textBox2.Text = cds.lpData.ToString();
  49. }
  50. base.WndProc( ref e);
  51. }
  52. }
  53. }

 

程序2

界面:

代码:


  
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Windows.Forms;
  4. namespace SendDemo2nd
  5. {
  6. public partial class Form2 : Form
  7. {
  8. public Form2()
  9. {
  10. InitializeComponent();
  11. }
  12. public struct CopyDataStruct
  13. {
  14. public IntPtr dwData;
  15. public int cbData;
  16. [ MarshalAs(UnmanagedType.LPStr)]
  17. public string lpData;
  18. }
  19. public const int WM_COPYDATA = 0x004A;
  20. //通过窗口标题来查找窗口句柄
  21. [ DllImport("User32.dll", EntryPoint = "FindWindow")]
  22. private static extern int FindWindow(string lpClassName, string lpWindowName);
  23. //在DLL库中的发送消息函数
  24. [ DllImport("User32.dll", EntryPoint = "SendMessage")]
  25. private static extern int SendMessage
  26. (
  27. int hWnd, // 目标窗口的句柄
  28. int Msg, // 在这里是WM_COPYDATA
  29. int wParam, // 第一个消息参数
  30. ref CopyDataStruct lParam // 第二个消息参数
  31. );
  32. private void button1_Click(object sender, EventArgs e)
  33. {
  34. string strURL = textBox1.Text;
  35. CopyDataStruct cds;
  36. cds.dwData = (IntPtr) 1; //这里可以传入一些自定义的数据,但只能是4字节整数
  37. cds.lpData = strURL; //消息字符串
  38. cds.cbData = System.Text.Encoding.Default.GetBytes(strURL).Length + 1; //注意,这里的长度是按字节来算的
  39. SendMessage(FindWindow( null, "Form1"), WM_COPYDATA, 0, ref cds); // 窗口标题
  40. }
  41. //接收消息方法
  42. protected override void WndProc(ref System.Windows.Forms.Message e)
  43. {
  44. if (e.Msg == WM_COPYDATA)
  45. {
  46. CopyDataStruct cds = (CopyDataStruct)e.GetLParam( typeof(CopyDataStruct));
  47. textBox2.Text = cds.lpData.ToString();
  48. }
  49. base.WndProc( ref e);
  50. }
  51. }
  52. }

测试

可以看到,程序1和程序2代码没什么分别,下面测试下效果:

 

如果你这个帖子对你有用,欢迎给我点赞 + 留言,谢谢

end


转载:https://blog.csdn.net/qq_38693757/article/details/117379292
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场