C#中使用SendMessage
发布时间:2019-01-24 编辑:jiaochengji.com
教程集为您提供C#中使用SendMessage等资源,欢迎您收藏本站,我们将为您提供最新的C#中使用SendMessage资源
在C#中,程序采用了的驱动采用了事件驱动而不是原来的消息驱动,虽然.net框架提供的事件已经十分丰富,但是在以前的系统中定义了丰富的消息对系统的编程提供了方便的实现方法,因此在C#中使用消息有时候还是大大提高编程的效率的。
1 定义消息
在c#中消息需要定义成windows系统中的原始的16进制数字,比如
const int WM_Lbutton = 0x201; //定义了鼠标的左键点击消息
public const int USER = 0x0400 // 是windows系统定义的用户消息
2 消息发送
消息发送是通过windows提供的API函数SendMessage来实现的它的原型定义为
[DllImport("User32.dll",EntryPoint="SendMessage")]
private static extern int SendMessage(
int hWnd, // handle to destination window
int Msg, // message
int wParam, // first message parameter
int lParam // second message parameter
);
3 消息的接受
在C#中,任何一个窗口都有也消息的接收处理函数,就是defproc函数
你可以在form中重载该函数来处理消息
protected override void DefWndProc ( ref System.WinForms.Message m )
{
switch(m.msg)
{
case WM_Lbutton :
///string与MFC中的CString的Format函数的使用方法有所不同
string message = string.Format("收到消息!参数为:{0},{1}",m.wParam,m.lParam);
MessageBox.Show(message);///显示一个消息框
break;
default:
base.DefWndProc(ref m);///调用基类函数处理非自定义消息。
break;
}
1 定义消息
在c#中消息需要定义成windows系统中的原始的16进制数字,比如
const int WM_Lbutton = 0x201; //定义了鼠标的左键点击消息
public const int USER = 0x0400 // 是windows系统定义的用户消息
2 消息发送
消息发送是通过windows提供的API函数SendMessage来实现的它的原型定义为
[DllImport("User32.dll",EntryPoint="SendMessage")]
private static extern int SendMessage(
int hWnd, // handle to destination window
int Msg, // message
int wParam, // first message parameter
int lParam // second message parameter
);
3 消息的接受
在C#中,任何一个窗口都有也消息的接收处理函数,就是defproc函数
你可以在form中重载该函数来处理消息
protected override void DefWndProc ( ref System.WinForms.Message m )
{
switch(m.msg)
{
case WM_Lbutton :
///string与MFC中的CString的Format函数的使用方法有所不同
string message = string.Format("收到消息!参数为:{0},{1}",m.wParam,m.lParam);
MessageBox.Show(message);///显示一个消息框
break;
default:
base.DefWndProc(ref m);///调用基类函数处理非自定义消息。
break;
}
您可能感兴趣的文章:
C#中使用SendMessage
C# Winform 整个窗口拖动的实现代码
C#实现关闭显示器的函数一例
c# Winform 全窗口拖动的代码
python中如何引入包?
php构造函数与析构函数实例详解
java实现两台服务器实现文件的复制
【Golang实现设计模式】桥接模式
JavaScript/jQuery、HTML、CSS 构建 Web IM 远程及时聊天通信程序
java中简单工厂模式、工厂方法模式、抽象工厂模式介绍
上一篇:如何有效的使用C#读取文件
下一篇:[C#]结束Excel的进程的方法
[关闭]