⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 java基础方面的一些实例代码
💻 CS
字号:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;

namespace WM_COPYDATAs
{
	/// <summary>
	/// Form1 的摘要说明。
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.Button button1;

		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;
		private System.Windows.Forms.Label label1;
		const int WM_COPYDATA = 0x004A;

//		SendMessage(hwnd,WM_COPYDATA,wParam,lParam); 
//		其中,WM_COPYDATA对应的十六进制数为0x004A
//	    wParam设置为包含数据的窗口的句柄。lParam指向一个COPYDATASTRUCT的结构:
//		typedef struct tagCOPYDATASTRUCT
//				{
//					DWORD dwData;//用户定义数据
//					DWORD cbData;//数据大小
//					PVOID lpData;//指向数据的指针
//				}COPYDATASTRUCT;
//		该结构用来定义用户数据。


		public Form1()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();

			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows 窗体设计器生成的代码
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.button1 = new System.Windows.Forms.Button();
			this.label1 = new System.Windows.Forms.Label();
			this.SuspendLayout();
			// 
			// textBox1
			// 
			this.textBox1.Location = new System.Drawing.Point(16, 56);
			this.textBox1.Name = "textBox1";
			this.textBox1.Size = new System.Drawing.Size(304, 21);
			this.textBox1.TabIndex = 0;
			this.textBox1.Text = "";
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(128, 88);
			this.button1.Name = "button1";
			this.button1.Size = new System.Drawing.Size(80, 32);
			this.button1.TabIndex = 1;
			this.button1.Text = "发送";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(112, 24);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(120, 23);
			this.label1.TabIndex = 2;
			this.label1.Text = "输入所要发送的信息";
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(336, 134);
			this.Controls.Add(this.label1);
			this.Controls.Add(this.button1);
			this.Controls.Add(this.textBox1);
			this.Name = "Form1";
			this.Text = "发送方窗体";
			this.ResumeLayout(false);

		}
		#endregion

		/// <summary>
		/// 应用程序的主入口点。
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		[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
			);
		//使用 extern 修饰符意味着方法在 C# 代码的外部实现

		[DllImport("User32.dll",EntryPoint="FindWindow")]
		private static extern int FindWindow(string lpClassName,string
			lpWindowName);

		private void button1_Click(object sender, System.EventArgs e)
		{
			//在发送方,用FindWindow找到接受方的句柄,然后向接受方发送WM_COPYDATA消息.
			//接受方在DefWndProc事件中,来处理这条消息.
			int WINDOW_HANDLER = FindWindow(null,@"接收方窗体");
			if(WINDOW_HANDLER == 0)
			{
			}
			else
			{
				byte[] sarr = System.Text.Encoding.Default.GetBytes(this.textBox1.Text);
				int len = sarr.Length;
				COPYDATASTRUCT cds;
				cds.dwData = (IntPtr) 100;
				cds.lpData = this.textBox1.Text;
				cds.cbData = len + 1;
				SendMessage(WINDOW_HANDLER, WM_COPYDATA, 0, ref cds);

			}
		
		}
	}
	public struct COPYDATASTRUCT
	{
		public IntPtr dwData;
		public int cbData;
		[MarshalAs(UnmanagedType.LPStr)] public string lpData;
		//UnmanagedType,指定如何将参数或字段封送到非托管代码。
		//LPStr,单字节、空终止的 ANSI 字符串。可在 System.String 或 System.Text.StringBuilder 数据类型上使用此成员。
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -