向已获得句柄的窗口中的编辑框发送消息遇到的问题 本帖最后由 zmm4040 于 2012-04-24 23:43:09 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 spy++找不到,说明这个框是程序自己画上去的,当然也就不可能有api能操纵它。 谢谢!现在我在代码中,首先设置鼠标坐标到编辑框里, 然后使用sendinput发送鼠标单击操作,再模拟键盘输出;结果是只在编辑框里单击后,输不出字符。int x = 242;//find position int y = 87; int xy = (y << 16) + x; Rect rect1 = new Rect(); GetWindowRect(hwnd, ref rect1); SetCursorPos(x + rect1.left, y + rect1.top); //Thread.Sleep(100); MouseInput myMinput = new MouseInput(); myMinput.dx = x; myMinput.dy = y; myMinput.Mousedata = 0; //myMinput.dwFlag = MouseEvent_Absolute | MouseEvent_Move | MouseEvent_LeftDown | MouseEvent_LeftUp; myMinput.dwFlag = MouseEvent_LeftDown | MouseEvent_LeftUp; myMinput.time = 0; Input[] myInput = new Input[1]; myInput[0] = new Input(); myInput[0].type = 0; myInput[0].mi = myMinput; UInt32 result = SendInput((uint)myInput.Length, myInput, Marshal.SizeOf(myInput[0].GetType())); Thread.Sleep(60); SendUnicode("df"); 附上 SendUnicode的函数;public static void SendUnicode(string message) { for (int i = 0; i < message.Length; i++) { Input[] input_down = new Input[1]; input_down[0].type = (int)InputType.INPUT_KEYBOARD; input_down[0].ki.dwFlags = (int)KEYEVENTF.UNICODE; input_down[0].ki.wScan = (short)message[i]; input_down[0].ki.wVk = 0; UInt32 result = SendInput((uint)input_down.Length, input_down, Marshal.SizeOf(input_down[0].GetType()));//keydown; (uint)input_down.Length //UInt32 result = SendInput((uint)myInput.Length, myInput, Marshal.SizeOf(myInput[0].GetType())); /*if (result != 0) { MessageBox.Show("Down successful"); }*/ Input[] input_up = new Input[1]; input_up[0].type = (int)InputType.INPUT_KEYBOARD; input_up[0].ki.wScan = (short)message[i]; input_up[0].ki.wVk = 0; input_up[0].ki.dwFlags = (int)(KEYEVENTF.KEYUP | KEYEVENTF.UNICODE); UInt32 result1 = SendInput((uint)input_down.Length, input_up, Marshal.SizeOf(input_up[0].GetType()));//keyup /*if (result1 != 0) { MessageBox.Show("Up successful"); } if (i == message.Length) { MessageBox.Show("All successful"); }*/ Thread.Sleep(10); } } [DllImport("user32.dll")] public static extern UInt32 SendInput(UInt32 nInputs, Input[] pInputs, int cbSize); [StructLayout(LayoutKind.Explicit)] public struct Input { [FieldOffset(0)] public Int32 type; [FieldOffset(4)] public MouseInput mi; [FieldOffset(4)] public tagKEYBDINPUT ki; [FieldOffset(4)] public tagHARDWAREINPUT hi; } private enum InputType { INPUT_MOUSE = 0, INPUT_KEYBOARD = 1, INPUT_HARDWARE = 2, } private enum KEYEVENTF { EXTENDEDKEY = 0x0001, KEYUP = 0x0002, UNICODE = 0x0004, SCANCODE = 0x0008, } [StructLayout(LayoutKind.Sequential)] public struct MouseInput { public Int32 dx; public Int32 dy; public Int32 Mousedata; public Int32 dwFlag; public Int32 time; public IntPtr dwExtraInfo; } [StructLayout(LayoutKind.Sequential)] public struct tagKEYBDINPUT { public Int16 wVk; public Int16 wScan; public Int32 dwFlags; public Int32 time; public IntPtr dwExtraInfo; } SQLSERVER的REPLACE方法 人人网自动登陆 c#中的"object"问题,代码摆出,100分奉上。 怎样在winform中用xml实现树形菜单? 请高手指点多个人同时开发一个项目时如果分工? 如何找到控件label ???? 谁能提供用ADO.Net+ACCESS或SQL开发的程序的源码 急求CLIPS动态连接库 在web编程时,如何得到alert对话框的返回值呀? C#进度条。 WMI 远程注册表 访问 如何获取Ajax网页数据?
结果是只在编辑框里单击后,输不出字符。int x = 242;//find position
int y = 87;
int xy = (y << 16) + x; Rect rect1 = new Rect();
GetWindowRect(hwnd, ref rect1);
SetCursorPos(x + rect1.left, y + rect1.top); //Thread.Sleep(100); MouseInput myMinput = new MouseInput();
myMinput.dx = x;
myMinput.dy = y;
myMinput.Mousedata = 0;
//myMinput.dwFlag = MouseEvent_Absolute | MouseEvent_Move | MouseEvent_LeftDown | MouseEvent_LeftUp;
myMinput.dwFlag = MouseEvent_LeftDown | MouseEvent_LeftUp;
myMinput.time = 0; Input[] myInput = new Input[1];
myInput[0] = new Input();
myInput[0].type = 0;
myInput[0].mi = myMinput; UInt32 result = SendInput((uint)myInput.Length, myInput, Marshal.SizeOf(myInput[0].GetType())); Thread.Sleep(60); SendUnicode("df");
{
for (int i = 0; i < message.Length; i++)
{
Input[] input_down = new Input[1];
input_down[0].type = (int)InputType.INPUT_KEYBOARD;
input_down[0].ki.dwFlags = (int)KEYEVENTF.UNICODE;
input_down[0].ki.wScan = (short)message[i];
input_down[0].ki.wVk = 0;
UInt32 result = SendInput((uint)input_down.Length, input_down, Marshal.SizeOf(input_down[0].GetType()));//keydown; (uint)input_down.Length
//UInt32 result = SendInput((uint)myInput.Length, myInput, Marshal.SizeOf(myInput[0].GetType()));
/*if (result != 0)
{
MessageBox.Show("Down successful");
}*/ Input[] input_up = new Input[1];
input_up[0].type = (int)InputType.INPUT_KEYBOARD;
input_up[0].ki.wScan = (short)message[i];
input_up[0].ki.wVk = 0;
input_up[0].ki.dwFlags = (int)(KEYEVENTF.KEYUP | KEYEVENTF.UNICODE);
UInt32 result1 = SendInput((uint)input_down.Length, input_up, Marshal.SizeOf(input_up[0].GetType()));//keyup
/*if (result1 != 0)
{
MessageBox.Show("Up successful");
} if (i == message.Length)
{
MessageBox.Show("All successful");
}*/
Thread.Sleep(10);
}
}
[DllImport("user32.dll")]
public static extern UInt32 SendInput(UInt32 nInputs, Input[] pInputs, int cbSize);
[StructLayout(LayoutKind.Explicit)]
public struct Input
{
[FieldOffset(0)]
public Int32 type;
[FieldOffset(4)]
public MouseInput mi;
[FieldOffset(4)]
public tagKEYBDINPUT ki;
[FieldOffset(4)]
public tagHARDWAREINPUT hi;
} private enum InputType
{
INPUT_MOUSE = 0,
INPUT_KEYBOARD = 1,
INPUT_HARDWARE = 2,
} private enum KEYEVENTF
{
EXTENDEDKEY = 0x0001,
KEYUP = 0x0002,
UNICODE = 0x0004,
SCANCODE = 0x0008,
} [StructLayout(LayoutKind.Sequential)]
public struct MouseInput
{
public Int32 dx;
public Int32 dy;
public Int32 Mousedata;
public Int32 dwFlag;
public Int32 time;
public IntPtr dwExtraInfo;
} [StructLayout(LayoutKind.Sequential)]
public struct tagKEYBDINPUT
{
public Int16 wVk;
public Int16 wScan;
public Int32 dwFlags;
public Int32 time;
public IntPtr dwExtraInfo;
}