MainWindow.xaml.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. using DraggAnimatedPanelExample;
  2. using GeekDesk.Constant;
  3. using GeekDesk.Control;
  4. using GeekDesk.Util;
  5. using GeekDesk.ViewModel;
  6. using GlobalHotKey;
  7. using SharpShell.SharpContextMenu;
  8. using System;
  9. using System.Collections.ObjectModel;
  10. using System.Diagnostics;
  11. using System.Drawing;
  12. using System.IO;
  13. using System.Windows;
  14. using System.Windows.Controls;
  15. using System.Windows.Input;
  16. using System.Windows.Interop;
  17. using System.Windows.Media;
  18. namespace GeekDesk
  19. {
  20. /// <summary>
  21. /// MainWindow.xaml 的交互逻辑
  22. /// </summary>
  23. ///
  24. public partial class MainWindow : Window
  25. {
  26. public static AppData appData = CommonCode.GetAppDataByFile();
  27. public MainWindow()
  28. {
  29. LoadData();
  30. InitializeComponent();
  31. this.Topmost = true;
  32. this.Loaded += Window_Loaded;
  33. this.SizeChanged += MainWindow_Resize;
  34. }
  35. private void LoadData()
  36. {
  37. this.DataContext = appData;
  38. if (appData.MenuList.Count == 0)
  39. {
  40. appData.MenuList.Add(new MenuInfo() { MenuName = "NewMenu", MenuId = System.Guid.NewGuid().ToString(), MenuEdit = Visibility.Collapsed});
  41. }
  42. this.Width = appData.AppConfig.WindowWidth;
  43. this.Height = appData.AppConfig.WindowHeight;
  44. }
  45. void Window_Loaded(object sender, RoutedEventArgs e)
  46. {
  47. if (!appData.AppConfig.StartedShowPanel)
  48. {
  49. this.Visibility = Visibility.Collapsed;
  50. }
  51. //加载完毕注册热键
  52. Hotkey.Regist(this, HotkeyModifiers.MOD_CONTROL | HotkeyModifiers.MOD_ALT, Key.Y, ()=>
  53. {
  54. if (this.Visibility == Visibility.Collapsed)
  55. {
  56. ShowApp();
  57. } else
  58. {
  59. this.Visibility = Visibility.Collapsed;
  60. }
  61. });
  62. }
  63. void MainWindow_Resize(object sender, System.EventArgs e)
  64. {
  65. if (this.DataContext != null)
  66. {
  67. AppData appData = this.DataContext as AppData;
  68. appData.AppConfig.WindowWidth = this.Width;
  69. appData.AppConfig.WindowHeight = this.Height;
  70. }
  71. }
  72. private void DragMove(object sender, MouseEventArgs e)
  73. {
  74. //if (e.LeftButton == MouseButtonState.Pressed)
  75. //{
  76. // this.DragMove();
  77. //}
  78. if (e.LeftButton == MouseButtonState.Pressed)
  79. {
  80. var windowMode = this.ResizeMode;
  81. if (this.ResizeMode != ResizeMode.NoResize)
  82. {
  83. this.ResizeMode = ResizeMode.NoResize;
  84. }
  85. this.UpdateLayout();
  86. /* 当点击拖拽区域的时候,让窗口跟着移动
  87. (When clicking the drag area, make the window follow) */
  88. DragMove();
  89. if (this.ResizeMode != windowMode)
  90. {
  91. this.ResizeMode = windowMode;
  92. }
  93. this.UpdateLayout();
  94. }
  95. }
  96. /// <summary>
  97. /// 关闭按钮单击事件
  98. /// </summary>
  99. /// <param name="sender"></param>
  100. /// <param name="e"></param>
  101. private void CloseButtonClick(object sender, RoutedEventArgs e)
  102. {
  103. this.Visibility = Visibility.Collapsed;
  104. }
  105. ///// <summary>
  106. ///// 左侧栏宽度改变 持久化
  107. ///// </summary>
  108. ///// <param name="sender"></param>
  109. ///// <param name="e"></param>
  110. //private void LeftCardResize(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e)
  111. //{
  112. // appData.AppConfig.MenuCardWidth = LeftColumn.Width.Value;
  113. //}
  114. /// <summary>
  115. /// 右键任务栏图标 显示主面板
  116. /// </summary>
  117. /// <param name="sender"></param>
  118. /// <param name="e"></param>
  119. private void ShowApp(object sender, RoutedEventArgs e)
  120. {
  121. ShowApp();
  122. }
  123. private void ShowApp()
  124. {
  125. if (appData.AppConfig.FollowMouse)
  126. {
  127. ShowAppAndFollowMouse();
  128. } else
  129. {
  130. this.Visibility = Visibility.Visible;
  131. }
  132. Keyboard.Focus(this);
  133. }
  134. /// <summary>
  135. /// 随鼠标位置显示面板 (鼠标始终在中间)
  136. /// </summary>
  137. private void ShowAppAndFollowMouse()
  138. {
  139. //获取鼠标位置
  140. System.Windows.Point p = MouseUtil.GetMousePosition();
  141. double left = SystemParameters.VirtualScreenLeft;
  142. double top = SystemParameters.VirtualScreenTop;
  143. double width = SystemParameters.VirtualScreenWidth;
  144. double height = SystemParameters.VirtualScreenHeight;
  145. double right = width - Math.Abs(left);
  146. double bottom = height - Math.Abs(top);
  147. if (p.X - this.Width / 2 < left)
  148. {
  149. //判断是否在最左边缘
  150. this.Left = left;
  151. }
  152. else if (p.X + this.Width / 2 > right)
  153. {
  154. //判断是否在最右边缘
  155. this.Left = right - this.Width;
  156. }
  157. else
  158. {
  159. this.Left = p.X - this.Width / 2;
  160. }
  161. if (p.Y - this.Height / 2 < top)
  162. {
  163. //判断是否在最上边缘
  164. this.Top = top;
  165. }
  166. else if (p.Y + this.Height / 2 > bottom)
  167. {
  168. //判断是否在最下边缘
  169. this.Top = bottom - this.Height;
  170. }
  171. else
  172. {
  173. this.Top = p.Y - this.Height / 2;
  174. }
  175. this.Visibility = Visibility.Visible;
  176. }
  177. /// <summary>
  178. /// 图片图标单击事件
  179. /// </summary>
  180. /// <param name="sender"></param>
  181. /// <param name="e"></param>
  182. private void NotifyIcon_Click(object sender, RoutedEventArgs e)
  183. {
  184. if (this.Visibility == Visibility.Collapsed)
  185. {
  186. ShowApp();
  187. }
  188. else
  189. {
  190. this.Visibility = Visibility.Collapsed;
  191. }
  192. }
  193. /// <summary>
  194. /// 右键任务栏图标 设置
  195. /// </summary>
  196. /// <param name="sender"></param>
  197. /// <param name="e"></param>
  198. private void ConfigApp(object sender, RoutedEventArgs e)
  199. {
  200. //MenuInfo menuInfo = ((MenuItem)sender).Tag as MenuInfo;
  201. //appData.MenuList.Remove(menuInfo);
  202. //CommonCode.SaveAppData(appData);
  203. }
  204. /// <summary>
  205. /// 右键任务栏图标退出
  206. /// </summary>
  207. /// <param name="sender"></param>
  208. /// <param name="e"></param>
  209. private void ExitApp(object sender, RoutedEventArgs e)
  210. {
  211. Application.Current.Shutdown();
  212. }
  213. //public static void ShowContextMenu(IntPtr hAppWnd, Window taskBar, System.Windows.Point pt)
  214. //{
  215. // WindowInteropHelper helper = new WindowInteropHelper(taskBar);
  216. // IntPtr callingTaskBarWindow = helper.Handle;
  217. // IntPtr wMenu = GetSystemMenu(hAppWnd, false);
  218. // // Display the menu
  219. // uint command = TrackPopupMenuEx(wMenu, TPM.LEFTBUTTON | TPM.RETURNCMD, (int) pt.X, (int) pt.Y, callingTaskBarWindow, IntPtr.Zero);
  220. // if (command == 0) return;
  221. // PostMessage(hAppWnd, WM.SYSCOMMAND, new IntPtr(command), IntPtr.Zero);
  222. //}
  223. /// <summary>
  224. /// 设置按钮左键弹出菜单
  225. /// </summary>
  226. /// <param name="sender"></param>
  227. /// <param name="e"></param>
  228. private void ConfigButtonClick(object sender, RoutedEventArgs e)
  229. {
  230. //SettingMenu.IsOpen = true;
  231. new ConfigWindow(appData.AppConfig, this).Show();
  232. }
  233. /// <summary>
  234. /// 禁用设置按钮右键菜单
  235. /// </summary>
  236. /// <param name="sender"></param>
  237. /// <param name="e"></param>
  238. private void SettingButton_Initialized(object sender, EventArgs e)
  239. {
  240. SettingButton.ContextMenu = null;
  241. }
  242. private void App_LostFocus(object sender, RoutedEventArgs e)
  243. {
  244. if (appData.AppConfig.AppHideType == AppHideType.LOST_FOCUS)
  245. {
  246. this.Visibility = Visibility.Collapsed;
  247. }
  248. }
  249. private void window_Deactivated(object sender, EventArgs e)
  250. {
  251. if (appData.AppConfig.AppHideType == AppHideType.LOST_FOCUS)
  252. {
  253. this.Visibility = Visibility.Collapsed;
  254. }
  255. }
  256. }
  257. }