110,533
社区成员
发帖
与我相关
我的任务
分享
//方法声明
[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;
}