C# app to record currently focused window
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

WindowMonitorService.cs 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. namespace WindowMonitor
  2. {
  3. using System;
  4. using System.Linq;
  5. using System.ServiceProcess;
  6. using System.Text;
  7. using System.Text.RegularExpressions;
  8. using System.Timers;
  9. /// <summary>
  10. /// Background service to monitor window status.
  11. /// </summary>
  12. public partial class WindowMonitorService : ServiceBase
  13. {
  14. /// <summary>
  15. /// Timer to use to schedule window checks.
  16. /// </summary>
  17. private Timer timer;
  18. /// <summary>
  19. /// The window monitor to use.
  20. /// </summary>
  21. private WindowMonitor monitor;
  22. /// <summary>
  23. /// The regex to use to extract image names.
  24. /// </summary>
  25. private Regex filterRegex;
  26. /// <summary>
  27. /// Initializes a new instance of the <see cref="WindowMonitorService"/> class.
  28. /// </summary>
  29. /// <param name="imageFilter">
  30. /// The filter to use to extract image names.
  31. /// </param>
  32. public WindowMonitorService(string imageFilter)
  33. {
  34. this.InitializeComponent();
  35. this.filterRegex = new Regex(imageFilter);
  36. }
  37. /// <summary>
  38. /// Starts the service.
  39. /// </summary>
  40. public void Startup()
  41. {
  42. this.monitor = new WindowMonitor();
  43. this.timer = new Timer(1000);
  44. this.timer.Elapsed += this.OnTimerElapsed;
  45. this.timer.Start();
  46. }
  47. /// <summary>
  48. /// Stops the service.
  49. /// </summary>
  50. public void Shutdown()
  51. {
  52. this.timer.Stop();
  53. this.timer.Elapsed -= this.OnTimerElapsed;
  54. this.timer.Dispose();
  55. this.timer = null;
  56. this.monitor = null;
  57. }
  58. /// <summary>
  59. /// Called when the service is being started by the Windows host.
  60. /// </summary>
  61. /// <param name="args">
  62. /// The arguments given to the service.
  63. /// </param>
  64. protected override void OnStart(string[] args)
  65. {
  66. this.Startup();
  67. }
  68. /// <summary>
  69. /// Called when the service is being stopped by the Windows host.
  70. /// </summary>
  71. protected override void OnStop()
  72. {
  73. this.Shutdown();
  74. }
  75. /// <summary>
  76. /// Called when our timer elapses, triggering us to do some work.
  77. /// </summary>
  78. /// <param name="sender">
  79. /// The timer that sent the event.
  80. /// </param>
  81. /// <param name="e">
  82. /// The event arguments.
  83. /// </param>
  84. private void OnTimerElapsed(object sender, ElapsedEventArgs e)
  85. {
  86. var handle = this.monitor.GetActiveWindowHandle();
  87. Console.WriteLine(this.monitor.GetWindowTitle(handle));
  88. var fileName = this.monitor.GetWindowFileName(handle);
  89. Console.WriteLine(fileName);
  90. var matches = this.filterRegex.Match(fileName);
  91. var builder = new StringBuilder();
  92. for (var i = 1; i <= matches.Groups.Count; i++)
  93. {
  94. if (builder.Length > 0)
  95. {
  96. builder.Append(' ');
  97. }
  98. builder.Append(matches.Groups[i].Value);
  99. }
  100. Console.WriteLine(builder);
  101. Console.WriteLine();
  102. }
  103. }
  104. }