// Clicking Button1 causes a message box to appear.
private void Button1_Click(System.Object sender, System.EventArgs e)
{
MessageBox.Show("Click here!");
}
// Use the SendKeys.Send method to trigger the Button1 click event
// and display the message box.
private void Form1_DoubleClick(object sender, System.EventArgs e)
{ // Send the enter key; since the tab stop of Button1 is 0, this
// will trigger the click event.
SendKeys.Send("{ENTER}");
}
private void Button1_Click(System.Object sender, System.EventArgs e)
{
MessageBox.Show("Click here!");
}
// Use the SendKeys.Send method to trigger the Button1 click event
// and display the message box.
private void Form1_DoubleClick(object sender, System.EventArgs e)
{ // Send the enter key; since the tab stop of Button1 is 0, this
// will trigger the click event.
SendKeys.Send("{ENTER}");
}
还能确定
Private Declare Sub Sleep Lib "kernel32" (ByVal dwmilliseconds As Long)
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassname As String, ByVal lpwindowname As String) As Long
Private Declare Function Sendmessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wmsg As Long, ByVal wparam As Long, lparam As Any) As Long
Private Const WM_CLOSE = &H10Private Sub asdf()
Dim hwnd, wnd As Long
Shell "xiadan.exe"
Sleep 4000
DoEvents
hwnd = FindWindow(vbNullString, "用户登录 - 青岛营业部")
SendKeys "100"
Sleep 1000
DoEvents
SendKeys "{enter}"
Sleep 1000
DoEvents
SendKeys "123456"
Sleep 1000
DoEvents
SendKeys "{enter}"
Sleep 1000
DoEvents
SendKeys "123456"
Sleep 1000
DoEvents
SendKeys "{enter}"
DoEvents
Sleep 10000
DoEventswnd = FindWindow(vbNullString, "网上股票交易系统 - 青岛营业部(51440)")Call Sendmessage(hwnd, WM_CLOSE, 0, ByVal 0&)
DoEvents
Call Sendmessage(wnd, WM_CLOSE, 0, ByVal 0&)
DoEventsIf wnd = 0 Then
MsgBox "测试登录失败!", vbCritical
ElseMsgBox "测试登录 OK!", vbInformation
End If
End Sub
主窗体俺是能找到,
但对应窗体中的TEXTBOX和BUTTON如何找啊?
(只要能找到,就能发送信息,这个我已经成功了)
类似下面窗口: 姓名: _______ (需输入)
密码: _______ (需输入)
确定 退出 注册
其中焦点已在 姓名: 后的textbox栏内,回车会切换到 密码后,再回车到 确定,
哪怕是“盲输”也行:连续发送 两个 guest和回车,再加一个回车。
当然若能正确按LABEL定位就最好了。
2. SendMessage或者PostMessage来发送消息到指定Handle的窗口
3. 如果有些按键的代码不知道的话,可以用vb找一下keycode,注意了,不是ASCII码
4. 扫描某一个按键状态的话就用GetKeyboardState
实现System.Windows.Forms.IMessageFilter中的PreFilterMessage
public bool PreFilterMessage(ref Message msg)
{
CurrentMessage = msg.ToString() + "|" + msg.Msg.ToString() + "|" + msg.Result.ToString();
return false;
}
把字符串打印出来看看就知道了,或者查查MSDN Windows SDK的这一部分有的
每次用SetWindowPos指定窗口的坐标,然后模拟鼠标行为
”2. SendMessage或者PostMessage来发送消息到指定Handle的窗口“
现在我的最大困难就是不能定位窗口。若能定位,当然用SendMessage直接发送就可以了
。
还是根据我举的例子,给个源码吧,多谢关注!
1. 每次用SetWindowPos指定窗口的坐标
2. SetCursorPos模拟鼠标行为,移动到猜测的控件位置。
3. 用GetCursorPos和WindowFromPoint找到控件Handle
4. SetFocus到这个控件
或者直接用mouse_event这个API模拟鼠标的点击操作。不知道这样行吗,如果有更好的方法学习~~~~^_^
HWND hwndParent, // handle to parent window
HWND hwndChildAfter, // handle to child window
LPCTSTR lpszClass, // class name
LPCTSTR lpszWindow // window name
);用以上的方法,就不怕他的控件会挪地方了,随便在哪里都能挖出来。
还是请那位仁兄提供一个较完整代码,俺好学学。
时间太急,实在没时间一点点摸索了,
解决后另开贴送100分。
HWND GetNextWindow(HWND hWnd,UNIT wCmd);
但运行时提示 user32.dll找不到入口。
环境是 WIN2000SERVER(VS2005 BETA)和WIN2003SERVER(VS2002),都不行.
什么原因?
1、
按下面代码确定后,
如input(hwnd_textbox, "1234");本来应该是1234,但最后确定后却成了12341234,
成了两遍,不知什么原因?
2、要控制的窗体中的各控件的句柄号不固定:每次此窗体重启后,其句柄号就变了,
这样就又需手工移动鼠标来取相关控件的句柄号,失去了做这个程序的本意。说回来
还是老问题:如何用其他方法定位想要的控件的句柄?
先取窗口相关部件句柄:
MessageBox.Show("准备检查窗口句柄,请将鼠标定位在 帐户:");
Thread.Sleep(2000);
GetCursorPos(out lpoint);
hwnd_textbox = WindowFromPoint(lpoint);
this.textBox2.Text = hwnd_textbox.ToString(); 再模拟键盘输入:
private void button1_Click(object sender, System.EventArgs e)
{
IntPtr hwnd_textbox;
int mp;
if (hwnd_win == IntPtr.Zero)
{
MessageBox.Show("警告:找不到目标窗口,请先-》 取窗口句柄");
return;
}
mp = int.Parse(textBox2.Text.Trim());
hwnd_textbox = (IntPtr)mp;
//下面是三个输入窗口,分别输入:
input(hwnd_textbox, "1234");
input((IntPtr)int.Parse(textBox4.Text.Trim()),"888888");
input((IntPtr)int.Parse(textBox5.Text.Trim()),"888888");
//将焦点定到 确定 按钮
SetFocus((IntPtr)int.Parse(textBox6.Text.Trim()));
SendKeys.Send("{ENTER}");
}
//向指定控件输入信息
public void input(IntPtr mptr, string mstr)
{
Message msg;
UnicodeEncoding encode = new UnicodeEncoding();
char[] chars = encode.GetChars(encode.GetBytes(mstr));
foreach (char c in chars)
{
msg = Message.Create(mptr, WM_CHAR, new IntPtr(c), new IntPtr(0));
PostMessage(msg.HWnd, msg.Msg, msg.WParam, msg.LParam);
}
}