123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433 |
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Timers;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Interop;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using SAGA.DotNetUtils;
- using SAGA.DotNetUtils.Extend;
- using SAGA.DotNetUtils.Hook;
- using SAGA.RevitUtils.Extends;
- using SAGA.RevitUtils.RevitDraw;
- using Point = System.Windows.Point;
- using Size = System.Windows.Size;
- namespace SAGA.RevitUtils.Windows
- {
- /// <summary>
- /// 所有窗体基类
- /// </summary>
- public class WinBase : Window
- {
- [DllImport("user32.dll")]
- protected static extern IntPtr WindowFromPoint(Point point);
- protected bool m_IsEnableEscHook = false;
- public bool IsLoadHistroyData { get; set; }
- /// <summary>
- /// 当前窗体的窗口句柄
- /// </summary>
- private IntPtr m_CurWindowHandle;
- public WinBase()
- {
- IsLoadHistroyData = true;
- string path = Path.Combine(AppBaseInfo.ImagePath, "sagacloud.ico");
- try
- {
- Icon = new BitmapImage(new Uri(path, UriKind.RelativeOrAbsolute));
- }
- catch (Exception ex)
- {
- throw ex;
- }
- // this.Topmost = true;
- //ResizeMode = ResizeMode.NoResize;
- ShowInTaskbar = false;
- Title = "命令窗体";
- SetWindowOwner();
- Loaded += OnLoaded;
- // this.LocationChanged += WinBase_LocationChanged;
- }
- protected KeyBordHook m_Hook = null;
- protected virtual void OnLoaded(object sender, RoutedEventArgs e)
- {
- WindowInteropHelper winHelper = new WindowInteropHelper(this);
- this.CurWindowHandle = winHelper.Handle;
- //由于窗体经常跑到屏幕外面,取消保存历史记录 mxg 20181030
- var datas = new List<SaveData>();
- if (IsLoadHistroyData) this.SetControlData(out datas);
- //SetWindowPosition(datas);
- if (Resources != null)
- {
- //// Uri uri = new Uri(@"pack://application:,,,/TSZ.RevitBaseDll;component/BaseStyle.xaml",
- // UriKind.RelativeOrAbsolute);
- // Resources.MergedDictionaries.Add(new ResourceDictionary() {Source = uri});
- }
- //ResourceDictionary resource = new ResourceDictionary();
- //Uri uri = new Uri(@"pack://application:,,,/TSZ.RevitBaseDll.WPF.DotNetUtils;component/BaseStyle.xaml", UriKind.RelativeOrAbsolute);
- //resource.Source = uri;
- //this.Resources = resource;
- if (this.m_IsEnableEscHook)
- {
- IntPtr handle = RevitDrawArea.GetDrawArea(ExternalDataWrapper.Current.Doc.ActiveView.Name);
- m_Hook = new KeyBordHook(KeyBordHookFlags.Keybord, handle);
- m_Hook.KeyUp += m_Hook_KeyUp;
- this.Closing += WinBase_Closing;
- m_Hook.Start();
- }
- SetBtnRenderOptions(this);
- }
- void WinBase_Closing(object sender, System.ComponentModel.CancelEventArgs e)
- {
- if (m_Hook != null)
- {
- m_Hook.Stop();
- }
- }
- void m_Hook_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
- {
- if (e.KeyValue == 27)
- {
- Close();
- }
- }
- #region 位置相关设定
- private int m_RevitLeft;
- public int RevitLeft
- {
- get { return m_RevitLeft; }
- }
- private int m_RevitRight;
- public int RevitRight
- {
- get { return m_RevitRight; }
- }
- private int m_RevitTop = 20;
- public int RevitTop
- {
- get { return m_RevitTop; }
- }
- private int m_RevitBottom = 20;
- public int RevitBottom
- {
- get { return m_RevitBottom; }
- }
- #endregion
- void LoadPosition()
- {
- m_RevitLeft = RevitHelper.RevitLeftPoint;
- m_RevitRight = RevitHelper.RevitRightPoint;
- m_RevitTop = RevitHelper.RevitTopPoint;
- m_RevitBottom = RevitHelper.RevitBottomPoint;
- }
- /// <summary>
- /// 设置所属父窗体
- /// </summary>
- protected void SetWindowOwner()
- {
- var winHelp = new WindowInteropHelper(this);
- WindowHelper.SetWindowForeground(winHelp.EnsureHandle());
- winHelp.Owner = WindowHelper.HWndRevit.Handle;
- }
- /// <summary>
- /// 重写Window关闭功能
- /// </summary>
- /// <param name="e"></param>
- protected override void OnClosed(EventArgs e)
- {
- StopTimer();
- base.OnClosed(e);
- var datas = new List<SaveData>() { GetLocationSaveData() };
- this.SaveControlData(datas);
- }
- /// <summary>
- /// 隐藏基类Show方法
- /// </summary>
- public new void Show()
- {
- base.Show();
- }
- /// <summary>
- /// 设置窗体位置 mxg 2016-3-25
- /// a,窗体与Revit同屏时保存窗体位置下次直接使用
- /// b,不在同屏时设置位置为右上角
- /// </summary>
- /// <param name="datas"></param>
- protected void SetWindowPosition(List<SaveData> datas)
- {
- LoadPosition();
- string location = "";
- if (datas != null)
- {
- var savedata = datas.FirstOrDefault(t => t.ControlType.Equals("Form"));
- if (savedata != null)
- location = savedata.ControlValue;
- }
- if (location.IsNullOrEmptyExt())
- {
- SetDefaultPosition();
- return;
- }
- var locations = location.Split(',');
- if (locations.Count() != 2)
- {
- SetDefaultPosition();
- return;
- }
- Top = locations[0].ToInt();
- Left = locations[1].ToInt();
- }
- /// <summary>
- /// 设置窗体的默认位置
- /// mxg 2016-3-25
- /// </summary>
- protected virtual void SetDefaultPosition()
- {
- Top = RevitTop + (RevitBottom - RevitTop - Height) / 2;
- Left = RevitLeft + (RevitRight - RevitLeft - Width) / 2;
- }
- /// <summary>
- /// 保存窗体位置
- /// mxg 2016-3-25
- /// </summary>
- /// <returns></returns>
- private SaveData GetLocationSaveData()
- {
- string value = Top.ToString() + "," + Left.ToString();
- return new SaveData("", "Form", "Location", value);
- }
- #region 维护监视鼠标
- /// <summary>
- /// 是否暂停监视鼠标移入移出
- /// </summary>
- public bool PauseWatchMouse { get; set; }
- /// <summary>
- /// 当前窗体的窗口句柄
- /// </summary>
- protected IntPtr CurWindowHandle
- {
- get { return m_CurWindowHandle; }
- set { m_CurWindowHandle = value; }
- }
- protected Timer m_Timer;
- private bool m_IsMouseEnter;
- //打开监视
- protected void StartEnterLeaveT()
- {
- try
- {
- StartTimer();
- Rect rect = new Rect(new Point(Left, Top), new Size(ActualWidth, ActualHeight));
- IntPtr intPtr = WindowFromPoint(WindowHelper.GetMousePoint());
- m_IsMouseEnter = !rect.Contains(WindowHelper.GetMousePoint()); //将状态取反是为了开始时能够触发事件
- bool isSameWindow = intPtr.Equals(CurWindowHandle);
- m_IsMouseEnter = m_IsMouseEnter && 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.Interval = 5;
- 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)
- {
- try
- {
- if (PauseWatchMouse) return;
- Rect rect = Dispatcher.Invoke(new Func<Rect>(() =>
- {
- Rect tempRect = new Rect(new Point(Left, Top), new Size(ActualWidth, ActualHeight));
- return tempRect;
- }));
- if (rect.Contains(WindowHelper.GetMousePoint()))
- {
- IntPtr intPtr = WindowFromPoint(WindowHelper.GetMousePoint());
- if (CurWindowHandle.Equals(intPtr))
- {
- if (!m_IsMouseEnter)
- {
- m_IsMouseEnter = true;
- OnMouseEnterT();
- }
- }
- }
- else
- {
- if (m_IsMouseEnter)
- {
- m_IsMouseEnter = false;
- OnMouseLeaveT();
- }
- }
- }
- catch (Exception excpetion)
- {
- //子线成命令自己处理
- throw excpetion;
- }
- }
- protected void CheckIfNeedRaiseOnMouseLeaveT()
- {
- //Rect rect = new Rect(new Point(Left, Top), new Size(ActualWidth, ActualHeight));
- try
- {
- Rect rect = Dispatcher.Invoke(new Func<Rect>(() =>
- {
- Rect tempRect = new Rect(new Point(Left, Top), new Size(ActualWidth, ActualHeight));
- return tempRect;
- }));
- if (!rect.Contains(WindowHelper.GetMousePoint()))
- {
- OnMouseLeaveT();
- }
- }
- catch (Exception ex)
- {
- //子线成命令自己处理
- throw ex;
- }
- }
- public event EventHandler MouseEnterT;
- public event EventHandler MouseLeaveT;
- protected virtual void OnMouseEnterT()
- {
- if (MouseEnterT != null)
- MouseEnterT(this, EventArgs.Empty);
- }
- protected virtual void OnMouseLeaveT()
- {
- if (MouseLeaveT != null)
- MouseLeaveT(this, EventArgs.Empty);
- }
- #endregion
- /// <summary>
- /// 设置控件的公共样式信息
- /// wzc 2017-08-16
- /// </summary>
- /// <param name="visual"></param>
- private void SetBtnRenderOptions(Visual visual)
- {
- for (int i = 0; i < VisualTreeHelper.GetChildrenCount(visual); i++)
- {
- Visual childVisual = (Visual)VisualTreeHelper.GetChild(visual, i);
- if (childVisual != null)
- {
- if (childVisual is Button)
- {
- var btn = childVisual as Button;
- RenderOptions.SetBitmapScalingMode(btn, BitmapScalingMode.NearestNeighbor);
- }
- if (childVisual is DataGrid)
- {
- // var dg = childVisual as DataGrid;
- // Color color = Color.FromArgb(255, 192, 192, 192);
- // SolidColorBrush brush = new SolidColorBrush(color);
- // dg.HorizontalGridLinesBrush = brush;
- // dg.VerticalGridLinesBrush = brush;
- }
- SetBtnRenderOptions(childVisual);
- }
- }
- }
- }
- }
|