public partial class Form1 : Form
{//设置文本内容的消息
private const int WM_SETTEXT = 0x000C;
//鼠标点击消息
const int BM_CLICK = 0x00F5;[DllImport("user32.dll")] private static extern IntPtr FindWindow( string lpClassName, string lpWindowName);[DllImport("User32.dll")]private static extern IntPtr FindWindowEx( IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass,string lpszWindows);[DllImport("User32.dll")]private static extern Int32 SendMessage( IntPtr hWnd, int Msg, IntPtr wParam, StringBuilder lParam);[DllImport("user32.dll ", CharSet = CharSet.Unicode)]
public static extern IntPtr PostMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);pri}vatevoid button1_Click(object sender, EventArgs e)
{ // 返回写字板主窗口句柄 IntPtr hWnd = FindWindow("Notepad", "Untitled - Notepad"); if (!hWnd.Equals(IntPtr.Zero)) { //返回写字板编辑窗口句柄 IntPtr edithWnd = FindWindowEx(hWnd, IntPtr.Zero, "Edit", null); if (!edithWnd.Equals(IntPtr.Zero)) // 发送WM_SETTEXT 消息: "Hello World!" SendMessage(edithWnd, WM_SETTEXT, IntPtr.Zero, new StringBuilder("Hello World!")); }}}
另:
Message msg = Message.Create(hwnd_button, BM_CLICK, new IntPtr(0), new IntPtr(0));
//点击hwnd_button句柄对应的按钮 PostMessage(msg.HWnd, msg.Msg, msg.WParam, msg.LParam);