/*-------------------------------------------------------------------------
* 功能描述: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 基础信息加载
///
/// 当前窗体的窗口句柄
///
protected IntPtr Handle { get; private set; }
///
/// 加载图标
///
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("图标加载失败");
}
}
///
/// 设置本窗口的Owner,并返回本窗体handle
///
///
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 鼠标位置监控
///
/// 是否暂停监视鼠标移入移出
///
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();
}
///
/// 检测鼠标状态
///
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 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
}
}