一段自动发送QQ聊天信息的代码 public void sendMessage()
{
IntPtr ip = IntPtr.Zero;
if (Regex.IsMatch(text_Intptr.Text.Trim(), @"^[a-fA-F0-9]+$"))
ip = (IntPtr)Convert.ToInt32(text_Intptr.Text.Trim(), 16);
else
{
ip = FindWindow("TXGuiFoundation", TextBox_Title.Text.Trim());
if (ip.ToInt32() == 0)
ip = GetIntPtr();
if (ip == IntPtr.Zero)
{
MessageBox.Show("请输入正确的标题");
return;
}
}
SetForegroundWindow(ip);
SetActiveWindow(ip);
var str = "";
var rtf = "";
richTextBox1.Invoke(new MethodInvoker(() => { str = richTextBox1.Text.Trim(); rtf = richTextBox1.Rtf.Trim(); }));
while (true)
{
if (ip.ToInt32() == GetForegroundWindow().ToInt32())
{
foreach (byte c in Encoding.Default.GetBytes(str))
{
SendMessage(ip.ToInt32(), 0x101, (int)c, 1);
SendMessage(ip.ToInt32(), 0x102, (int)c, 1);
}
SendKeys.SendWait("%s");
}
Thread.Sleep(sleep);
}
}我就这两段代码不明白
SendMessage(ip.ToInt32(), 0x101, (int)c, 1);
SendMessage(ip.ToInt32(), 0x102, (int)c, 1);他这里为向聊天输入框插入字符,
但是他这里判断没有先去得到句柄,而是用
0x101和0x102
那这两个是怎么得来的呢?
这两个表示的是什么呢?
为什么是两个,按道理输入框只有一个啊?谢谢
{
IntPtr ip = IntPtr.Zero;
if (Regex.IsMatch(text_Intptr.Text.Trim(), @"^[a-fA-F0-9]+$"))
ip = (IntPtr)Convert.ToInt32(text_Intptr.Text.Trim(), 16);
else
{
ip = FindWindow("TXGuiFoundation", TextBox_Title.Text.Trim());
if (ip.ToInt32() == 0)
ip = GetIntPtr();
if (ip == IntPtr.Zero)
{
MessageBox.Show("请输入正确的标题");
return;
}
}
SetForegroundWindow(ip);
SetActiveWindow(ip);
var str = "";
var rtf = "";
richTextBox1.Invoke(new MethodInvoker(() => { str = richTextBox1.Text.Trim(); rtf = richTextBox1.Rtf.Trim(); }));
while (true)
{
if (ip.ToInt32() == GetForegroundWindow().ToInt32())
{
foreach (byte c in Encoding.Default.GetBytes(str))
{
SendMessage(ip.ToInt32(), 0x101, (int)c, 1);
SendMessage(ip.ToInt32(), 0x102, (int)c, 1);
}
SendKeys.SendWait("%s");
}
Thread.Sleep(sleep);
}
}我就这两段代码不明白
SendMessage(ip.ToInt32(), 0x101, (int)c, 1);
SendMessage(ip.ToInt32(), 0x102, (int)c, 1);他这里为向聊天输入框插入字符,
但是他这里判断没有先去得到句柄,而是用
0x101和0x102
那这两个是怎么得来的呢?
这两个表示的是什么呢?
为什么是两个,按道理输入框只有一个啊?谢谢
0x102:释放一个键
一个down一个up
SendMessage(ip.ToInt32(), 0x101, (int)c, 1);
SendMessage(ip.ToInt32(), 0x102, (int)c, 1);
是模拟按下这个C字符的键那有没有办法取得这个输入框对像后,再用API来设置Text,
而不是这样在窗口中进行模拟输入呢?谢谢
具体含义可以百度查一下
keybd_event
sendmessage 都可以。
sendkeys就算了 不能后台操作
sendmessage比较好。能后台操作
最小化后都能操作