MainWindow.xaml.cs 10 KB

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