/branches/vs2010/src/ACorns.Hawkeye.CoreUI/Utils/WindowProperties.cs

# · C# · 159 lines · 130 code · 23 blank · 6 comment · 20 complexity · effb3418977ba114c103be76da107255 MD5 · raw file

  1. using System;
  2. using System.Drawing;
  3. using System.Text;
  4. using System.Windows.Forms;
  5. using ACorns.Hawkeye.Plugins;
  6. using ACorns.Hawkeye.Core.Utils;
  7. using ACorns.Hawkeye.Core.UI;
  8. namespace ACorns.Hawkeye.Utils
  9. {
  10. /// <summary>
  11. /// Summary description for WindowProperties.
  12. /// </summary>
  13. internal class WindowProperties : IDisposable
  14. {
  15. private static Pen drawPen = new Pen(Brushes.Red, 2);
  16. private IntPtr detectedWindow = IntPtr.Zero;
  17. private Point lastPoint = Point.Empty;
  18. private object selectedObject;
  19. public IntPtr DetectedWindow
  20. {
  21. get { return detectedWindow; }
  22. }
  23. public object ActiveObject
  24. {
  25. get { return selectedObject; }
  26. }
  27. public object ActiveWindow
  28. {
  29. get
  30. {
  31. if (detectedWindow != IntPtr.Zero)
  32. {
  33. return Control.FromHandle(detectedWindow);
  34. }
  35. else
  36. {
  37. return null;
  38. }
  39. }
  40. }
  41. public string ClassName
  42. {
  43. get
  44. {
  45. if (!IsValid)
  46. return null;
  47. return NativeUtils.GetClassName(detectedWindow);
  48. }
  49. }
  50. public bool IsManagedByClassName
  51. {
  52. get
  53. {
  54. string className = ClassName;
  55. if (className != null && className.StartsWith("WindowsForms10"))
  56. {
  57. return true;
  58. }
  59. else
  60. {
  61. return false;
  62. }
  63. //Match match = classNameRegex.Match(ClassName);
  64. //return match.Success;
  65. }
  66. }
  67. public bool IsValid
  68. {
  69. get { return detectedWindow != IntPtr.Zero; }
  70. }
  71. public bool IsManaged
  72. {
  73. get { return ActiveWindow != null; }
  74. }
  75. internal bool SetWindowHandle(IntPtr handle, Point lastPoint)
  76. {
  77. Refresh();
  78. this.lastPoint = lastPoint;
  79. this.detectedWindow = handle;
  80. Refresh();
  81. Highlight();
  82. bool changed = false;
  83. object activeWindow = ActiveWindow;
  84. if ( activeWindow != selectedObject )
  85. {
  86. changed = true;
  87. selectedObject = activeWindow;
  88. }
  89. // try to load plugins
  90. Point checkPoint = lastPoint;
  91. if ( detectedWindow != IntPtr.Zero )
  92. checkPoint = NativeUtils.NativeScreenToClient(detectedWindow, lastPoint);
  93. object lastSelected = detectedWindow;
  94. if (checkPoint != Point.Empty && PluginManager.Instance.ResolveSelection(checkPoint, lastSelected, ref selectedObject))
  95. {
  96. changed = true;
  97. }
  98. return changed;
  99. }
  100. public void Refresh()
  101. {
  102. if (!IsValid)
  103. return;
  104. IntPtr toUpdate = this.detectedWindow;
  105. IntPtr parentWindow = NativeUtils.GetParent(toUpdate);
  106. if (parentWindow != IntPtr.Zero)
  107. {
  108. toUpdate = parentWindow; // using parent
  109. }
  110. NativeUtils.InvalidateRect(toUpdate, IntPtr.Zero, true);
  111. NativeUtils.UpdateWindow(toUpdate);
  112. bool result = NativeUtils.RedrawWindow(toUpdate, IntPtr.Zero, IntPtr.Zero, NativeUtils.RDW.RDW_FRAME | NativeUtils.RDW.RDW_INVALIDATE | NativeUtils.RDW.RDW_UPDATENOW | NativeUtils.RDW.RDW_ERASENOW | NativeUtils.RDW.RDW_ALLCHILDREN);
  113. }
  114. public void Highlight()
  115. {
  116. IntPtr windowDC;
  117. RECT windowRect = new RECT(0, 0, 0, 0);
  118. NativeUtils.GetWindowRect(detectedWindow, ref windowRect);
  119. IntPtr parentWindow = NativeUtils.GetParent(detectedWindow);
  120. windowDC = NativeUtils.GetWindowDC(detectedWindow);
  121. if (windowDC != IntPtr.Zero)
  122. {
  123. Graphics graph = Graphics.FromHdc(windowDC, detectedWindow);
  124. graph.DrawRectangle(drawPen, 1, 1, windowRect.Width - 2, windowRect.Height - 2);
  125. graph.Dispose();
  126. NativeUtils.ReleaseDC(detectedWindow, windowDC);
  127. }
  128. }
  129. #region IDisposable Members
  130. public void Dispose()
  131. {
  132. Refresh();
  133. }
  134. #endregion
  135. }
  136. }