【转】在C#中使用SendMessage
SendMessage是一个在user32.dll中声明的API函数,在C#中导入如下:
using System.Runtime.InteropServices;
[DllImport("user32.dll", EntryPoint="SendMessageA")]
public static extern int SendMessage (IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);
本文描述其参数 lParam 的用法,主要是数据类型之间的转化。
● 一种最简单的处理方式是声明多个SendMessage函数(overload),用所需的数据类型直接替换IntPtr。例如:
//声明:
[DllImport("user32.dll", EntryPoint="SendMessageA")]
private static extern int SendMessage (IntPtr hwnd, int wMsg, IntPtr wParam, string lParam);
[DllImport("user32.dll", EntryPoint="SendMessageA")]
private static extern int SendMessage (IntPtr hwnd, int wMsg, IntPtr wParam, ref Rectangle lParam);
//调用:
string s = "hello, floodzhu";
SendMessage(this.textBox1.Handle, WM_SETTEXT, IntPtr.Zero, s); Rectangle rect = new Rectangle();
SendMessage(this.richTextBox1.Handle, EM_GETRECT, (IntPtr)0, ref rect);
● 对要求返回字符串的类型(out string)可以用 StringBuilder 代替,此时不需要 out/ref。例如:
[DllImport("user32.dll", EntryPoint="SendMessageA")]
private static extern int SendMessage (IntPtr hwnd, int wMsg, int wParam, StringBuilder lParam);
private void button1_Click(object sender, System.EventArgs e)
{
const int buffer_size = 1024;
StringBuilder buffer = new StringBuilder(buffer_size);
SendMessage(this.textBox1.Handle, WM_GETTEXT, buffer_size, buffer);
//MessageBox.Show(buffer.ToString());
}
● 如果想用 InPtr 类型统一处理的话,可以借助于 Marshal 或者 GCHandle 的相关方法。例如:
[DllImport("user32.dll", EntryPoint="SendMessageA")]
private static extern int SendMessage (IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);
private void button2_Click(object sender, System.EventArgs e)
{
Rectangle rect = new Rectangle();
IntPtr buffer = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(Rectangle)));
Marshal.StructureToPtr(rect, buffer ,true);
SendMessage(this.richTextBox1.Handle, EM_GETRECT, (IntPtr)0, buffer);
rect = (Rectangle)Marshal.PtrToStructure(buffer, typeof(Rectangle));
Marshal.FreeHGlobal(buffer);
}
或者
private void button2_Click(object sender, System.EventArgs e)
{
Rectangle rect = new Rectangle();
GCHandle gch = GCHandle.Alloc(rect); SendMessage(this.richTextBox1.Handle, EM_GETRECT, (IntPtr)0, (IntPtr)gch);
rect = (Rectangle)Marshal.PtrToStructure((IntPtr)gch, typeof(Rectangle)); gch.Free();
}
【转】在C#中使用SendMessage的更多相关文章
- C#中使用SendMessage在进程间传递数据的实例
原文:C#中使用SendMessage在进程间传递数据的实例 1 新建解决方案SendMessageExample 在解决方案下面新建三个项目:CopyDataStruct,Receiver和Send ...
- C#中使用SendMessage进行进程通信的实例
原文:C#中使用SendMessage进行进程通信的实例 1 新建解决方案SendMessageSecondExample 在解决方案下面新建两个项目:Sender和Receiver,两者的输出类型均 ...
- C_中使用SendMessage
SendMessage是一个在user32.dll中声明的API函数,在C#中导入如下: using System.Runtime.InteropServices; [DllImport(" ...
- 【转】vc中使用SendMessage正确发送自定义消息的方法--不错
原文网址:http://zhoumf1214.blog.163.com/blog/static/5241940200910265532959/ 最近在用VC2008做开发,后来由于要用到消息的发送,而 ...
- C#中使用 SendMessage 向非顶端窗体发送组合键
开门见山,不废话了, 直接举例说明一下: 比如发送ALT + F 以下是 用spy++截取的消息内容 <00001> 000310DC P WM_SYSKEYDOWN nVirtKey:V ...
- vc中使用SendMessage正确发送自定义消息的方法
最近在用VC2008做开发,后来由于要用到消息的发送,而且需要自定义消息,在网上查找了很多例子,根据他们所说的,虽然大致都差不多,但是基本上没有 一个能完全做出来的.要知道VC编程有一个小地方出错,都 ...
- [winfrom]C#中使用SendMessage
在C#中,程序采用了的驱动采用了事件驱动而不是原来的消息驱动,虽然.net框架提供的事件已经十分丰富,但是在以前的系统中定义了丰富的消息对系统的编程提供了方便的实现方法,因此在C#中使用消息有时候还是 ...
- Unity3D中的SendMessage使用(消息传递的三种方法)
概述 Unity提供的消息推送机制可以非常方便我们的脚本开发,它实现的是一种伪监听者模式,利用的是反射机制. 常用函数 关于消息推送,常用的函数有三个:”SendMessage“.”SendMessa ...
- 在C#中使用 SendMessage 实现操作外部其他程序上的控件教程
一.C#代码实现 本案例使用的是c# winform .NET Framework 4.7.2 首先我们声明一个寻找窗体的函数 [DllImport("User32.dll", E ...
随机推荐
- W:Failed to fetch http://archive.ubuntukylin.com:10006/ubuntukylin/dists/pre
由于用ubuntu的时候装了几个ubuntukylin的软件(像搜狗拼音for linux),于是最近总是蹦出一个红色的三角提示,说无法更新,虽说不影响使用但是还是很不爽.解决方法记录如下: 进入系统 ...
- [WebService] the namespace on the "definitions" element, is not a valid SOAP version
公司对外通过webservice访问别人接口,对方webservice IP地址发生变化,切换过去之后,始终报错,在网上搜索了各种办法之后,暂时总结该问题几种可能解决办法,待真正解决时用的到. 异常详 ...
- 咱就入个门之NHibernate映射文件配置(一)
之前写了数据库连接配置,这次说说映射文件的配置,即表映射[ORM的核心就是此啦!]. 下面我们使用最原始的手动配置hbm.xml文件. 步骤: 1.添加People类 namespace NHiber ...
- Android学习网站推荐(转)
收集了一些比较好的Android学习网站,希望对大家有所帮助: 1.http://developer.android.com/ Android官方网站,可惜被屏蔽了,需要使用FQ软件 2.http:/ ...
- hdu 4568 Hunter(spfa预处理 + 状压dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4568 思路:首先spfa预处理出每对宝藏之间的最短距离以及宝藏到边界的最短距离,然后dp[state] ...
- animation动画兼容所有手机
.canvasAnim{ position: absolute; width:240px; height:240px; top:; z-index:; top:-20px; left:-5px; bo ...
- Arduino101学习笔记(四)—— 数字IO
1.设置管脚模式 //***************************************************************************************** ...
- 自己yy的Splay
#include <iostream> #include <cstdio> #include <queue> using namespace std; ; stru ...
- DSP using Matlab 示例Example2.2
a. n = -2:10; x = [1:7,6:-1:1]; % generate x(n) [x11,n11] = sigshift(x,n,5); [x12,n12] = sigshift(x, ...
- hdu 1520 Anniversary party 基础树dp
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...