RevitWindow.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*-------------------------------------------------------------------------
  2. * 功能描述:RevitWindow
  3. * 作者:xulisong
  4. * 创建时间: 2019/6/3 18:04:27
  5. * 版本号:v1.0
  6. * -------------------------------------------------------------------------*/
  7. using FWindSoft.Common;
  8. using FWindSoft.WindowsApi;
  9. using FWindSoft.Wpf;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Diagnostics;
  13. using System.IO;
  14. using System.Linq;
  15. using System.Text;
  16. using System.Threading.Tasks;
  17. using System.Timers;
  18. using System.Windows;
  19. using System.Windows.Interop;
  20. using System.Windows.Media.Imaging;
  21. namespace FWindSoft.Revit.Window
  22. {
  23. public class RevitWindow: HWindow
  24. {
  25. public RevitWindow()
  26. {
  27. LoadIcon();
  28. Handle=SetWindowOwner();
  29. ResizeMode =System.Windows.ResizeMode.NoResize;
  30. ShowInTaskbar = false;
  31. }
  32. protected override void OnLoaded(RoutedEventArgs e)
  33. {
  34. base.OnLoaded(e);
  35. StartHook();
  36. }
  37. #region 基础信息加载
  38. /// <summary>
  39. /// 当前窗体的窗口句柄
  40. /// </summary>
  41. protected IntPtr Handle { get; private set; }
  42. /// <summary>
  43. /// 加载图标
  44. /// </summary>
  45. protected virtual void LoadIcon()
  46. {
  47. try
  48. {
  49. string path = Path.Combine(App.DllRunPath, "Default.ico");
  50. if (!File.Exists(path))
  51. {
  52. return;
  53. }
  54. Icon = new BitmapImage(new Uri(path, UriKind.RelativeOrAbsolute));
  55. }
  56. catch (Exception)
  57. {
  58. Debug.WriteLine("图标加载失败");
  59. }
  60. }
  61. /// <summary>
  62. /// 设置本窗口的Owner,并返回本窗体handle
  63. /// </summary>
  64. /// <returns></returns>
  65. protected IntPtr SetWindowOwner()
  66. {
  67. var winHelp = new WindowInteropHelper(this);
  68. var handle = winHelp.EnsureHandle();
  69. WinAPI.SetForegroundWindow(handle);
  70. winHelp.Owner = App.MainHandle;
  71. return handle;
  72. }
  73. #endregion
  74. #region 增加键盘监控信息
  75. protected KeyboardHook m_Hook = null;
  76. private void StartHook()
  77. {
  78. m_Hook = new KeyboardHook(true, App.MainHandle);
  79. m_Hook.KeyUp += (send, e) =>
  80. {
  81. if (e.KeyData == Keys.Escape)
  82. {
  83. Close();
  84. }
  85. };
  86. this.Closed += (send, e) => StopHook();
  87. m_Hook.Install();
  88. }
  89. private void StopHook()
  90. {
  91. if (m_Hook != null)
  92. {
  93. m_Hook.Dispose();
  94. }
  95. }
  96. #endregion
  97. #region 鼠标位置监控
  98. /// <summary>
  99. /// 是否暂停监视鼠标移入移出
  100. /// </summary>
  101. public bool PauseWatchMouse { get; set; }
  102. protected Timer m_Timer;
  103. private bool m_IsMouseIn;
  104. protected void StartEnterLeaveT()
  105. {
  106. try
  107. {
  108. StartTimer();
  109. Rect rect = GetRect();
  110. WinAPI.GetCursorPos(out WindowsApi.Point pt);
  111. var isInWindow = rect.Contains(new System.Windows.Point(pt.X,pt.Y));
  112. IntPtr intPtr = WinAPI.WindowFromPoint(pt);
  113. bool isSameWindow = intPtr.Equals(Handle);
  114. //初始化状态,如果鼠标指针在外部,为了启动命令,在这里需要设置进入命令的前一个状态
  115. //参考具体执行命令
  116. m_IsMouseIn = !isInWindow && isSameWindow;
  117. }
  118. catch (Exception ex)
  119. {
  120. throw ex;
  121. }
  122. }
  123. //关闭监视
  124. protected void StopEnterLeaveT()
  125. {
  126. StopTimer();
  127. }
  128. private void StartTimer()
  129. {
  130. try
  131. {
  132. if (m_Timer == null)
  133. m_Timer = new Timer(5);
  134. if (!m_Timer.Enabled)
  135. {
  136. m_Timer.Elapsed += m_Timer_Elapsed;
  137. m_Timer.Start();
  138. }
  139. }
  140. catch (Exception ex)
  141. {
  142. throw ex;
  143. }
  144. }
  145. private void StopTimer()
  146. {
  147. try
  148. {
  149. if (m_Timer != null)
  150. {
  151. if (m_Timer.Enabled)
  152. m_Timer.Stop();
  153. m_Timer.Dispose();
  154. m_Timer = null;
  155. }
  156. }
  157. catch (Exception ex)
  158. {
  159. throw ex;
  160. }
  161. }
  162. private void m_Timer_Elapsed(object sender, ElapsedEventArgs e)
  163. {
  164. if (PauseWatchMouse) return;
  165. CheckMouseState();
  166. }
  167. /// <summary>
  168. /// 检测鼠标状态
  169. /// </summary>
  170. private void CheckMouseState()
  171. {
  172. try
  173. {
  174. Rect rect = GetRect();
  175. WinAPI.GetCursorPos(out WindowsApi.Point currentPoint);
  176. if (rect.Contains(new System.Windows.Point(currentPoint.X, currentPoint.Y)))
  177. {
  178. IntPtr currentIntPtr = WinAPI.WindowFromPoint(currentPoint);
  179. if (Handle.Equals(currentIntPtr))
  180. {
  181. if (!m_IsMouseIn)
  182. {
  183. m_IsMouseIn = true;
  184. OnMouseEnterT();
  185. }
  186. }
  187. }
  188. else
  189. {
  190. if (m_IsMouseIn)
  191. {
  192. m_IsMouseIn = false;
  193. OnMouseLeaveT();
  194. }
  195. }
  196. }
  197. catch (Exception excpetion)
  198. {
  199. throw excpetion;
  200. }
  201. }
  202. protected Rect GetRect()
  203. {
  204. Rect rect = Dispatcher.Invoke(new Func<Rect>(() =>
  205. {
  206. Rect tempRect = new Rect(new System.Windows.Point(Left, Top), new Size(ActualWidth, ActualHeight));
  207. return tempRect;
  208. }));
  209. return rect;
  210. }
  211. protected void RaiseOnMouseLeaveT()
  212. {
  213. try
  214. {
  215. var rect = GetRect();
  216. WinAPI.GetCursorPos(out WindowsApi.Point currentPoint);
  217. if (!rect.Contains(new System.Windows.Point(currentPoint.X, currentPoint.Y)))
  218. {
  219. OnMouseLeaveT();
  220. }
  221. }
  222. catch (Exception ex)
  223. {
  224. throw ex;
  225. }
  226. }
  227. public event EventHandler MouseEnterT;
  228. public event EventHandler MouseLeaveT;
  229. protected virtual void OnMouseEnterT()
  230. {
  231. MouseEnterT?.Invoke(this, EventArgs.Empty);
  232. }
  233. protected virtual void OnMouseLeaveT()
  234. {
  235. MouseLeaveT?.Invoke(this, EventArgs.Empty);
  236. }
  237. #endregion
  238. }
  239. }