WinBase.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Runtime.InteropServices;
  6. using System.Timers;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Interop;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using SAGA.DotNetUtils;
  13. using SAGA.DotNetUtils.Extend;
  14. using SAGA.DotNetUtils.Hook;
  15. using SAGA.RevitUtils.Extends;
  16. using SAGA.RevitUtils.RevitDraw;
  17. using Point = System.Windows.Point;
  18. using Size = System.Windows.Size;
  19. namespace SAGA.RevitUtils.Windows
  20. {
  21. /// <summary>
  22. /// 所有窗体基类
  23. /// </summary>
  24. public class WinBase : Window
  25. {
  26. [DllImport("user32.dll")]
  27. protected static extern IntPtr WindowFromPoint(Point point);
  28. protected bool m_IsEnableEscHook = false;
  29. public bool IsLoadHistroyData { get; set; }
  30. /// <summary>
  31. /// 当前窗体的窗口句柄
  32. /// </summary>
  33. private IntPtr m_CurWindowHandle;
  34. public WinBase()
  35. {
  36. IsLoadHistroyData = true;
  37. string path = Path.Combine(AppBaseInfo.ImagePath, "sagacloud.ico");
  38. try
  39. {
  40. Icon = new BitmapImage(new Uri(path, UriKind.RelativeOrAbsolute));
  41. }
  42. catch (Exception ex)
  43. {
  44. throw ex;
  45. }
  46. // this.Topmost = true;
  47. //ResizeMode = ResizeMode.NoResize;
  48. ShowInTaskbar = false;
  49. Title = "命令窗体";
  50. SetWindowOwner();
  51. Loaded += OnLoaded;
  52. // this.LocationChanged += WinBase_LocationChanged;
  53. }
  54. protected KeyBordHook m_Hook = null;
  55. protected virtual void OnLoaded(object sender, RoutedEventArgs e)
  56. {
  57. WindowInteropHelper winHelper = new WindowInteropHelper(this);
  58. this.CurWindowHandle = winHelper.Handle;
  59. //由于窗体经常跑到屏幕外面,取消保存历史记录 mxg 20181030
  60. var datas = new List<SaveData>();
  61. if (IsLoadHistroyData) this.SetControlData(out datas);
  62. //SetWindowPosition(datas);
  63. if (Resources != null)
  64. {
  65. //// Uri uri = new Uri(@"pack://application:,,,/TSZ.RevitBaseDll;component/BaseStyle.xaml",
  66. // UriKind.RelativeOrAbsolute);
  67. // Resources.MergedDictionaries.Add(new ResourceDictionary() {Source = uri});
  68. }
  69. //ResourceDictionary resource = new ResourceDictionary();
  70. //Uri uri = new Uri(@"pack://application:,,,/TSZ.RevitBaseDll.WPF.DotNetUtils;component/BaseStyle.xaml", UriKind.RelativeOrAbsolute);
  71. //resource.Source = uri;
  72. //this.Resources = resource;
  73. if (this.m_IsEnableEscHook)
  74. {
  75. IntPtr handle = RevitDrawArea.GetDrawArea(ExternalDataWrapper.Current.Doc.ActiveView.Name);
  76. m_Hook = new KeyBordHook(KeyBordHookFlags.Keybord, handle);
  77. m_Hook.KeyUp += m_Hook_KeyUp;
  78. this.Closing += WinBase_Closing;
  79. m_Hook.Start();
  80. }
  81. SetBtnRenderOptions(this);
  82. }
  83. void WinBase_Closing(object sender, System.ComponentModel.CancelEventArgs e)
  84. {
  85. if (m_Hook != null)
  86. {
  87. m_Hook.Stop();
  88. }
  89. }
  90. void m_Hook_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
  91. {
  92. if (e.KeyValue == 27)
  93. {
  94. Close();
  95. }
  96. }
  97. #region 位置相关设定
  98. private int m_RevitLeft;
  99. public int RevitLeft
  100. {
  101. get { return m_RevitLeft; }
  102. }
  103. private int m_RevitRight;
  104. public int RevitRight
  105. {
  106. get { return m_RevitRight; }
  107. }
  108. private int m_RevitTop = 20;
  109. public int RevitTop
  110. {
  111. get { return m_RevitTop; }
  112. }
  113. private int m_RevitBottom = 20;
  114. public int RevitBottom
  115. {
  116. get { return m_RevitBottom; }
  117. }
  118. #endregion
  119. void LoadPosition()
  120. {
  121. m_RevitLeft = RevitHelper.RevitLeftPoint;
  122. m_RevitRight = RevitHelper.RevitRightPoint;
  123. m_RevitTop = RevitHelper.RevitTopPoint;
  124. m_RevitBottom = RevitHelper.RevitBottomPoint;
  125. }
  126. /// <summary>
  127. /// 设置所属父窗体
  128. /// </summary>
  129. protected void SetWindowOwner()
  130. {
  131. var winHelp = new WindowInteropHelper(this);
  132. WindowHelper.SetWindowForeground(winHelp.EnsureHandle());
  133. winHelp.Owner = WindowHelper.HWndRevit.Handle;
  134. }
  135. /// <summary>
  136. /// 重写Window关闭功能
  137. /// </summary>
  138. /// <param name="e"></param>
  139. protected override void OnClosed(EventArgs e)
  140. {
  141. StopTimer();
  142. base.OnClosed(e);
  143. var datas = new List<SaveData>() { GetLocationSaveData() };
  144. this.SaveControlData(datas);
  145. }
  146. /// <summary>
  147. /// 隐藏基类Show方法
  148. /// </summary>
  149. public new void Show()
  150. {
  151. base.Show();
  152. }
  153. /// <summary>
  154. /// 设置窗体位置 mxg 2016-3-25
  155. /// a,窗体与Revit同屏时保存窗体位置下次直接使用
  156. /// b,不在同屏时设置位置为右上角
  157. /// </summary>
  158. /// <param name="datas"></param>
  159. protected void SetWindowPosition(List<SaveData> datas)
  160. {
  161. LoadPosition();
  162. string location = "";
  163. if (datas != null)
  164. {
  165. var savedata = datas.FirstOrDefault(t => t.ControlType.Equals("Form"));
  166. if (savedata != null)
  167. location = savedata.ControlValue;
  168. }
  169. if (location.IsNullOrEmptyExt())
  170. {
  171. SetDefaultPosition();
  172. return;
  173. }
  174. var locations = location.Split(',');
  175. if (locations.Count() != 2)
  176. {
  177. SetDefaultPosition();
  178. return;
  179. }
  180. Top = locations[0].ToInt();
  181. Left = locations[1].ToInt();
  182. }
  183. /// <summary>
  184. /// 设置窗体的默认位置
  185. /// mxg 2016-3-25
  186. /// </summary>
  187. protected virtual void SetDefaultPosition()
  188. {
  189. Top = RevitTop + (RevitBottom - RevitTop - Height) / 2;
  190. Left = RevitLeft + (RevitRight - RevitLeft - Width) / 2;
  191. }
  192. /// <summary>
  193. /// 保存窗体位置
  194. /// mxg 2016-3-25
  195. /// </summary>
  196. /// <returns></returns>
  197. private SaveData GetLocationSaveData()
  198. {
  199. string value = Top.ToString() + "," + Left.ToString();
  200. return new SaveData("", "Form", "Location", value);
  201. }
  202. #region 维护监视鼠标
  203. /// <summary>
  204. /// 是否暂停监视鼠标移入移出
  205. /// </summary>
  206. public bool PauseWatchMouse { get; set; }
  207. /// <summary>
  208. /// 当前窗体的窗口句柄
  209. /// </summary>
  210. protected IntPtr CurWindowHandle
  211. {
  212. get { return m_CurWindowHandle; }
  213. set { m_CurWindowHandle = value; }
  214. }
  215. protected Timer m_Timer;
  216. private bool m_IsMouseEnter;
  217. //打开监视
  218. protected void StartEnterLeaveT()
  219. {
  220. try
  221. {
  222. StartTimer();
  223. Rect rect = new Rect(new Point(Left, Top), new Size(ActualWidth, ActualHeight));
  224. IntPtr intPtr = WindowFromPoint(WindowHelper.GetMousePoint());
  225. m_IsMouseEnter = !rect.Contains(WindowHelper.GetMousePoint()); //将状态取反是为了开始时能够触发事件
  226. bool isSameWindow = intPtr.Equals(CurWindowHandle);
  227. m_IsMouseEnter = m_IsMouseEnter && isSameWindow;
  228. }
  229. catch (Exception ex)
  230. {
  231. throw ex;
  232. }
  233. }
  234. //关闭监视
  235. protected void StopEnterLeaveT()
  236. {
  237. StopTimer();
  238. }
  239. private void StartTimer()
  240. {
  241. try
  242. {
  243. if (m_Timer == null)
  244. m_Timer = new Timer(5);
  245. if (!m_Timer.Enabled)
  246. {
  247. m_Timer.Interval = 5;
  248. m_Timer.Elapsed += m_Timer_Elapsed;
  249. m_Timer.Start();
  250. }
  251. }
  252. catch (Exception ex)
  253. {
  254. throw ex;
  255. }
  256. }
  257. private void StopTimer()
  258. {
  259. try
  260. {
  261. if (m_Timer != null)
  262. {
  263. if (m_Timer.Enabled)
  264. m_Timer.Stop();
  265. m_Timer.Dispose();
  266. m_Timer = null;
  267. }
  268. }
  269. catch (Exception ex)
  270. {
  271. throw ex;
  272. }
  273. }
  274. private void m_Timer_Elapsed(object sender, ElapsedEventArgs e)
  275. {
  276. try
  277. {
  278. if (PauseWatchMouse) return;
  279. Rect rect = Dispatcher.Invoke(new Func<Rect>(() =>
  280. {
  281. Rect tempRect = new Rect(new Point(Left, Top), new Size(ActualWidth, ActualHeight));
  282. return tempRect;
  283. }));
  284. if (rect.Contains(WindowHelper.GetMousePoint()))
  285. {
  286. IntPtr intPtr = WindowFromPoint(WindowHelper.GetMousePoint());
  287. if (CurWindowHandle.Equals(intPtr))
  288. {
  289. if (!m_IsMouseEnter)
  290. {
  291. m_IsMouseEnter = true;
  292. OnMouseEnterT();
  293. }
  294. }
  295. }
  296. else
  297. {
  298. if (m_IsMouseEnter)
  299. {
  300. m_IsMouseEnter = false;
  301. OnMouseLeaveT();
  302. }
  303. }
  304. }
  305. catch (Exception excpetion)
  306. {
  307. //子线成命令自己处理
  308. throw excpetion;
  309. }
  310. }
  311. protected void CheckIfNeedRaiseOnMouseLeaveT()
  312. {
  313. //Rect rect = new Rect(new Point(Left, Top), new Size(ActualWidth, ActualHeight));
  314. try
  315. {
  316. Rect rect = Dispatcher.Invoke(new Func<Rect>(() =>
  317. {
  318. Rect tempRect = new Rect(new Point(Left, Top), new Size(ActualWidth, ActualHeight));
  319. return tempRect;
  320. }));
  321. if (!rect.Contains(WindowHelper.GetMousePoint()))
  322. {
  323. OnMouseLeaveT();
  324. }
  325. }
  326. catch (Exception ex)
  327. {
  328. //子线成命令自己处理
  329. throw ex;
  330. }
  331. }
  332. public event EventHandler MouseEnterT;
  333. public event EventHandler MouseLeaveT;
  334. protected virtual void OnMouseEnterT()
  335. {
  336. if (MouseEnterT != null)
  337. MouseEnterT(this, EventArgs.Empty);
  338. }
  339. protected virtual void OnMouseLeaveT()
  340. {
  341. if (MouseLeaveT != null)
  342. MouseLeaveT(this, EventArgs.Empty);
  343. }
  344. #endregion
  345. /// <summary>
  346. /// 设置控件的公共样式信息
  347. /// wzc 2017-08-16
  348. /// </summary>
  349. /// <param name="visual"></param>
  350. private void SetBtnRenderOptions(Visual visual)
  351. {
  352. for (int i = 0; i < VisualTreeHelper.GetChildrenCount(visual); i++)
  353. {
  354. Visual childVisual = (Visual)VisualTreeHelper.GetChild(visual, i);
  355. if (childVisual != null)
  356. {
  357. if (childVisual is Button)
  358. {
  359. var btn = childVisual as Button;
  360. RenderOptions.SetBitmapScalingMode(btn, BitmapScalingMode.NearestNeighbor);
  361. }
  362. if (childVisual is DataGrid)
  363. {
  364. // var dg = childVisual as DataGrid;
  365. // Color color = Color.FromArgb(255, 192, 192, 192);
  366. // SolidColorBrush brush = new SolidColorBrush(color);
  367. // dg.HorizontalGridLinesBrush = brush;
  368. // dg.VerticalGridLinesBrush = brush;
  369. }
  370. SetBtnRenderOptions(childVisual);
  371. }
  372. }
  373. }
  374. }
  375. }