MainWindow.xaml.cs 11 KB

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