美文网首页开发资料
自动化测试(控制其他程序控件)

自动化测试(控制其他程序控件)

作者: 落地成佛 | 来源:发表于2018-07-11 11:02 被阅读24次
        public partial class Form1 : Form
        {
            public uint VK_RETURN = 0x0D;
            public int WM_SETTEXT = 0x000C;
            public int WM_KEYDOWN = 0x100;
            public Form1()
            {
                InitializeComponent();
            }
            private void button1_Click(object sender, EventArgs e)
            {
                var iResult = new List<IntPtr>();
                IntPtr hwnd = FindWindow(null, "DWS");
    
              //循环窗体下的所有控件
                EnumChildWindows(
                hwnd,
                (h, l) =>
                {
                    IntPtr f1 = FindWindowEx(h, IntPtr.Zero, "WindowsForms10.EDIT.app.0.34f5582_r12_ad1", null);
                    if (f1 == IntPtr.Zero)
                        return true;
                    else
                    {
                        iResult.Add(f1);
                        return true;
                    }
                },
                0);
                var indexs = int.Parse( textBox1.Text);
    
                 SendMessage(iResult.Skip(indexs).Take(1).First(), WM_SETTEXT, IntPtr.Zero, new StringBuilder("00050B80"));
                  //回车键
                 SendMessage(iResult.Skip(indexs).Take(1).First(), WM_KEYDOWN, VK_RETURN, 0);
                //设置文字
            }
    
    
            [DllImport("User32.dll", EntryPoint = "FindWindow")]
            public extern static IntPtr FindWindow(string lpClassName, string lpWindowName);
            [DllImport("user32.dll")]
            private static extern Int32 SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, StringBuilder lParam);
            [DllImport("user32.dll", EntryPoint = "SendMessage")]
            public static extern int SendMessage(IntPtr hwnd, int wMsg, uint wParam, uint lParam);
            [DllImport("user32.dll", EntryPoint = "FindWindowEx", CharSet = CharSet.Auto)]
            private extern static IntPtr FindWindowEx(IntPtr parent, IntPtr child, string classname, string captionName);
    
            public delegate bool CallBack(IntPtr hwnd, int lParam);
            [DllImport("user32.DLL")]
            public static extern int EnumChildWindows(IntPtr hWndParent, CallBack lpfn, int lParam);
        }
    

    相关文章

      网友评论

        本文标题:自动化测试(控制其他程序控件)

        本文链接:https://www.haomeiwen.com/subject/hsthpftx.html