C# 采用Automation控制自动拨打接听电话

1 篇文章 0 订阅

在这里插入图片描述
启动找到第三方软件的进程,并获取到它的句柄

int pid = 0;
  private void Form2_Load(object sender, EventArgs e)
  {
      Process[] ps = Process.GetProcessesByName("MicroSIP");
      if (ps.Length > 0)
      {
          foreach (Process p in ps)
              p.Kill();
      }

      pid = StartExe(@"D:\Users\lenovo\AppData\Local\MicroSIP\microsip.exe");
      
  }

  private void Button_Click(object sender, EventArgs e)
  {
      Button btn = (Button)sender as Button;
      int btntext = Convert.ToInt32(btn.Text);
      ButtonLeftClick(btnList[btntext]);
  }

  private void button1_Click(object sender, EventArgs e)
  {
      automationElement = GetWindowHandle(pid, 1);
      var autoBtn = automationElement.FindAll(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "Button"));
      btnList = autoBtn;

      var textBox_str = automationElement.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "ComboBox"));

      textBox1.Text = textBox_str.Current.Name;

      timer1.Start();
  }

  ///<summary>
  ///根据传入的路径启动相应的可执行程序,并返回进程ID
  ///</summary>
  public Int32 StartExe(string strExePath)
  {
      if (null == strExePath)
      {
          return 0;
      }

      Process ps = Process.Start(strExePath);
      Thread.Sleep(3000);

      return ps.Id;
  }

  ///<summary>
  ///根据进程ID,查找相应窗体,并返回窗体句柄
  ///</summary>
  public AutomationElement GetWindowHandle(Int32 pid, int iWaitSecond)
  {
      AutomationElement targetWindow = null;
      int iWaitTime = 0;

      try
      {
          Process ps = Process.GetProcessById(pid);
          targetWindow = AutomationElement.FromHandle(ps.MainWindowHandle);

          while (null == targetWindow)
          {
              if (iWaitTime > iWaitSecond)
              {
                  break;
              }

              Thread.Sleep(500);

              targetWindow = AutomationElement.FromHandle(ps.MainWindowHandle);
          }

          return targetWindow;
      }
      catch (System.Exception ex)
      {
          string msg = "没有找到指定的窗口,请确认窗口已经启动!";

          throw new InvalidProgramException(msg, ex);
      }
  }

定位button按钮

///<summart>
///根据Button按钮句柄,进行鼠标左键单击
///</summary>
public static bool ButtonLeftClick(AutomationElement ButtonHandle)
{
    object objButton = null;
    InvokePattern ivkpButton = null;

    try
    {
        if (null == ButtonHandle)
        {
            return false;
        }

        if (!ButtonHandle.TryGetCurrentPattern(InvokePattern.Pattern, out objButton))
        {
            return false;
        }

        ivkpButton = (InvokePattern)objButton;

        ivkpButton.Invoke();

        return true;
    }
    catch (System.Exception ex)
    {
        string msg = "鼠标左键单击失败!";

        throw new InvalidProgramException(msg, ex);
    }
}

定位复选框

/// <summary>
      /// 判断复选框的值
      /// </summary>
      /// <param name="element"></param>
      /// <returns></returns>
      private bool IsElementToggledOn(AutomationElement element)
      {
          if (element == null)
          {
              return false;
          }

          Object objPattern;
          TogglePattern togPattern;
          if (true == element.TryGetCurrentPattern(TogglePattern.Pattern, out objPattern))
          {
              togPattern = objPattern as TogglePattern;
              return togPattern.Current.ToggleState == ToggleState.On;
          }
          return false;
      }

      /// <summary>
      /// 点击复选框
      /// </summary>
      /// <param name="element"></param>
      private void ClickToggledOn(AutomationElement element)
      {
          if (element == null)
          {
              // TODO: Invalid parameter error handling.
              return;
          }

          Object objPattern;
          TogglePattern togPattern;
          if (true == element.TryGetCurrentPattern(TogglePattern.Pattern, out objPattern))
          {
              togPattern = objPattern as TogglePattern;
              togPattern.Toggle();
          }
      }

      private void timer1_Tick(object sender, EventArgs e)
      {
          try
          {


              var status = automationElement.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "msctls_statusbar32"));
              string name = status.Current.Name;
              label1.Text = name;

              var textBox_str = automationElement.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "ComboBox"));
              textBox1.Text = textBox_str.Current.Name;
              
          }
          catch (Exception ex)
          {

          }
      }

      private void button22_Click(object sender, EventArgs e)
      {
          Form3 form = new Form3();
          form.ShowDialog();
          var textBox_str = automationElement.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.ClassNameProperty, "ComboBox"));

          ValuePattern valuePattern = (ValuePattern)textBox_str.GetCurrentPattern(ValuePattern.Pattern);

          valuePattern.SetValue(Form3.textNumber);
      }
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

搬砖的诗人Z

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值