为什么第二个函数不被执行
程序很简单 就是打开 然后登陆
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
namespace ConsoleApplication2
{
class Program
{
const int WM_SETTEXT = 0x000C;
const int WM_LBUTTONDOWN = 0x0201;
const int WM_LBUTTONUP = 0x0202;
const int WM_CLOSE = 0x0010;
public static string user = "账号";
public static string pass = "123456";
[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("User32.dll", EntryPoint = "SendMessage")]
private static extern int SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, string lParam);
[DllImport("User32.dll ")]
public static extern IntPtr FindWindowEx(IntPtr parent, IntPtr childe, string strclass, string FrmText);
static void Main(string[] args)
{
open();
login();
}
private static void open()
{
Process pr = new Process();
pr.StartInfo.FileName = @"D:\Program Files\11game\11Game.exe";
pr.Start();
}
private static void login()
{
IntPtr hwnd = FindWindow("#32770", "11对战平台 - 1.0.4.3");
IntPtr htextbox = FindWindowEx(hwnd, IntPtr.Zero, "ComboBox", null);
IntPtr htextbox2 = FindWindowEx(hwnd, htextbox, "EDIT", null);//填上次获得的句柄,可以得到下一个的句柄
IntPtr hbutton = FindWindowEx(hwnd, IntPtr.Zero, "BUTTON", "登 录");
SendMessage(htextbox, WM_SETTEXT, IntPtr.Zero, user);
SendMessage(htextbox2, WM_SETTEXT, IntPtr.Zero, pass);
SendMessage(hbutton, WM_LBUTTONDOWN, IntPtr.Zero, null);
SendMessage(hbutton, WM_LBUTTONUP, IntPtr.Zero, null);
}
}
}
我想知道为什么我在login处下个断点 然后调试执行可以成功 直接运行程序就登陆不了了呢
[解决办法]
pr.Start();后面加
pr.WaitForExit();试试
[解决办法]
我想应该 信息不对 一般应该是 click 吧,你这个是 down + up
[解决办法]
因为11对战平台的启动需要时间的,而你在刚已启动后就马上找他的窗体,那么肯定找不到,你把
login函数中的第一行改为
while(true)
{
IntPtr hwnd = FindWindow("#32770", "11对战平台 - 1.0.4.3");
if(hwnd!=null)
break;
Thread.Sleep(100);
}
[解决办法]
FindWindow之前等待一下,找不到的话就循环等待再找几次。这个不是马上就能找到的。