当前位置:   article > 正文

(8)微信UI自动化-监控微信进程运行状态(C#)_c# uiautomation 微信

c# uiautomation 微信

微信定时发送消息

(1)自动化程序在运行过程中应为依赖的主程序是微信,所以要对微信进程运行状态进行监控,如果发现微信处于运行状态的情况则自动化程序可以执行对应操作,微信应为人为或者意外导致关闭的情况则需要自动化程序马上响应做出对应的处理。

(2)实现思路,开启一个线程定时监控微信的窗体是否存在则可以实现

实现过程

首先定义一个WIN32 API的调用类,用来查找窗体。

  1. /// <summary>
  2. /// 发现窗口
  3. /// </summary>
  4. public class Win32_Find_Window
  5. {
  6. [DllImport("user32.dll", EntryPoint = "FindWindow")]
  7. private extern static IntPtr FindWindow(string lpClassName, string lpWindowName);
  8. public static IntPtr Do(string className, string windowName)
  9. {
  10. return FindWindow(className, windowName);
  11. }
  12. }

定义好一个查找微信窗体的调用类

  1. public class WXWin32
  2. {
  3. private static IntPtr currentWeixin = IntPtr.Zero;
  4. public static IntPtr GetWeiXin()
  5. {
  6. // if (currentWeixin == IntPtr.Zero)
  7. currentWeixin = Win32_Find_Window.Do("WeChatMainWndForPC", "微信");
  8. return currentWeixin;
  9. }
  10. }

定义一个监控的基类,因为后续很多监控都需要

  1. public class BaseMonitor : IMonitor
  2. {
  3. public BaseMonitor() {
  4. MonitorStatus = MonitorStatus.Stop;
  5. }
  6. public MonitorStatus MonitorStatus { get; protected set; }
  7. public event EventHandler<EventArgs> MonitorStop;
  8. public virtual void Break()
  9. {
  10. MonitorStatus = MonitorStatus.Stoping;
  11. }
  12. public virtual void Monitor()
  13. {
  14. }
  15. protected void ExecuteMonitorStop()
  16. {
  17. MonitorStatus = MonitorStatus.Stop;
  18. if (MonitorStop != null)
  19. {
  20. MonitorStop(this, EventArgs.Empty);
  21. }
  22. }
  23. }
  24. public enum MonitorStatus
  25. {
  26. Monitoring = 0,
  27. Stoping = 1,
  28. Stop = 2
  29. }

定义监控类

  1. namespace WpfApp1.UIAuto.Monitor
  2. {
  3. public class UI_WX_Process_Monitor : BaseMonitor
  4. {
  5. public event EventHandler<ProcessEventArgs> ProcessEvent;
  6. public ProcessStatus Status { get; private set; } = ProcessStatus.UnKnow;
  7. public override void Monitor()
  8. {
  9. if (MonitorStatus == MonitorStatus.Monitoring)
  10. return;
  11. SystemLog.Info("启动微信是否打开监听");
  12. MonitorStatus = MonitorStatus.Monitoring;
  13. Thread th = new Thread(new ThreadStart(() =>
  14. {
  15. while (MonitorStatus == MonitorStatus.Monitoring)
  16. {
  17. if (WXWin32.GetWeiXin() != IntPtr.Zero)
  18. {
  19. DoProcess(ProcessStatus.Run);
  20. }
  21. else
  22. {
  23. DoProcess(ProcessStatus.Close);
  24. }
  25. Thread.Sleep(1000);
  26. }
  27. ExecuteMonitorStop();
  28. }));
  29. th.Start();
  30. }
  31. private void DoProcess(ProcessStatus status)
  32. {
  33. if (Status == status)
  34. return;
  35. Status = status;
  36. if (ProcessEvent != null)
  37. {
  38. ProcessEvent(this, new ProcessEventArgs { Status = status });
  39. }
  40. }
  41. public override void Break()
  42. {
  43. base.Break();
  44. SystemLog.Info("关闭微信是否打开监听");
  45. }
  46. }
  47. public class ProcessEventArgs : EventArgs
  48. {
  49. public ProcessStatus Status { get; set; }
  50. }
  51. public enum ProcessStatus
  52. {
  53. UnKnow = 0,
  54. Run = 1,
  55. Close= 2
  56. }
  57. }

调用

  1. public MainWindow()
  2. {
  3. UI_WX_Process_Monitor process = new UI_WX_Process_Monitor();
  4. process.ProcessEvent += Process_ProcessEvent;
  5. process.Monitor();
  6. }
  7. private void Process_ProcessEvent(object sender, ProcessEventArgs e)
  8. {
  9. if (e.Status == ProcessStatus.Close)
  10. {
  11. //你的具体业务代码
  12. }
  13. }

上一篇(7)微信UI自动化-自动发送消息给指定联系人(C#)(.Net)

下一篇(9)微信UI自动化-监控微信网络状态(C#)

因为文章所表达的意思可能无法满足每一位阅读需求,需要源码或者支持请联系作者QQ 978124155

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/105125
推荐阅读
相关标签
  

闽ICP备14008679号