123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256 |
- /*-------------------------------------------------------------------------
- * 功能描述:RevitWindow
- * 作者:xulisong
- * 创建时间: 2019/6/3 18:04:27
- * 版本号:v1.0
- * -------------------------------------------------------------------------*/
- using FWindSoft.Common;
- using FWindSoft.WindowsApi;
- using FWindSoft.Wpf;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Timers;
- using System.Windows;
- using System.Windows.Interop;
- using System.Windows.Media.Imaging;
- namespace FWindSoft.Revit.Window
- {
- public class RevitWindow: HWindow
- {
- public RevitWindow()
- {
- LoadIcon();
- Handle=SetWindowOwner();
- ResizeMode =System.Windows.ResizeMode.NoResize;
- ShowInTaskbar = false;
- }
- protected override void OnLoaded(RoutedEventArgs e)
- {
- base.OnLoaded(e);
- StartHook();
- }
- #region 基础信息加载
- /// <summary>
- /// 当前窗体的窗口句柄
- /// </summary>
- protected IntPtr Handle { get; private set; }
- /// <summary>
- /// 加载图标
- /// </summary>
- protected virtual void LoadIcon()
- {
- try
- {
- string path = Path.Combine(App.DllRunPath, "Default.ico");
- if (!File.Exists(path))
- {
- return;
- }
- Icon = new BitmapImage(new Uri(path, UriKind.RelativeOrAbsolute));
- }
- catch (Exception)
- {
- Debug.WriteLine("图标加载失败");
- }
- }
- /// <summary>
- /// 设置本窗口的Owner,并返回本窗体handle
- /// </summary>
- /// <returns></returns>
- protected IntPtr SetWindowOwner()
- {
- var winHelp = new WindowInteropHelper(this);
- var handle = winHelp.EnsureHandle();
- WinAPI.SetForegroundWindow(handle);
- winHelp.Owner = App.MainHandle;
- return handle;
- }
- #endregion
- #region 增加键盘监控信息
- protected KeyboardHook m_Hook = null;
- private void StartHook()
- {
- m_Hook = new KeyboardHook(true, App.MainHandle);
- m_Hook.KeyUp += (send, e) =>
- {
- if (e.KeyData == Keys.Escape)
- {
- Close();
- }
- };
- this.Closed += (send, e) => StopHook();
- m_Hook.Install();
- }
- private void StopHook()
- {
- if (m_Hook != null)
- {
- m_Hook.Dispose();
- }
- }
- #endregion
- #region 鼠标位置监控
- /// <summary>
- /// 是否暂停监视鼠标移入移出
- /// </summary>
- public bool PauseWatchMouse { get; set; }
- protected Timer m_Timer;
- private bool m_IsMouseIn;
- protected void StartEnterLeaveT()
- {
- try
- {
- StartTimer();
- Rect rect = GetRect();
- WinAPI.GetCursorPos(out WindowsApi.Point pt);
- var isInWindow = rect.Contains(new System.Windows.Point(pt.X,pt.Y));
- IntPtr intPtr = WinAPI.WindowFromPoint(pt);
- bool isSameWindow = intPtr.Equals(Handle);
- //初始化状态,如果鼠标指针在外部,为了启动命令,在这里需要设置进入命令的前一个状态
- //参考具体执行命令
- m_IsMouseIn = !isInWindow && isSameWindow;
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- //关闭监视
- protected void StopEnterLeaveT()
- {
- StopTimer();
- }
- private void StartTimer()
- {
- try
- {
- if (m_Timer == null)
- m_Timer = new Timer(5);
- if (!m_Timer.Enabled)
- {
- m_Timer.Elapsed += m_Timer_Elapsed;
- m_Timer.Start();
- }
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- private void StopTimer()
- {
- try
- {
- if (m_Timer != null)
- {
- if (m_Timer.Enabled)
- m_Timer.Stop();
- m_Timer.Dispose();
- m_Timer = null;
- }
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- private void m_Timer_Elapsed(object sender, ElapsedEventArgs e)
- {
- if (PauseWatchMouse) return;
- CheckMouseState();
- }
- /// <summary>
- /// 检测鼠标状态
- /// </summary>
- private void CheckMouseState()
- {
- try
- {
- Rect rect = GetRect();
- WinAPI.GetCursorPos(out WindowsApi.Point currentPoint);
- if (rect.Contains(new System.Windows.Point(currentPoint.X, currentPoint.Y)))
- {
- IntPtr currentIntPtr = WinAPI.WindowFromPoint(currentPoint);
- if (Handle.Equals(currentIntPtr))
- {
- if (!m_IsMouseIn)
- {
- m_IsMouseIn = true;
- OnMouseEnterT();
- }
- }
- }
- else
- {
- if (m_IsMouseIn)
- {
- m_IsMouseIn = false;
- OnMouseLeaveT();
- }
- }
- }
- catch (Exception excpetion)
- {
- throw excpetion;
- }
- }
- protected Rect GetRect()
- {
- Rect rect = Dispatcher.Invoke(new Func<Rect>(() =>
- {
- Rect tempRect = new Rect(new System.Windows.Point(Left, Top), new Size(ActualWidth, ActualHeight));
- return tempRect;
- }));
- return rect;
- }
- protected void RaiseOnMouseLeaveT()
- {
- try
- {
- var rect = GetRect();
- WinAPI.GetCursorPos(out WindowsApi.Point currentPoint);
- if (!rect.Contains(new System.Windows.Point(currentPoint.X, currentPoint.Y)))
- {
- OnMouseLeaveT();
- }
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- public event EventHandler MouseEnterT;
- public event EventHandler MouseLeaveT;
- protected virtual void OnMouseEnterT()
- {
- MouseEnterT?.Invoke(this, EventArgs.Empty);
- }
- protected virtual void OnMouseLeaveT()
- {
- MouseLeaveT?.Invoke(this, EventArgs.Empty);
- }
- #endregion
- }
- }
|