C#如何在外部程序的文本框中输入文字?

hzbclx 2009-11-23 11:40:53
如题,假如我有一个学生信息管理系统的软件,其中有一个窗口是输入学生的个人信息的,包含姓名,年龄,年级等输入框。
现在我想要写一个程序去操作输入学生信息窗口的,让那些文本框自动写入信息。
...全文
1914 9 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
fengling2001 2009-11-24
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 leomaya 的回复:]
类似于钩子

请搜索"FindWindow SendMessage",或者"FindWindow PostMessage"
[/Quote]
hWnd = FindWindow(A 程序名字,NULL);
SendMessage(hWnd , WM_SETTEXT, (IntPtr)0, "姓名");
LeoMaya 2009-11-24
  • 打赏
  • 举报
回复
类似于钩子

请搜索"FindWindow SendMessage",或者"FindWindow PostMessage"
netusemaster 2009-11-24
  • 打赏
  • 举报
回复
http://topic.csdn.net/u/20091112/21/318e7060-ee1e-42bc-8fde-e2b19e45bb48.html

//方法声明
[DllImport("user32.dll")]
static extern int SendMessage(IntPtr Handle, int WParam, int LParam);

[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 Msg, IntPtr wParam, string lParam);
[DllImport("user32.dll", EntryPoint = "SetForegroundWindow", SetLastError = true)]
private static extern void SetForegroundWindow(IntPtr hwnd);

//单击事件
private void btn_msg_start_Click(object sender, EventArgs e)
{
try
{//启动旺旺客户端程序
System.Diagnostics.Process txt = System.Diagnostics.Process.Start("C:\\Program Files\\Alisoft\\WangWang\\AliIM.exe", @"d:\text_test.txt");
txt.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
//等待一秒,以便目标程序notepad.exe输入状态就绪
txt.WaitForInputIdle(1000);
//如果目标程序 notepad.exe 没有停止响应,则继续
if (txt.Responding)
{
SearchWindow();
}
}
catch { }
}




const int WM_GETTEXT = 0x000D;
const int WM_SETTEXT = 0x000C;
const int WM_CLICK = 0x00F5;


private int SearchWindow()
{
int retval = 0; //增加一个返回值用来判断操作是否成功

//下面的这些参数都可以用Spy++查到
string lpszParentClass = "StandardWindow"; //整个窗口的类名
string lpszParentWindow = "登录窗口"; //窗口标题
string lpszClass = "EditComponent"; //需要查找的子窗口的类名,也就是用户名输入框
string lppassword = "ATL:Edit";//密码输入框
string lpszClass_Submit = "StandardButton"; //需要查找的Button的类名
string lpszName_Submit = "登 录"; ///需要查找的Button的标题


IntPtr ParenthWnd = new IntPtr(0);
IntPtr EdithWnd = new IntPtr(0);

//查到窗体,得到整个窗体
ParenthWnd = FindWindow(lpszParentClass,lpszParentWindow);

//判断这个窗体是否有效
if (!ParenthWnd.Equals(IntPtr.Zero))
{
//得到User Name这个子窗体,并设置其内容
EdithWnd = FindWindowEx(ParenthWnd,EdithWnd,lpszClass,"");
if (!EdithWnd.Equals(IntPtr.Zero))
{

//调用SendMessage方法设置其内容
SendMessage(EdithWnd, WM_SETTEXT, (IntPtr)0, "sdfsdfd");
retval ++;
}
//得到Password这个子窗体,并设置其内容

EdithWnd = FindWindowEx(ParenthWnd, EdithWnd,lppassword, "");
if (!EdithWnd.Equals(IntPtr.Zero))
{

SendMessage(EdithWnd, WM_SETTEXT, (IntPtr)0, "sdfsdsdfsdasdfd");
retval ++;
}



//得到Button这个子窗体,并触发它的Click事件
EdithWnd = FindWindowEx(ParenthWnd,EdithWnd, lpszClass_Submit, lpszName_Submit);
if (!EdithWnd.Equals(IntPtr.Zero))
{
SendMessage(EdithWnd, WM_CLICK, (IntPtr)0, "0");
retval++;
}
}

return retval;
}

JackLeiMM 2009-11-24
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 leomaya 的回复:]
类似于钩子

请搜索"FindWindow SendMessage",或者"FindWindow PostMessage"
[/Quote]
应该不是钩子吧,是句柄操作吧!
每个窗口都是有句柄的,你操作句柄,向句柄赋值就可以了
liherun 2009-11-24
  • 打赏
  • 举报
回复
学习
sito_hongta 2009-11-24
  • 打赏
  • 举报
回复
1樓正解!
hzbclx 2009-11-24
  • 打赏
  • 举报
回复
灰常感谢
coolio1 2009-11-24
  • 打赏
  • 举报
回复
这是个好东西。学习一下下

111,097

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧