winform窗体之间通过 windows API SendMessage函数传值
-----------------------------------------------------------‘接收窗体’代码.cs------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; namespace WindowsFormsApplication2
{
/// <summary>
/// 接收窗体
/// </summary>
public partial class ReceiveForm : Form
{
SendForm sendForm;
public ReceiveForm()
{
InitializeComponent();
sendForm = new SendForm(this.Handle);
sendForm.Show();
} public const int USER = 0x500;
/// <summary>
/// 自定义消息
/// </summary>
public const int MYMESSAGE = 0x500 + 1; /// <summary>
/// 重写窗体的消息处理函数DefWndProc,从中加入自己定义消息 MYMESSAGE 的检测的处理入口
/// </summary>
/// <param name="m"></param>
protected override void DefWndProc(ref Message m)
{
switch (m.Msg)
{
//接收自定义消息MYMESSAGE,并显示其参数
case MYMESSAGE:
SENDDATASTRUCT myData = new SENDDATASTRUCT();//这是创建自定义信息的结构
Type mytype = myData.GetType();
myData = (SENDDATASTRUCT)m.GetLParam(mytype);//这里获取的就是作为LParam参数发送来的信息的结构
textBox1.Text = myData.lpData; //显示收到的自定义信息
break;
default:
base.DefWndProc(ref m);
break;
}
}
}
}
-----------------------------------------------------------‘接收窗体’代码.Desiger.cs------------------------------------------------------------
namespace WindowsFormsApplication2
{
/// <summary>
/// 接收窗体
/// </summary>
partial class ReceiveForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null; /// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
} #region Windows Form Designer generated code /// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(30, 72);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(189, 21);
this.textBox1.TabIndex = 0;
//
// ReceiveForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(270, 174);
this.Controls.Add(this.textBox1);
this.Name = "ReceiveForm";
this.Text = "ReceiveForm";
this.ResumeLayout(false);
this.PerformLayout(); } #endregion public System.Windows.Forms.TextBox textBox1;
}
}
-----------------------------------------------------------’发送窗体‘代码.cs------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices; namespace WindowsFormsApplication2
{
/// <summary>
/// 发送窗体
/// </summary>
public partial class SendForm : Form
{
public SendForm()
{
InitializeComponent();
}
public SendForm(IntPtr Handle)
{
SendToHandle = Handle;
InitializeComponent();
}
/// <summary>
/// 接收窗口的句柄
/// </summary>
private IntPtr SendToHandle;
public const int USER = 0x500;
/// <summary>
/// 自定义的消息ID
/// </summary>
public const int MYMESSAGE = USER + 1; /// <summary>
///消息发送API
/// </summary>
/// <param name="hWnd">信息发往的窗口的句柄</param>
/// <param name="Msg">消息ID</param>
/// <param name="wParam">貌似没用</param>
/// <param name="lParam">传输的数据参数</param>
/// <returns></returns>
[DllImport("User32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessage(
IntPtr hWnd, // 信息发往的窗口的句柄
int Msg, // 消息ID
int wParam, // 参数1
ref SENDDATASTRUCT lParam // 参数2 [MarshalAs(UnmanagedType.LPTStr)]StringBuilder lParam
);
/// <summary>
/// 发送消息 调用SendMessage API
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Send_Click(object sender, EventArgs e)
{
string myText = textBox1.Text;
SENDDATASTRUCT myData = new SENDDATASTRUCT();
myData.lpData = myText;
SendMessage(SendToHandle, MYMESSAGE, 100, ref myData);//发送自定义消息给句柄为SendToHandle 的窗口,
} }
}
-----------------------------------------------------------’发送窗体‘代码.Desiger.cs------------------------------------------------------------
namespace WindowsFormsApplication2
{
/// <summary>
/// 发送窗体
/// </summary>
partial class SendForm
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null; /// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
} #region Windows 窗体设计器生成的代码 /// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Send = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// Send
//
this.Send.Location = new System.Drawing.Point(299, 69);
this.Send.Name = "Send";
this.Send.Size = new System.Drawing.Size(75, 23);
this.Send.TabIndex = 0;
this.Send.Text = "Send";
this.Send.UseVisualStyleBackColor = true;
this.Send.Click += new System.EventHandler(this.Send_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(48, 71);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(228, 21);
this.textBox1.TabIndex = 1;
//
// SendForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(415, 166);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.Send);
this.Name = "SendForm";
this.Text = "SendForm";
this.ResumeLayout(false);
this.PerformLayout(); } #endregion private System.Windows.Forms.Button Send;
private System.Windows.Forms.TextBox textBox1;
}
}
-----------------------------------------------------------’程序入口‘代码------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms; namespace WindowsFormsApplication2
{
static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new ReceiveForm());
}
}
}
------------------------------------总结----------------------------------------------------------------
1) 在发送窗体 调用API方法发送
2) 在接收窗体 重写窗体的消息处理函数DefWndProc 在m参数里面取出传过来的值
winform窗体之间通过 windows API SendMessage函数传值的更多相关文章
- c# winform 窗体之间的传参
说起winform程序中窗体之间的参数互传,大家找度娘会找到很多方法: 1.在窗体类中创建全局变量,类型为公开.静态的: 2.在窗体类中定义狗仔函数: 3.通过实践来船体参数: 这三种思路完全来自于霖 ...
- Windows API 常用函数---转载
Windows API 常用函数 2014-10-15 14:21 xiashengwang 阅读(2105) 评论(0) 编辑 收藏 .Net中虽然类库很强的,但还是有些时候功能有限,掌握 ...
- 使用IDA PRO+OllyDbg+PEview 追踪windows API 动态链接库函数的调用过程
使用IDA PRO+OllyDbg+PEview 追踪windows API 动态链接库函数的调用过程 http://blog.csdn.net/liujiayu2/article/details/5 ...
- C#使用事件方式Winform窗体之间传值
[摘自:http://www.cnblogs.com/codeToUp/p/5371062.html] 工程的源代码地址:https://github.com/yes-or-no/WinFormTra ...
- 通过委托事件实现winform窗体之间的互相刷新
新建winform窗体Form1和Form2; 接下来要通过点击Form2的按钮,实现Form1界面的同步刷新. 先展示一下最终效果: 1.Form1界面如下: 2.点击按钮弹出Form2,界面如下: ...
- 观察者模式的应用:Winform窗体之间传值
观察者模式的应用:Winform窗体传值 观察者模式的概念: 定义了对象之间的一对多依赖,这样一来,当一个对象改变状态时,它的所有依赖者都会收到通知并更新. 今天我们就学着用一下这个观察者模式,先想象 ...
- windows API普通函数跟回调函数有何区别
通俗点讲:1.普通函数(假设我们都是函数)你卖电脑,我买电脑,我给你钱(调用你)后,你给我电脑(得到返回值).这种情况下,我给钱后就不能走开,必须等你把电脑给我,否则你交货的时候可能找不到人.2.回调 ...
- Windows API 常用函数
.Net中虽然类库很强的,但还是有些时候功能有限,掌握常用的api函数,会给我们解决问题提供另一种思路,下面给出自己常用到的Api函数,以备查询. 知道api函数,但却不知道c#或VB.net该如何声 ...
- C#winform窗体如何通过windowApi的FindWindow函数获取窗体句柄
在同一个程序里,传统方式是通过this来设置当前窗体的最大化.最小化等操作, 那么怎样通过窗体句柄来设置窗体的最大化.最小化呢? 1.界面布局 通过this设置窗体最大化: name:btnWindo ...
随机推荐
- javascript设计模式之观察者模式
观察者模式又称发布/订阅模式 publish/subscribe 它是一种一对多的关系,让多个观察者对象同时监听某一主题对象,这个主题对象的状态发生变化时就会通知所有的观察者对象,使得他们能够自动 ...
- PHP MVC
学习一个框架之前,基本上我们都需要知道什么是mvc,即model-view-control,说白了就是数据控制以及页面的分离实现,mvc就 是这样应运而生的,mvc分为了三个层次,而且三个层次各司其职 ...
- Xcode中创建文件夹
如果在xcode工程中new group,只是在视觉效果上分好了几个文件夹,方便分类管理,但在finder中并不会创建新的文件夹,在硬盘目录还是所有文件都并列在一个文件夹内,更恶心的是当你重新打开工程 ...
- 简谈ashx
是一般处理程序, 是asp.net web 组件的一种,ashx是其扩展名. 实现IHttpHandler接口,接收并处理http请求.这个接口有一个IsReusable成员,一个待实现的方法Proc ...
- NOIP2016の遊記
看了cydiater的游记,我更加认识到我有多弱,大神有多强 剩余的时间不多了,NOIP前停的一周课又颓了相当多的时间,感觉NOIP真的药丸 最后一天复习模板,看一下错误,总结做题的经验,现在实力实在 ...
- IT励志与指导文章合集(链接)
乔布斯在斯坦福大学的演讲 http://www.cnblogs.com/tryingx/p/3715886.html 程序员的8个级别 http://www.cnblogs.com/tryingx/p ...
- Chapter 2: Design the user experience
Apply the user interface design for a web application 介绍了Css的常用属性和html5的新element,以及Htmlhelper的简单方法,如 ...
- mongo安全:增加用户名密码
0.简述:在非auth下创建账户,然后重启 1.以不需要用户名密码的方式启动mongodb 2.运行客户端mongo,输入以下指令 show dbs;use admin;db.createRole({ ...
- Linux java环境安装
一.jdk yum 安装方法 1.wegt http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260 ...
- 批处理定时重启print打印服务,解决打印机异常队列堆积
公司有台打印机,由于是公用的,经常出现一个较大的文档卡在队列里面,导致队列后面的打印无法被执行,人工去清理岂不是太费事了,下面分享一个批处理文件 @echo off echo 计划任务开始执行 3 e ...