c#windows消息循环sendmessage实例
2023-09-11 14:16:44 时间
[DllImport("User32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessage(
IntPtr hWnd, // 信息发往的窗口的句柄
int Msg, // 消息ID
int wParam, // 参数1
string lParam
);
[DllImport("User32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessage2(
IntPtr hWnd, // 信息发往的窗口的句柄
int Msg, // 消息ID
int wParam, // 参数1
IntPtr lParam
);
[DllImport("User32.dll", EntryPoint = "FindWindow")]
private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("User32.dll", EntryPoint = "FindWindowEx")]
private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpClassName, string lpWindowName);
private void button4_Click(object sender, EventArgs e)
{
IntPtr hwnd= FindWindow(null, "testForm");
string info = "my info:ΑΒβδγζηθ";
IntPtr pinfo= Marshal.StringToHGlobalAnsi(info);
SendMessage2(hwnd, Form2.USERMESSAGEID, 12, pinfo);
SendMessage(hwnd, Form2.USERMESSAGEID, 12, info);
}
public const int USERMESSAGEID =0x2021;
protected override void WndProc(ref Message m)
{
switch(m.Msg)
{
case USERMESSAGEID:
{
int wparam= m.WParam.ToInt32();
string str = Marshal.PtrToStringAnsi(m.LParam);
break;
}
}
base.WndProc(ref m);
}
[DllImport("User32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessage(
IntPtr hWnd, // 信息发往的窗口的句柄
int Msg, // 消息ID
int wParam, // 参数1
ref My_lParam lParam
);
IntPtr ptr = FindWindow(null, "主界面");
SendMessage(ptr, MYMESSAGE, 1, ref m);//发送消息
protected override void DefWndProc(ref Message m)
{
try
{
switch (m.Msg)
{
case MYMESSAGE:
{
My_lParam ml = new My_lParam();
Type t = ml.GetType();
ml = (My_lParam)m.GetLParam(t);
break;
}
deafult :base.DefWndProc(ref m);
}
catch()
{ }
}
相关文章
- Windows下Nginx的启动、停止等命令&Nginx 配置多域名&windows下设置Nginx开机自动启动
- C# 代码重启windows服务
- C#子线程中更新ui-----c# 多线程多文件批量下载
- Asp.net(C#) windows 服务{用于实现计划任务,事件监控等}
- C#进阶系列——WebApi 接口参数不再困惑:传参详解
- windows下使用C#获取特定进程网络流量
- C#开发Windows服务的基础代码
- MFC Windows 程序设计[二十四]之windows目录展示(附源码)
- Windows之Chocolatey:windows下的apt-get即Chocolatey简介、安装、使用方法之详细攻略
- WINDOWS.H already included. MFC apps must not #include <Windows.h>
- chromium在windows上的编译 构建 Checking out and Building Chromium for Windows
- .Net C# Windows Service于server无法启动,错误 193:0xc1
- Windows命令行使用FTP——windows下自带ftp命令
- c#:Ocelot集成Consul初体验
- 【设计模式】观察者模式(C#)