C#教程

关注公众号 jb51net

关闭
首页 > 软件编程 > C#教程 > C# Automation控制自动拨打接听电话

C#使用Automation实现控制自动拨打接听电话

作者:搬砖的诗人Z

这篇文章主要为大家详细介绍了C#如何使用Automation实现控制自动拨打接听电话,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下

效果图

启动找到第三方软件的进程,并获取到它的句柄

实现代码

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);
      }

到此这篇关于C#使用Automation实现控制自动拨打接听电话的文章就介绍到这了,更多相关C# Automation控制自动拨打接听电话内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文