MainWindow.xaml.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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.Thread;
  8. using GeekDesk.Util;
  9. using GeekDesk.ViewModel;
  10. using HandyControl.Data;
  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. using static GeekDesk.Util.ShowWindowFollowMouse;
  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 ToDoInfoWindow toDoInfoWindow = (ToDoInfoWindow)ToDoInfoWindow.GetThis();
  32. public static ToDoInfoWindow toDoInfoWindow;
  33. public static int hotKeyId = -1;
  34. public static int toDoHotKeyId = -1;
  35. public static MainWindow mainWindow;
  36. public MainWindow()
  37. {
  38. LoadData();
  39. InitializeComponent();
  40. mainWindow = this;
  41. this.Topmost = true;
  42. this.Loaded += Window_Loaded;
  43. this.SizeChanged += MainWindow_Resize;
  44. ToDoTask.BackLogCheck();
  45. }
  46. private void LoadData()
  47. {
  48. this.DataContext = appData;
  49. if (appData.MenuList.Count == 0)
  50. {
  51. appData.MenuList.Add(new MenuInfo() { MenuName = "NewMenu", MenuId = System.Guid.NewGuid().ToString(), MenuEdit = Visibility.Collapsed});
  52. }
  53. this.Width = appData.AppConfig.WindowWidth;
  54. this.Height = appData.AppConfig.WindowHeight;
  55. }
  56. void Window_Loaded(object sender, RoutedEventArgs e)
  57. {
  58. if (!appData.AppConfig.StartedShowPanel)
  59. {
  60. this.Visibility = Visibility.Collapsed;
  61. } else
  62. {
  63. ShowApp();
  64. }
  65. RegisterHotKey(true);
  66. //RegisterCreateToDoHotKey(true);
  67. if (!appData.AppConfig.SelfStartUped)
  68. {
  69. RegisterUtil.SetSelfStarting(appData.AppConfig.SelfStartUp, Constants.MY_NAME);
  70. }
  71. UpdateThread.Update();
  72. }
  73. /// <summary>
  74. /// 注册当前窗口的热键
  75. /// </summary>
  76. public static void RegisterHotKey(bool first)
  77. {
  78. try
  79. {
  80. if (appData.AppConfig.HotkeyModifiers != 0)
  81. {
  82. //加载完毕注册热键
  83. hotKeyId = Hotkey.Regist(new WindowInteropHelper(MainWindow.mainWindow).Handle, appData.AppConfig.HotkeyModifiers, appData.AppConfig.Hotkey, () =>
  84. {
  85. if (MotionControl.hotkeyFinished)
  86. {
  87. if (mainWindow.Visibility == Visibility.Collapsed)
  88. {
  89. ShowApp();
  90. }
  91. else
  92. {
  93. mainWindow.Visibility = Visibility.Collapsed;
  94. }
  95. }
  96. });
  97. }
  98. if (!first)
  99. {
  100. HandyControl.Controls.Growl.Success("GeekDesk快捷键注册成功(" + appData.AppConfig.HotkeyStr + ")!", "HotKeyGrowl");
  101. }
  102. }
  103. catch (Exception)
  104. {
  105. if (first)
  106. {
  107. HandyControl.Controls.Growl.WarningGlobal("GeekDesk启动快捷键已被其它程序占用(" + appData.AppConfig.HotkeyStr + ")!");
  108. }
  109. else
  110. {
  111. HandyControl.Controls.Growl.Warning("GeekDesk启动快捷键已被其它程序占用(" + appData.AppConfig.HotkeyStr + ")!", "HotKeyGrowl");
  112. }
  113. }
  114. }
  115. /// <summary>
  116. /// 注册新建待办的热键
  117. /// </summary>
  118. public static void RegisterCreateToDoHotKey(bool first)
  119. {
  120. try
  121. {
  122. if (appData.AppConfig.ToDoHotkeyModifiers!=0)
  123. {
  124. //加载完毕注册热键
  125. toDoHotKeyId = Hotkey.Regist(new WindowInteropHelper(MainWindow.mainWindow).Handle, appData.AppConfig.ToDoHotkeyModifiers, appData.AppConfig.ToDoHotkey, () =>
  126. {
  127. if (MotionControl.hotkeyFinished)
  128. {
  129. ToDoInfoWindow.ShowNone();
  130. }
  131. });
  132. }
  133. if (!first)
  134. {
  135. HandyControl.Controls.Growl.Success("新建待办任务快捷键注册成功(" + appData.AppConfig.ToDoHotkeyStr + ")!", "HotKeyGrowl");
  136. }
  137. }
  138. catch (Exception)
  139. {
  140. if (first)
  141. {
  142. HandyControl.Controls.Growl.WarningGlobal("新建待办任务快捷键已被其它程序占用(" + appData.AppConfig.ToDoHotkeyStr + ")!");
  143. }
  144. else
  145. {
  146. HandyControl.Controls.Growl.Warning("新建待办任务快捷键已被其它程序占用(" + appData.AppConfig.ToDoHotkeyStr + ")!", "HotKeyGrowl");
  147. }
  148. }
  149. }
  150. //private void DisplayWindowHotKeyPress(object sender, KeyPressedEventArgs e)
  151. //{
  152. // if (e.HotKey.Key == Key.Y)
  153. // {
  154. // if (this.Visibility == Visibility.Collapsed)
  155. // {
  156. // ShowApp();
  157. // }
  158. // else
  159. // {
  160. // this.Visibility = Visibility.Collapsed;
  161. // }
  162. // }
  163. //}
  164. void MainWindow_Resize(object sender, System.EventArgs e)
  165. {
  166. if (this.DataContext != null)
  167. {
  168. AppData appData = this.DataContext as AppData;
  169. appData.AppConfig.WindowWidth = this.Width;
  170. appData.AppConfig.WindowHeight = this.Height;
  171. }
  172. }
  173. private void DragMove(object sender, MouseEventArgs e)
  174. {
  175. //if (e.LeftButton == MouseButtonState.Pressed)
  176. //{
  177. // this.DragMove();
  178. //}
  179. if (e.LeftButton == MouseButtonState.Pressed)
  180. {
  181. var windowMode = this.ResizeMode;
  182. if (this.ResizeMode != ResizeMode.NoResize)
  183. {
  184. this.ResizeMode = ResizeMode.NoResize;
  185. }
  186. this.UpdateLayout();
  187. /* 当点击拖拽区域的时候,让窗口跟着移动
  188. (When clicking the drag area, make the window follow) */
  189. DragMove();
  190. if (this.ResizeMode != windowMode)
  191. {
  192. this.ResizeMode = windowMode;
  193. }
  194. this.UpdateLayout();
  195. }
  196. }
  197. /// <summary>
  198. /// 关闭按钮单击事件
  199. /// </summary>
  200. /// <param name="sender"></param>
  201. /// <param name="e"></param>
  202. private void CloseButtonClick(object sender, RoutedEventArgs e)
  203. {
  204. this.Visibility = Visibility.Collapsed;
  205. }
  206. ///// <summary>
  207. ///// 左侧栏宽度改变 持久化
  208. ///// </summary>
  209. ///// <param name="sender"></param>
  210. ///// <param name="e"></param>
  211. //private void LeftCardResize(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e)
  212. //{
  213. // appData.AppConfig.MenuCardWidth = LeftColumn.Width.Value;
  214. //}
  215. /// <summary>
  216. /// 右键任务栏图标 显示主面板
  217. /// </summary>
  218. /// <param name="sender"></param>
  219. /// <param name="e"></param>
  220. private void ShowApp(object sender, RoutedEventArgs e)
  221. {
  222. ShowApp();
  223. }
  224. public static void ShowApp()
  225. {
  226. if (appData.AppConfig.FollowMouse)
  227. {
  228. ShowWindowFollowMouse.Show(mainWindow, MousePosition.CENTER, 0, 0);
  229. } else
  230. {
  231. mainWindow.Visibility = Visibility.Visible;
  232. }
  233. Keyboard.Focus(mainWindow);
  234. }
  235. /// <summary>
  236. /// 图片图标单击事件
  237. /// </summary>
  238. /// <param name="sender"></param>
  239. /// <param name="e"></param>
  240. private void NotifyIcon_Click(object sender, RoutedEventArgs e)
  241. {
  242. if (this.Visibility == Visibility.Collapsed)
  243. {
  244. ShowApp();
  245. }
  246. else
  247. {
  248. this.Visibility = Visibility.Collapsed;
  249. }
  250. }
  251. /// <summary>
  252. /// 右键任务栏图标 设置
  253. /// </summary>
  254. /// <param name="sender"></param>
  255. /// <param name="e"></param>
  256. private void ConfigApp(object sender, RoutedEventArgs e)
  257. {
  258. ConfigWindow.Show(appData.AppConfig, this);
  259. }
  260. /// <summary>
  261. /// 右键任务栏图标退出
  262. /// </summary>
  263. /// <param name="sender"></param>
  264. /// <param name="e"></param>
  265. private void ExitApp(object sender, RoutedEventArgs e)
  266. {
  267. Application.Current.Shutdown();
  268. }
  269. //public static void ShowContextMenu(IntPtr hAppWnd, Window taskBar, System.Windows.Point pt)
  270. //{
  271. // WindowInteropHelper helper = new WindowInteropHelper(taskBar);
  272. // IntPtr callingTaskBarWindow = helper.Handle;
  273. // IntPtr wMenu = GetSystemMenu(hAppWnd, false);
  274. // // Display the menu
  275. // uint command = TrackPopupMenuEx(wMenu, TPM.LEFTBUTTON | TPM.RETURNCMD, (int) pt.X, (int) pt.Y, callingTaskBarWindow, IntPtr.Zero);
  276. // if (command == 0) return;
  277. // PostMessage(hAppWnd, WM.SYSCOMMAND, new IntPtr(command), IntPtr.Zero);
  278. //}
  279. /// <summary>
  280. /// 设置图标
  281. /// </summary>
  282. /// <param name="sender"></param>
  283. /// <param name="e"></param>
  284. private void ConfigButtonClick(object sender, RoutedEventArgs e)
  285. {
  286. SettingMenus.IsOpen = true;
  287. }
  288. /// <summary>
  289. /// 设置菜单点击
  290. /// </summary>
  291. /// <param name="sender"></param>
  292. /// <param name="e"></param>
  293. private void ConfigMenuClick(object sender, RoutedEventArgs e)
  294. {
  295. ConfigWindow.Show(appData.AppConfig, this);
  296. }
  297. /// <summary>
  298. /// 待办任务
  299. /// </summary>
  300. /// <param name="sender"></param>
  301. /// <param name="e"></param>
  302. private void BacklogMenuClick(object sender, RoutedEventArgs e)
  303. {
  304. ToDoWindow.Show();
  305. }
  306. /// <summary>
  307. /// 禁用设置按钮右键菜单
  308. /// </summary>
  309. /// <param name="sender"></param>
  310. /// <param name="e"></param>
  311. private void SettingButton_Initialized(object sender, EventArgs e)
  312. {
  313. SettingButton.ContextMenu = null;
  314. }
  315. private void App_LostFocus(object sender, RoutedEventArgs e)
  316. {
  317. if (appData.AppConfig.AppHideType == AppHideType.LOST_FOCUS)
  318. {
  319. this.Visibility = Visibility.Collapsed;
  320. }
  321. }
  322. private void window_Deactivated(object sender, EventArgs e)
  323. {
  324. if (appData.AppConfig.AppHideType == AppHideType.LOST_FOCUS)
  325. {
  326. this.Visibility = Visibility.Collapsed;
  327. }
  328. }
  329. private void window_SizeChanged(object sender, SizeChangedEventArgs e)
  330. {
  331. if (this.DataContext != null)
  332. {
  333. AppData appData = this.DataContext as AppData;
  334. appData.AppConfig.WindowWidth = this.Width;
  335. appData.AppConfig.WindowHeight = this.Height;
  336. }
  337. }
  338. }
  339. }