陈程的技术博客

  • 关于作者
全栈软件工程师
一个专注于技术研究创新的程序员
  1. 首页
  2. .NET
  3. 正文

C# 采用Automation控制自动化操作第三方软件

2021年8月25日 1604点热度 0人点赞 0条评论

https://chencblog.top/wp-content/uploads/2021/08/自动控制电话软件.mp4

自动控制软电话

git:https://gitee.com/zuiyuewentian/automation-micro-sip

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);
      }
  }
///<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# winform 自动化
最后更新:2021年8月25日

博主

全栈工程师,侧重项目技术解决方案规划和开发

打赏 点赞
< 上一篇
下一篇 >

文章评论

取消回复

分类
  • .NET (65)
  • docker (3)
  • linux (12)
  • python (20)
  • web (14)
  • 小程序 (4)
  • 数据库 (2)
  • 未分类 (4)
  • 杂七杂八 (10)
标签聚合
js DevExpress centos C# linux winform python nginx
最新 热点 随机
最新 热点 随机
.NET开发手册标准参考 招募兼职前端开发 Centos安装dotnet6环境 VS上切换分支,vs编译运行出现bug,A fatal error was encountered彻底解决方案 用C#封装一个线程安全的缓存器,达到目标定时定量更新入库 C#通过特性的方式去校验指定数据是否为空
centOS 配置pip永久下载源镜像 JsonHelper序列化和反序列化 DreamSkin自定义美化控件-TreeView控件 python快速把office文档execl或者word等转成pdf 怎样用JS写一个下拉加载数据的table xml序列化和反序列化代码

COPYRIGHT © 2021 陈程的技术博客. ALL RIGHTS RESERVED.

THEME KRATOS MADE BY VTROIS