[DllImport("User32.dll", EntryPoint = "FindWindow")]
    private static extern IntPtr FindWindow(string lpClassName,string lpWindowName);

    [DllImport("user32.dll", EntryPoint = "FindWindowEx",SetLastError = true)]
    private static extern IntPtr FindWindowEx(IntPtr hwndParent,IntPtr hwndChildAfter, stringlpszClass, string lpszWindow);

    [DllImport("User32.dll", EntryPoint = "SendMessage")]
    private static extern int SendMessage(IntPtr hWnd,int Msg, IntPtr wParam, string lParam);

    const int WM_GETTEXT = 0x000D;
    const int WM_SETTEXT = 0x000C;
    const int WM_CLICK = 0x00F5;

    public Form1()
    {
      InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {

        int retval = 0; //增加一个返回值用来判断操作是否成功
        //string lpszParentClass = "#32770"; //整个窗口的类名
        string lpszParentWindow = "Form1"; //窗口标题
        string lpszClass = "WindowsForms10.EDIT.app.0.b7ab7b"; //需要查找的子窗口的类名,也就是输入框
        //string lpszClass = "Edit";
        string lpszClass_Submit = "WindowsForms10.BUTTON.app.0.b7ab7b"; //需要查找的Button的类名
        //string lpszClass_Submit = "Button";
        string lpszName_Submit = "确定"; //需要查找的Button的标题
        string text = "";

        IntPtr ParenthWnd = new IntPtr(0);
        IntPtr EdithWnd = new IntPtr(0);

        //查到窗体,得到整个窗体
        ParenthWnd = FindWindow(null, lpszParentWindow);

        //判断这个窗体是否有效
        if (!ParenthWnd.Equals(IntPtr.Zero))
        {
          //得到Form1这个子窗体的文本框,并设置其内容
          EdithWnd = FindWindowEx(ParenthWnd, EdithWnd, lpszClass, "");   [color=#FF0000]这里获取到的EdithWnd始终为0;[/color]
        
          if (!EdithWnd.Equals(IntPtr.Zero))
          {
            text = "test1";
            //调用SendMessage方法设置其内容
            SendMessage(EdithWnd, WM_SETTEXT, IntPtr.Zero, text);
            retval++;
          }
        
          //得到Button这个子窗体,并触发它的Click事件
          EdithWnd = FindWindowEx(ParenthWnd,
          (IntPtr)0, lpszClass_Submit, lpszName_Submit);
          if (!EdithWnd.Equals(IntPtr.Zero))
          {
            SendMessage(EdithWnd, WM_CLICK, (IntPtr)0, "0");
            retval++;
          }
        }
    }

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-20
  • 2022-02-21
  • 2022-02-18
  • 2021-09-21
  • 2022-02-27
猜你喜欢
  • 2022-12-23
  • 2021-07-30
  • 2022-01-11
  • 2022-12-23
  • 2022-12-23
  • 2022-02-08
  • 2021-12-21
相关资源
相似解决方案