C# sendmessage
时间: 2024-02-05 18:09:05 浏览: 91
以下是使用C#调用SendMessage函数的示例代码:
```csharp
using System;
using System.Runtime.InteropServices;
class Program
{
const int BM_CLICK = 0xF5;
[DllImport("user32.dll", EntryPoint = "FindWindow")]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", EntryPoint = "FindWindowEx")]
private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
static void Main(string[] args)
{
IntPtr maindHwnd = FindWindow(null, "QQ用户登录");
if (maindHwnd != IntPtr.Zero)
{
IntPtr childHwnd = FindWindowEx(maindHwnd, IntPtr.Zero, null, "登录");
if (childHwnd != IntPtr.Zero)
{
SendMessage(childHwnd, BM_CLICK, 0, 0);
}
else
{
Console.WriteLine("没有找到子窗口");
}
}
else
{
Console.WriteLine("没有找到窗口");
}
}
}
```
这段代码演示了如何使用C#调用SendMessage函数。首先,使用FindWindow函数找到QQ用户登录窗口的句柄,然后使用FindWindowEx函数找到登录按钮的句柄。最后,使用SendMessage函数发送BM_CLICK消息,模拟点击登录按钮。
阅读全文