当前位置:编程学习 > C#/ASP.NET >>

向已获得句柄的窗口中的编辑框发送消息遇到的问题

--------------------编程问答-------------------- 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;
        } --------------------编程问答-------------------- 求助!
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,