您的位置:首页 > 编程语言 > C#

C#向win32程序窗口中的文本框设置指定文本

2011-12-04 20:09 615 查看
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}vate

void 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);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐