`
az7772010
  • 浏览: 205672 次
  • 性别: Icon_minigender_1
  • 来自: 天津
社区版块
存档分类
最新评论

windows消息C#中使用SendMessage

 
阅读更多
备注:主要描述在调用API函数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# 进程间通信 Windows消息通讯,SendMessage

    Windows进程之间是相互独立的,通过Windows消息机制,我们可以在进程之间进行通信,适合一台电脑的windows平台下进行消息交换,该例是两个winform之间通过windows消息发送和接收数据。 1、进程间通信 2、SendMessage...

    在C#中SendMessage和PostMessage的参数传递

    在C#中可以使用Window API提供的SendMessage和PostMessage来传递参数。两者的区别简单介绍下:返回值的不同,我们先看一下 MSDN 里的声明: LRESULT SendMessage( HWND hWnd, UINT Msg, WPARAM wParam, LPARAM ...

    C#中使用SendMessage

    在C#中,程序采用了的驱动采用了事件驱动而不是原来的消息驱动,虽然.net框架提供的事件已经十分丰富,但是在以前的系统中定义了丰富的消息对系统的编程提供了方便的实现方法,因此在C#中使用消息有时候还是大大提高...

    c# 在WebBrowser中用SendMessage模拟鼠标点击

    代码如下:using System; using System.... using System.Windows.Forms; using System.Runtime.InteropServices; namespace BrowserMouseClick { public partial class Form1 : Form { [DllImport(“user32.dl

    另外一个通过Windows API函数SendMessage发送消息的范例,这个范例改变文本框的只读属性(2KB)...

    另外一个通过Windows API函数SendMessage发送消息的范例,这个范例改变文本框的只读属性(2KB)

    C# 进程间通信(WindowsFormsApplication、WpfApplication相互之间)

    简单使用两种方法实现WindowsFormsApplication与WindowsFormsApplication之间,WpfApplication与WpfApplication之间,WpfApplication与WindowsFormsApplication之间的进程通信。

    c#实现HidUsb设备通信

    赋所有源代码,开发工具vs2010 framework3.5 baidu搜索c# HidUsb都是大同小异案例,而且拿下来基本不能用。...Boolean sbool = Device.SendMessage(cmdMsg); //发送数据 //第六步:释放所有资源 Device.Dispose();

    SendMessageDemo

    C#通过FindWindow获取进程的句柄,并通过SendMessage发送CopyData消息,并含有接受消息的方法

    C# 自定义窗体的最大化、最小化和关闭按钮

    在窗体的类中声明两个变量 private Point mouseOffset; //记录鼠标指针的坐标 private bool isMouseDown = false; //记录鼠标按键是否按下 创建该窗体 MouseDown、MouseMove、MouseUp事件的相应处理程序 private ...

    C#浏览器编程,学习使用

    在“新建项目”对话框的项目类型窗口中选中“Visual C#”作为项目开发语言,在模板窗口中选中“Windows 应用程序”作为项目开发模板,在“名称”、“位置”编辑框中输入自己设定的项目名字和项目存储位置,本讲义...

    wm_commands:流行的Windows程序的Wm_Command SendMessage常量

    WM_Command SendMessage常量,用于流行的应用程序通过从EXE和DLL文件中提取菜单资源,您通常可以发现常量,以通过语言自己自动化这些功能。 .rc文件是文本,类似于以下内容(来自“进程监视器”): CONTEXT_...

    C# WinForm控件美化之ImageComboBox

    这篇文章中我们重点需要实现的是(3)、(4)两项功能,下面我们来介绍具体实现的方法。 第一步,实现ImageComboBoxItem类。 要实现显示图标,当然要给每个项添加与图标相关的信息了,ImageComboBoxItem类应该包括...

    C#与HidUsb通信

    baidu搜索c# HidUsb都是大同小异案例,而且拿下来基本不能用。大都是围绕public static extern int CreateFile(省略众多...Boolean sbool = Device.SendMessage(cmdMsg); //发送数据 Device.Dispose(); //释放所有资源

    C#与HidUsb设备通信

    baidu搜索c# HidUsb都是大同小异案例,而且拿下来基本不能用。大都是围绕public static extern int CreateFile(省略众多...Boolean sbool = Device.SendMessage(cmdMsg); //发送数据 Device.Dispose(); //释放所有资源

    C#写gps中心服务处理程序

    } <br> #region Windows 窗体设计器生成的代码 /// <summary><br> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary><br> private void Initialize...

    SysListView32通用表格控件内容读取程序(64位版本)

    多年前上传过这个软件的第一个32位版的,到现在64位操作系统占了主流发现之前的32位版本很多如Windows的服务列表读取不了(最初写这个软件就是为了读取Windows服务列表),所以写了这个64位版本的。没有什么别的改进...

    系统全局范围内一键关进程(C#)

    这是一个小程序,综合了几个API函数的使用,这些小功能非常适合新手们学习。

    WindowsForm实现TextBox占位符Placeholder提示功能

    在WinForm程序中,实现TextBox文本输入框占位符的方式也很多,最常用的是方式基于Windows Api SendMessage函数发送EM_SETCUEBANNER消息,或者通过TextBox自带的焦点事件处理。 SendMessage函数实现 创建一个继承...

    C#实现基于加减按钮形式控制系统音量及静音的方法

    本文实例讲述了C#实现基于加减按钮形式控制系统音量及静音的方法。分享给大家供大家参考。具体如下: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using ...

Global site tag (gtag.js) - Google Analytics