zoukankan
html css js c++ java
c#虚拟键盘、虚拟鼠标以及窗口查找
我想编制一个qq自动登陆程序,确发现C#中对很多api没有封装,只有使用平台调用了。其中用到窗体查找和虚拟键盘、虚拟鼠标等函数。具体代码如下:
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Text;
using
System.Windows.Forms;
using
System.Runtime.InteropServices;
namespace
SimulateMouse
{
public
partial
class
DemoForm : Form
{
[StructLayout(LayoutKind.Sequential)]
struct
NativeRECT
{
public
int
left;
public
int
top;
public
int
right;
public
int
bottom;
}
[Flags]
enum
MouseEventFlag :
uint
{
Move
=
0x0001
,
LeftDown
=
0x0002
,
LeftUp
=
0x0004
,
RightDown
=
0x0008
,
RightUp
=
0x0010
,
MiddleDown
=
0x0020
,
MiddleUp
=
0x0040
,
XDown
=
0x0080
,
XUp
=
0x0100
,
Wheel
=
0x0800
,
VirtualDesk
=
0x4000
,
Absolute
=
0x8000
}
[DllImport(
"
user32.dll
"
)]
static
extern
bool
SetCursorPos(
int
X,
int
Y);
[DllImport(
"
user32.dll
"
)]
static
extern
void
mouse_event(MouseEventFlag flags,
int
dx,
int
dy,
uint
data, UIntPtr extraInfo);
[DllImport(
"
user32.dll
"
)]
static
extern
IntPtr FindWindow(
string
strClass,
string
strWindow);
[DllImport(
"
user32.dll
"
)]
static
extern
IntPtr FindWindowEx(HandleRef hwndParent, HandleRef hwndChildAfter,
string
strClass,
string
strWindow);
[DllImport(
"
user32.dll
"
)]
static
extern
bool
GetWindowRect(HandleRef hwnd,
out
NativeRECT rect);
[DllImport(
"
user32.dll
"
)]
static
extern
void
keybd_event(
byte
bVk,
byte
bScan,
uint
dwFlags,
uint
dwExtraInfo);
private
Point endPosition;
public
DemoForm()
{
InitializeComponent();
}
private
void
button1_Click(
object
sender, EventArgs e)
{
NativeRECT rect;
IntPtr ptrTaskbar
=
FindWindow(
"
#32770
"
,
"
QQ用户登录
"
);
if
(ptrTaskbar
==
IntPtr.Zero)
{
MessageBox.Show(
"
No taskbar found.
"
);
return
;
}
IntPtr ptrStartBtn
=
FindWindowEx(
new
HandleRef(
this
, ptrTaskbar),
new
HandleRef(
this
, IntPtr.Zero),
"
ComboBox
"
,
null
);
if
(ptrStartBtn
==
IntPtr.Zero)
{
MessageBox.Show(
"
No qq号码框 found.
"
);
return
;
}
GetWindowRect(
new
HandleRef(
this
, ptrStartBtn),
out
rect);
endPosition.X
=
(rect.left
+
rect.right)
/
2
;
endPosition.Y
=
(rect.top
+
rect.bottom)
/
2
;
SetCursorPos(endPosition.X, endPosition.Y);
mouse_event(MouseEventFlag.LeftDown,
0
,
0
,
0
, UIntPtr.Zero);
mouse_event(MouseEventFlag.LeftUp,
0
,
0
,
0
, UIntPtr.Zero);
mouse_event(MouseEventFlag.LeftDown,
0
,
0
,
0
, UIntPtr.Zero);
mouse_event(MouseEventFlag.LeftUp,
0
,
0
,
0
, UIntPtr.Zero);
System.IO.StreamWriter tw
=
new
System.IO.StreamWriter(
@"
d:/aa.txt
"
);
tw.Write(
"
49593533
"
);
tw.Close();
System.IO.StreamReader tr
=
new
System.IO.StreamReader(
@"
d:/aa.txt
"
);
String mystr
=
tr.ReadToEnd();
tr.Close();
for
(
int
i
=
0
;i
<
mystr.Length;i
++
)
keybd_event((
byte
)mystr[i],
0
,
0
,
0
);
IntPtr ptrPassWord
=
FindWindowEx(
new
HandleRef(
this
, ptrTaskbar),
new
HandleRef(
this
, IntPtr.Zero),
"
#32770
"
,
null
);
GetWindowRect(
new
HandleRef(
this
, ptrPassWord),
out
rect);
endPosition.X
=
(rect.left
+
rect.right)
/
2
;
endPosition.Y
=
(rect.top
+
rect.bottom)
/
2
;
SetCursorPos(endPosition.X, endPosition.Y);
mouse_event(MouseEventFlag.LeftDown,
0
,
0
,
0
, UIntPtr.Zero);
mouse_event(MouseEventFlag.LeftUp,
0
,
0
,
0
, UIntPtr.Zero);
mouse_event(MouseEventFlag.LeftDown,
0
,
0
,
0
, UIntPtr.Zero);
mouse_event(MouseEventFlag.LeftUp,
0
,
0
,
0
, UIntPtr.Zero);
mystr
=
"
mypassword
"
;
for
(
int
i
=
0
; i
<
mystr.Length; i
++
)
{
keybd_event((
byte
)mystr[i],
0
,
0
,
0
);
}
}
}
}
说明:(
1
)所有虚拟键盘码可以到http:
//
msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/WinUI/WindowsUserInterface/UserInput/VirtualKeyCodes.asp去查找。
(
2
)首先介绍一下Keybd_event函数。Keybd_event能触发一个按键事件,也就是说回产生一个WM_KEYDOWN或WM_KEYUP消息。当然也可以用产生这两个消息来模拟按键,但是没有直接用这个函数方便。Keybd_event共有四个参数,第一个为按键的虚拟键值,如回车键为vk_return, tab键为vk_tab。第二个参数为扫描码,一般不用设置,用0代替就行第三个参数为选项标志,如果为keydown则置0即可,如果为keyup则设成“KEYEVENTF_KEYUP”或者2,第四个参数一般也是置0即可。
keybd_event(VK_CONTROL, (BYTE)
0
,
0
,
0
);
keybd_event(
'
A
'
,(BYTE)
0
,
0
,
0
);
//
此处可以用 'A', (BYTE)65, 用'a'不起作用.
keybd_event(
'
A
'
, (BYTE)
0
, KEYEVENTF_KEYUP,
0
);
keybd_event(VK_CONTROL, (BYTE)
0
, KEYEVENTF_KEYUP,
0
);
上面的代码表示 ctl
+
a
查看全文
相关阅读:
centos 6,7 上cgroup资源限制使用举例
redis sentinel哨兵的使用
redis发布-订阅
Golang cpu的使用设置--GOMAXPROCS
Golang 端口复用测试
Golang client绑定本地IP和端口
Go并发控制--context的使用
Go 并发控制--WaitGroup的使用
go thrift报错问题--WriteStructEnd
secureCRT上传本地文件到虚拟机
原文地址:https://www.cnblogs.com/kokoliu/p/712193.html
最新文章
[bzoj3611][Heoi2014]大工程
[bzoj1017]: [JSOI2008]魔兽地图DotR
[bzoj1049][HAOI2006]数字序列
[bzoj1806] [ioi2007]Miners 矿工配餐
[bzoj1031][JSOI2007]字符加密Cipher
[bzoj1030][JSOI2007]文本生成器
[bzoj1150] [CTSC2007]数据备份Backup
[bzoj2288][POJ Challenge]生日礼物
[bzoj3203][Sdoi2013]保护出题人
[bzoj3295] [Cqoi2011]动态逆序对
热门文章
MongoDB数据库GroupBy查询使用Spring-data-mongondb的实现
Echarts的相关问题记录与应用
AngularJS引入Echarts的Demo
用SpringMvc实现Excel导出功能
JAVA多线程(二)
Sqlsever
JAVA多线程(一)
Nginx的配置文件
JAVA内嵌数据库H2的使用入门
程序员找工作的几点建议
Copyright © 2011-2022 走看看