MainWindow.xaml.cs 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  1. using GeekDesk.Constant;
  2. using GeekDesk.Control.UserControls.Config;
  3. using GeekDesk.Control.UserControls.PannelCard;
  4. using GeekDesk.Control.Windows;
  5. using GeekDesk.Interface;
  6. using GeekDesk.MyThread;
  7. using GeekDesk.Plugins.EveryThing;
  8. using GeekDesk.Plugins.EveryThing.Constant;
  9. using GeekDesk.Task;
  10. using GeekDesk.Util;
  11. using GeekDesk.ViewModel;
  12. using GeekDesk.ViewModel.Temp;
  13. using Microsoft.Win32;
  14. using NPinyin;
  15. using ShowSeconds;
  16. using System;
  17. using System.Collections.ObjectModel;
  18. using System.Diagnostics;
  19. using System.Runtime.InteropServices;
  20. using System.Text;
  21. using System.Threading;
  22. using System.Windows;
  23. using System.Windows.Controls;
  24. using System.Windows.Input;
  25. using System.Windows.Interop;
  26. using System.Windows.Media.Animation;
  27. using System.Windows.Media.Imaging;
  28. using System.Windows.Shell;
  29. using System.Windows.Threading;
  30. using static GeekDesk.Util.ShowWindowFollowMouse;
  31. namespace GeekDesk
  32. {
  33. /// <summary>
  34. /// MainWindow.xaml 的交互逻辑
  35. /// </summary>
  36. ///
  37. public partial class MainWindow : Window, IWindowCommon
  38. {
  39. public static AppData appData = CommonCode.GetAppDataByFile();
  40. public static ToDoInfoWindow toDoInfoWindow;
  41. public static int hotKeyId = -1;
  42. public static int toDoHotKeyId = -1;
  43. public static int colorPickerHotKeyId = -1;
  44. public static MainWindow mainWindow;
  45. public MainWindow()
  46. {
  47. //加载数据
  48. LoadData();
  49. InitializeComponent();
  50. //用于其他类访问
  51. mainWindow = this;
  52. //执行待办提醒
  53. ToDoTask.BackLogCheck();
  54. ////实例化隐藏 Hide类,进行时间timer设置
  55. MarginHide.ReadyHide(this);
  56. if (appData.AppConfig.MarginHide)
  57. {
  58. MarginHide.StartHide();
  59. }
  60. }
  61. /// <summary>
  62. /// 搜索快捷键按下
  63. /// </summary>
  64. /// <param name="sender"></param>
  65. /// <param name="e"></param>
  66. private void SearchHotKeyDown(object sender, CanExecuteRoutedEventArgs e)
  67. {
  68. if (appData.AppConfig.SearchType == SearchType.HOT_KEY)
  69. {
  70. ShowSearchBox();
  71. }
  72. }
  73. /// <summary>
  74. /// 显示搜索框
  75. /// </summary>
  76. private void ShowSearchBox()
  77. {
  78. RunTimeStatus.SEARCH_BOX_SHOW = true;
  79. RightCard.VisibilitySearchCard(Visibility.Visible);
  80. SearchBox.Width = 400;
  81. SearchBox.Focus();
  82. //执行一遍a查询
  83. SearchBox_TextChanged(null, null);
  84. }
  85. /// <summary>
  86. /// 搜索开始
  87. /// </summary>
  88. /// <param name="sender"></param>
  89. /// <param name="e"></param>
  90. private void SearchBox_TextChanged(object sender, TextChangedEventArgs e)
  91. {
  92. if (!RunTimeStatus.SEARCH_BOX_SHOW
  93. && appData.AppConfig.SearchType != SearchType.KEY_DOWN
  94. )
  95. {
  96. SearchBox.TextChanged -= SearchBox_TextChanged;
  97. SearchBox.Clear();
  98. SearchBox.TextChanged += SearchBox_TextChanged;
  99. return;
  100. }
  101. if (!RunTimeStatus.SEARCH_BOX_SHOW) ShowSearchBox();
  102. //刷新搜索后 鼠标移动次数置为0
  103. RunTimeStatus.MOUSE_MOVE_COUNT = 0;
  104. //隐藏popup
  105. RightCard.MyPoptip.IsOpen = false;
  106. string inputText = SearchBox.Text.ToLower();
  107. RightCard.VerticalUFG.Visibility = Visibility.Collapsed;
  108. if (!string.IsNullOrEmpty(inputText))
  109. {
  110. RunTimeStatus.EVERYTHING_SEARCH_DELAY_TIME = 300;
  111. if (!RunTimeStatus.EVERYTHING_NEW_SEARCH)
  112. {
  113. RunTimeStatus.EVERYTHING_NEW_SEARCH = true;
  114. SearchDelay();
  115. }
  116. } else
  117. {
  118. new Thread(() =>
  119. {
  120. this.Dispatcher.Invoke(() =>
  121. {
  122. SearchIconList.RemoveAll();
  123. });
  124. }).Start();
  125. }
  126. }
  127. private void SearchDelay()
  128. {
  129. new Thread(() =>
  130. {
  131. while (RunTimeStatus.EVERYTHING_SEARCH_DELAY_TIME > 0)
  132. {
  133. Thread.Sleep(10);
  134. RunTimeStatus.EVERYTHING_SEARCH_DELAY_TIME -= 10;
  135. }
  136. RunTimeStatus.EVERYTHING_NEW_SEARCH = false;
  137. this.Dispatcher.Invoke(() =>
  138. {
  139. if (SearchIconList.IconList.Count > 0)
  140. {
  141. SearchIconList.RemoveAll();
  142. }
  143. string inputText = SearchBox.Text.ToLower().Trim();
  144. int count = 0;
  145. //GeekDesk数据搜索
  146. ObservableCollection<MenuInfo> menuList = appData.MenuList;
  147. foreach (MenuInfo menu in menuList)
  148. {
  149. ObservableCollection<IconInfo> iconList = menu.IconList;
  150. foreach (IconInfo icon in iconList)
  151. {
  152. if (RunTimeStatus.EVERYTHING_NEW_SEARCH) return;
  153. string pyName = Pinyin.GetInitials(icon.Name).ToLower();
  154. if (icon.Name.Contains(inputText) || pyName.Contains(inputText))
  155. {
  156. SearchIconList.IconList.Add(icon);
  157. count++;
  158. }
  159. }
  160. }
  161. if (appData.AppConfig.EnableEveryThing == true)
  162. {
  163. new Thread(() =>
  164. {
  165. //EveryThing全盘搜索
  166. ObservableCollection<IconInfo> iconBakList = EveryThingUtil.Search(inputText);
  167. count += iconBakList.Count;
  168. this.Dispatcher.Invoke(() =>
  169. {
  170. TotalMsgBtn.Visibility = Visibility.Visible;
  171. TotalMsgBtn.Content = count + " of " + Convert.ToInt64(EveryThingUtil.Everything_GetNumResults());
  172. foreach (IconInfo icon in iconBakList)
  173. {
  174. if (RunTimeStatus.EVERYTHING_NEW_SEARCH) return;
  175. SearchIconList.IconList.Add(icon);
  176. }
  177. });
  178. //异步加载图标
  179. if (iconBakList != null && iconBakList.Count > 0)
  180. {
  181. new Thread(() =>
  182. {
  183. foreach (IconInfo icon in iconBakList)
  184. {
  185. if (RunTimeStatus.EVERYTHING_NEW_SEARCH) return;
  186. this.Dispatcher.Invoke(() =>
  187. {
  188. icon.BitmapImage_NoWrite = ImageUtil.GetBitmapIconByUnknownPath(icon.Path);
  189. }, DispatcherPriority.SystemIdle);
  190. }
  191. }).Start();
  192. }
  193. }).Start();
  194. }
  195. if (RightCard.SearchListBox.Items.Count > 0)
  196. {
  197. RightCard.SearchListBox.SelectedIndex = 0;
  198. }
  199. RightCard.VerticalUFG.Visibility = Visibility.Visible;
  200. });
  201. }).Start();
  202. }
  203. /// <summary>
  204. /// 隐藏搜索框
  205. /// </summary>
  206. public void HidedSearchBox()
  207. {
  208. RunTimeStatus.EVERYTHING_NEW_SEARCH = true;
  209. new Thread(() =>
  210. {
  211. Thread.Sleep(1000);
  212. RunTimeStatus.EVERYTHING_NEW_SEARCH = false;
  213. }).Start();
  214. new Thread(() =>
  215. {
  216. this.Dispatcher.Invoke(() =>
  217. {
  218. Keyboard.Focus(SearchBox);
  219. RunTimeStatus.SEARCH_BOX_SHOW = false;
  220. SearchBox.TextChanged -= SearchBox_TextChanged;
  221. SearchBox.Clear();
  222. SearchBox.TextChanged += SearchBox_TextChanged;
  223. SearchBox.Width = 0;
  224. TotalMsgBtn.Content = "0 of 0";
  225. TotalMsgBtn.Visibility = Visibility.Hidden;
  226. RightCard.VisibilitySearchCard(Visibility.Collapsed);
  227. SearchIconList.RemoveAll();
  228. });
  229. }).Start();
  230. }
  231. /// <summary>
  232. /// 加载缓存数据
  233. /// </summary>
  234. private void LoadData()
  235. {
  236. this.DataContext = appData;
  237. if (appData.MenuList.Count == 0)
  238. {
  239. appData.MenuList.Add(new MenuInfo() { MenuName = "NewMenu", MenuId = System.Guid.NewGuid().ToString(), MenuEdit = Visibility.Collapsed });
  240. }
  241. this.Width = appData.AppConfig.WindowWidth;
  242. this.Height = appData.AppConfig.WindowHeight;
  243. }
  244. /// <summary>
  245. /// 窗口加载完毕 执行方法
  246. /// </summary>
  247. /// <param name="sender"></param>
  248. /// <param name="e"></param>
  249. void Window_Loaded(object sender, RoutedEventArgs e)
  250. {
  251. BGSettingUtil.BGSetting();
  252. if (!appData.AppConfig.StartedShowPanel)
  253. {
  254. this.Visibility = Visibility.Collapsed;
  255. }
  256. else
  257. {
  258. ShowApp();
  259. }
  260. //给任务栏图标一个名字
  261. BarIcon.Text = Constants.MY_NAME;
  262. //注册热键
  263. if (true == appData.AppConfig.EnableAppHotKey)
  264. {
  265. RegisterHotKey(true);
  266. }
  267. if (true == appData.AppConfig.EnableTodoHotKey)
  268. {
  269. RegisterCreateToDoHotKey(true);
  270. }
  271. if (true == appData.AppConfig.EnableColorPickerHotKey)
  272. {
  273. RegisterColorPickerHotKey(true);
  274. }
  275. //注册自启动
  276. if (!appData.AppConfig.SelfStartUped && !Constants.DEV)
  277. {
  278. RegisterUtil.SetSelfStarting(appData.AppConfig.SelfStartUp, Constants.MY_NAME);
  279. }
  280. //注册鼠标监听事件
  281. if (appData.AppConfig.MouseMiddleShow)
  282. {
  283. MouseHookThread.Hook();
  284. }
  285. //显秒插件
  286. if (appData.AppConfig.SecondsWindow == true)
  287. {
  288. SecondsWindow.ShowWindow();
  289. }
  290. //监听实时文件夹菜单
  291. FileWatcher.EnableLinkMenuWatcher(appData);
  292. //更新线程开启 检测更新
  293. UpdateThread.Update();
  294. //建立相对路径
  295. RelativePathThread.MakeRelativePath();
  296. //毛玻璃 暂时未解决阴影问题
  297. //BlurGlassUtil.EnableBlur(this);
  298. //设置归属桌面 解决桌面覆盖程序界面的bug
  299. WindowUtil.SetOwner(this, WindowUtil.GetDesktopHandle(this, DesktopLayer.Progman));
  300. if (appData.AppConfig.EnableEveryThing == true)
  301. {
  302. //开启EveryThing插件
  303. EveryThingUtil.EnableEveryThing();
  304. }
  305. Keyboard.Focus(SearchBox);
  306. MessageUtil.ChangeWindowMessageFilter(MessageUtil.WM_COPYDATA, 1);
  307. }
  308. /// <summary>
  309. /// 注册当前窗口的热键
  310. /// </summary>
  311. public static void RegisterHotKey(bool first)
  312. {
  313. try
  314. {
  315. if (appData.AppConfig.HotkeyModifiers != GlobalHotKey.HotkeyModifiers.None)
  316. {
  317. hotKeyId = GlobalHotKey.RegisterHotKey(appData.AppConfig.HotkeyModifiers, appData.AppConfig.Hotkey, () =>
  318. {
  319. if (RunTimeStatus.MAIN_HOT_KEY_DOWN) return;
  320. RunTimeStatus.MAIN_HOT_KEY_DOWN = true;
  321. new Thread(() =>
  322. {
  323. Thread.Sleep(RunTimeStatus.MAIN_HOT_KEY_TIME);
  324. RunTimeStatus.MAIN_HOT_KEY_DOWN = false;
  325. }).Start();
  326. if (MotionControl.hotkeyFinished)
  327. {
  328. if (CheckShouldShowApp())
  329. {
  330. ShowApp();
  331. }
  332. else
  333. {
  334. HideApp();
  335. }
  336. }
  337. });
  338. if (!first)
  339. {
  340. HandyControl.Controls.Growl.Success("GeekDesk快捷键注册成功(" + appData.AppConfig.HotkeyStr + ")!", "HotKeyGrowl");
  341. }
  342. }
  343. else
  344. {
  345. }
  346. }
  347. catch (Exception)
  348. {
  349. if (first)
  350. {
  351. HandyControl.Controls.Growl.WarningGlobal("GeekDesk启动快捷键已被其它程序占用(" + appData.AppConfig.HotkeyStr + ")!");
  352. }
  353. else
  354. {
  355. HandyControl.Controls.Growl.Warning("GeekDesk启动快捷键已被其它程序占用(" + appData.AppConfig.HotkeyStr + ")!", "HotKeyGrowl");
  356. }
  357. }
  358. }
  359. /// <summary>
  360. /// 注册新建待办的热键
  361. /// </summary>
  362. public static void RegisterCreateToDoHotKey(bool first)
  363. {
  364. try
  365. {
  366. if (appData.AppConfig.HotkeyModifiers != GlobalHotKey.HotkeyModifiers.None)
  367. {
  368. //加载完毕注册热键
  369. toDoHotKeyId = GlobalHotKey.RegisterHotKey(appData.AppConfig.ToDoHotkeyModifiers, appData.AppConfig.ToDoHotkey, () =>
  370. {
  371. if (MotionControl.hotkeyFinished)
  372. {
  373. ToDoWindow.ShowOrHide();
  374. }
  375. });
  376. if (!first)
  377. {
  378. HandyControl.Controls.Growl.Success("新建待办任务快捷键注册成功(" + appData.AppConfig.ToDoHotkeyStr + ")!", "HotKeyGrowl");
  379. }
  380. }
  381. }
  382. catch (Exception)
  383. {
  384. if (first)
  385. {
  386. HandyControl.Controls.Growl.WarningGlobal("新建待办任务快捷键已被其它程序占用(" + appData.AppConfig.ToDoHotkeyStr + ")!");
  387. }
  388. else
  389. {
  390. HandyControl.Controls.Growl.Warning("新建待办任务快捷键已被其它程序占用(" + appData.AppConfig.ToDoHotkeyStr + ")!", "HotKeyGrowl");
  391. }
  392. }
  393. }
  394. /// <summary>
  395. /// 注册新建待办的热键
  396. /// </summary>
  397. public static void RegisterColorPickerHotKey(bool first)
  398. {
  399. try
  400. {
  401. if (appData.AppConfig.HotkeyModifiers != GlobalHotKey.HotkeyModifiers.None)
  402. {
  403. //加载完毕注册热键
  404. colorPickerHotKeyId = GlobalHotKey.RegisterHotKey(appData.AppConfig.ColorPickerHotkeyModifiers, appData.AppConfig.ColorPickerHotkey, () =>
  405. {
  406. if (MotionControl.hotkeyFinished)
  407. {
  408. GlobalColorPickerWindow.CreateNoShow();
  409. }
  410. });
  411. if (!first)
  412. {
  413. HandyControl.Controls.Growl.Success("拾色器快捷键注册成功(" + appData.AppConfig.ColorPickerHotkeyStr + ")!", "HotKeyGrowl");
  414. }
  415. }
  416. }
  417. catch (Exception)
  418. {
  419. if (first)
  420. {
  421. HandyControl.Controls.Growl.WarningGlobal("拾色器快捷键已被其它程序占用(" + appData.AppConfig.ColorPickerHotkeyStr + ")!");
  422. }
  423. else
  424. {
  425. HandyControl.Controls.Growl.Warning("拾色器快捷键已被其它程序占用(" + appData.AppConfig.ColorPickerHotkeyStr + ")!", "HotKeyGrowl");
  426. }
  427. }
  428. }
  429. /// <summary>
  430. /// 程序窗体拖动
  431. /// </summary>
  432. /// <param name="sender"></param>
  433. /// <param name="e"></param>
  434. private void DragMove(object sender, MouseEventArgs e)
  435. {
  436. if (e.LeftButton == MouseButtonState.Pressed)
  437. {
  438. var windowMode = this.ResizeMode;
  439. if (this.ResizeMode != ResizeMode.NoResize)
  440. {
  441. this.ResizeMode = ResizeMode.NoResize;
  442. }
  443. this.UpdateLayout();
  444. /* 当点击拖拽区域的时候,让窗口跟着移动
  445. (When clicking the drag area, make the window follow) */
  446. DragMove();
  447. if (this.ResizeMode != windowMode)
  448. {
  449. this.ResizeMode = windowMode;
  450. }
  451. this.UpdateLayout();
  452. }
  453. }
  454. /// <summary>
  455. /// 关闭按钮单击事件
  456. /// </summary>
  457. /// <param name="sender"></param>
  458. /// <param name="e"></param>
  459. private void CloseButtonClick(object sender, RoutedEventArgs e)
  460. {
  461. HideApp();
  462. }
  463. ///// <summary>
  464. ///// 左侧栏宽度改变 持久化
  465. ///// </summary>
  466. ///// <param name="sender"></param>
  467. ///// <param name="e"></param>
  468. //private void LeftCardResize(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e)
  469. //{
  470. // appData.AppConfig.MenuCardWidth = LeftColumn.Width.Value;
  471. //}
  472. /// <summary>
  473. /// 右键任务栏图标 显示主面板
  474. /// </summary>
  475. /// <param name="sender"></param>
  476. /// <param name="e"></param>
  477. public void ShowApp(object sender, RoutedEventArgs e)
  478. {
  479. ShowApp();
  480. }
  481. public static void ShowApp()
  482. {
  483. //有全屏化应用则不显示
  484. //if (CommonCode.IsPrimaryFullScreen())
  485. //{
  486. // return;
  487. //}
  488. if (MarginHide.ON_HIDE)
  489. {
  490. //修改贴边隐藏状态为未隐藏
  491. MarginHide.IS_HIDE = false;
  492. if (!CommonCode.MouseInWindow(mainWindow))
  493. {
  494. RunTimeStatus.MARGIN_HIDE_AND_OTHER_SHOW = true;
  495. MarginHide.WaitHide(3000);
  496. }
  497. }
  498. if (appData.AppConfig.FollowMouse)
  499. {
  500. ShowWindowFollowMouse.Show(mainWindow, MousePosition.CENTER, 0, 0);
  501. }
  502. MainWindow.mainWindow.Activate();
  503. mainWindow.Show();
  504. //mainWindow.Visibility = Visibility.Visible;
  505. if (appData.AppConfig.AppAnimation)
  506. {
  507. appData.AppConfig.IsShow = true;
  508. }
  509. else
  510. {
  511. appData.AppConfig.IsShow = null;
  512. //防止永远不显示界面
  513. if (mainWindow.Opacity < 1)
  514. {
  515. mainWindow.Opacity = 1;
  516. }
  517. }
  518. //FadeStoryBoard(1, (int)CommonEnum.WINDOW_ANIMATION_TIME, Visibility.Visible);
  519. Keyboard.Focus(mainWindow);
  520. if (RunTimeStatus.SHOW_MENU_PASSWORDBOX)
  521. {
  522. mainWindow.RightCard.PDDialog.SetFocus();
  523. }
  524. else
  525. {
  526. Keyboard.Focus(mainWindow.SearchBox);
  527. }
  528. }
  529. public static void HideApp()
  530. {
  531. if (appData.AppConfig.AppAnimation)
  532. {
  533. appData.AppConfig.IsShow = false;
  534. }
  535. else
  536. {
  537. appData.AppConfig.IsShow = null;
  538. HideAppVis();
  539. }
  540. }
  541. private static void HideAppVis()
  542. {
  543. //关闭锁定
  544. RunTimeStatus.LOCK_APP_PANEL = false;
  545. if (RunTimeStatus.SEARCH_BOX_SHOW)
  546. {
  547. mainWindow.HidedSearchBox();
  548. }
  549. mainWindow.Visibility = Visibility.Collapsed;
  550. //if (!MarginHide.IS_HIDE)
  551. //{
  552. //}
  553. //else
  554. //{
  555. // ShowApp();
  556. //}
  557. }
  558. /// <summary>
  559. /// 淡入淡出效果
  560. /// </summary>
  561. /// <param name="opacity"></param>
  562. /// <param name="milliseconds"></param>
  563. /// <param name="visibility"></param>
  564. public static void FadeStoryBoard(int opacity, int milliseconds, Visibility visibility)
  565. {
  566. if (appData.AppConfig.AppAnimation)
  567. {
  568. DoubleAnimation opacityAnimation = new DoubleAnimation
  569. {
  570. From = mainWindow.Opacity,
  571. To = opacity,
  572. Duration = new Duration(TimeSpan.FromMilliseconds(milliseconds))
  573. };
  574. opacityAnimation.Completed += (s, e) =>
  575. {
  576. mainWindow.BeginAnimation(OpacityProperty, null);
  577. if (visibility == Visibility.Visible)
  578. {
  579. mainWindow.Opacity = 1;
  580. }
  581. else
  582. {
  583. mainWindow.Opacity = 0;
  584. CommonCode.SortIconList();
  585. }
  586. };
  587. Timeline.SetDesiredFrameRate(opacityAnimation, 60);
  588. mainWindow.BeginAnimation(OpacityProperty, opacityAnimation);
  589. }
  590. else
  591. {
  592. //防止关闭动画后 窗体仍是0透明度
  593. mainWindow.Opacity = 1;
  594. mainWindow.Visibility = visibility;
  595. if (visibility == Visibility.Collapsed)
  596. {
  597. CommonCode.SortIconList();
  598. }
  599. }
  600. }
  601. /// <summary>
  602. /// 图片图标单击事件
  603. /// </summary>
  604. /// <param name="sender"></param>
  605. /// <param name="e"></param>
  606. private void NotifyIcon_Click(object sender, RoutedEventArgs e)
  607. {
  608. if (CheckShouldShowApp())
  609. {
  610. ShowApp();
  611. }
  612. else
  613. {
  614. HideApp();
  615. }
  616. }
  617. private static bool CheckShouldShowApp()
  618. {
  619. return mainWindow.Visibility == Visibility.Collapsed
  620. || mainWindow.Opacity == 0
  621. || MarginHide.IS_HIDE
  622. || !WindowUtil.WindowIsTop(mainWindow);
  623. }
  624. /// <summary>
  625. /// 右键任务栏图标 设置
  626. /// </summary>
  627. /// <param name="sender"></param>
  628. /// <param name="e"></param>
  629. private void ConfigApp(object sender, RoutedEventArgs e)
  630. {
  631. ConfigWindow.Show(appData.AppConfig, this);
  632. }
  633. /// <summary>
  634. /// 右键任务栏图标打开程序目录
  635. /// </summary>
  636. /// <param name="sender"></param>
  637. /// <param name="e"></param>
  638. private void OpenThisDir(object sender, RoutedEventArgs e)
  639. {
  640. Process p = new Process();
  641. p.StartInfo.FileName = "Explorer.exe";
  642. p.StartInfo.Arguments = "/e,/select," + Constants.APP_DIR + Constants.MY_NAME + ".exe";
  643. p.Start();
  644. }
  645. /// <summary>
  646. /// 设置图标
  647. /// </summary>
  648. /// <param name="sender"></param>
  649. /// <param name="e"></param>
  650. private void ConfigButtonClick(object sender, RoutedEventArgs e)
  651. {
  652. SettingMenus.IsOpen = true;
  653. }
  654. /// <summary>
  655. /// 设置菜单点击
  656. /// </summary>
  657. /// <param name="sender"></param>
  658. /// <param name="e"></param>
  659. private void ConfigMenuClick(object sender, RoutedEventArgs e)
  660. {
  661. ConfigWindow.Show(appData.AppConfig, this);
  662. }
  663. /// <summary>
  664. /// 待办任务
  665. /// </summary>
  666. /// <param name="sender"></param>
  667. /// <param name="e"></param>
  668. private void BacklogMenuClick(object sender, RoutedEventArgs e)
  669. {
  670. ToDoWindow.Show();
  671. }
  672. /// <summary>
  673. /// 禁用设置按钮右键菜单
  674. /// </summary>
  675. /// <param name="sender"></param>
  676. /// <param name="e"></param>
  677. private void SettingButton_Initialized(object sender, EventArgs e)
  678. {
  679. SettingButton.ContextMenu = null;
  680. }
  681. private void AppWindowLostFocus()
  682. {
  683. if (appData.AppConfig.AppHideType == AppHideType.LOST_FOCUS
  684. && this.Opacity == 1 && !RunTimeStatus.LOCK_APP_PANEL)
  685. {
  686. //如果开启了贴边隐藏 则窗体不贴边才隐藏窗口
  687. if (!appData.AppConfig.MarginHide || (appData.AppConfig.MarginHide && !MarginHide.IS_HIDE))
  688. {
  689. HideApp();
  690. }
  691. }
  692. }
  693. private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
  694. {
  695. if (this.DataContext != null)
  696. {
  697. AppData appData = this.DataContext as AppData;
  698. appData.AppConfig.WindowWidth = this.Width;
  699. appData.AppConfig.WindowHeight = this.Height;
  700. }
  701. }
  702. /// <summary>
  703. /// 右键任务栏图标退出
  704. /// </summary>
  705. /// <param name="sender"></param>
  706. /// <param name="e"></param>
  707. private void ExitApp(object sender, RoutedEventArgs e)
  708. {
  709. if (appData.AppConfig.MouseMiddleShow || appData.AppConfig.SecondsWindow == true)
  710. {
  711. MouseHookThread.Dispose();
  712. }
  713. if (appData.AppConfig.EnableEveryThing == true)
  714. {
  715. EveryThingUtil.DisableEveryThing();
  716. }
  717. Application.Current.Shutdown();
  718. }
  719. /// <summary>
  720. /// 重启
  721. /// </summary>
  722. /// <param name="sender"></param>
  723. /// <param name="e"></param>
  724. private void ReStartApp(object sender, RoutedEventArgs e)
  725. {
  726. if (appData.AppConfig.MouseMiddleShow || appData.AppConfig.SecondsWindow == true)
  727. {
  728. MouseHookThread.Dispose();
  729. }
  730. Process p = new Process();
  731. p.StartInfo.FileName = Constants.APP_DIR + "GeekDesk.exe";
  732. p.StartInfo.WorkingDirectory = Constants.APP_DIR;
  733. p.Start();
  734. Application.Current.Shutdown();
  735. }
  736. /// <summary>
  737. /// 关闭托盘图标
  738. /// </summary>
  739. /// <param name="sender"></param>
  740. /// <param name="e"></param>
  741. private void CloseBarIcon(object sender, RoutedEventArgs e)
  742. {
  743. appData.AppConfig.ShowBarIcon = false;
  744. }
  745. public void OnKeyDown(object sender, KeyEventArgs e)
  746. {
  747. //char c = (char)e.Key;
  748. if (e.Key == Key.Escape)
  749. {
  750. HideApp();
  751. }
  752. if (RunTimeStatus.SEARCH_BOX_SHOW && (e.Key == Key.Up
  753. || e.Key == Key.Down
  754. || e.Key == Key.Tab
  755. || e.Key == Key.Enter
  756. ))
  757. {
  758. if (e.Key == Key.Down || e.Key == Key.Tab)
  759. {
  760. RightCard.SearchListBoxIndexAdd();
  761. }
  762. else if (e.Key == Key.Up)
  763. {
  764. RightCard.SearchListBoxIndexSub();
  765. }
  766. else if (e.Key == Key.Enter)
  767. {
  768. RightCard.StartupSelectionItem();
  769. }
  770. }
  771. }
  772. /// <summary>
  773. /// 为了让修改菜单的textBox失去焦点
  774. /// </summary>
  775. /// <param name="sender"></param>
  776. /// <param name="e"></param>
  777. private void MainWindow_MouseDown(object sender, MouseButtonEventArgs e)
  778. {
  779. SearchBox.Focus();
  780. }
  781. /// <summary>
  782. /// 鼠标进入后
  783. /// </summary>
  784. /// <param name="sender"></param>
  785. /// <param name="e"></param>
  786. private void MainWindow_MouseEnter(object sender, MouseEventArgs e)
  787. {
  788. //防止延迟贴边隐藏
  789. RunTimeStatus.MARGIN_HIDE_AND_OTHER_SHOW = false;
  790. }
  791. /// <summary>
  792. /// 打开屏幕拾色器
  793. /// </summary>
  794. /// <param name="sender"></param>
  795. /// <param name="e"></param>
  796. private void ColorPicker(object sender, RoutedEventArgs e)
  797. {
  798. TaskbarContextMenu.IsOpen = false;
  799. GlobalColorPickerWindow.CreateNoShow();
  800. }
  801. private void Window_GotFocus(object sender, RoutedEventArgs e)
  802. {
  803. // 如果没有在修改菜单 并且不是右键点击了面板
  804. if (!RunTimeStatus.IS_MENU_EDIT
  805. && !RunTimeStatus.SHOW_RIGHT_BTN_MENU
  806. && !RunTimeStatus.APP_BTN_IS_DOWN)
  807. {
  808. if (RunTimeStatus.SHOW_MENU_PASSWORDBOX)
  809. {
  810. //必须在其它文本框没有工作的时候才给密码框焦点
  811. RightCard.PDDialog.SetFocus();
  812. }
  813. else
  814. {
  815. //必须在其它文本框没有工作的时候才给搜索框焦点
  816. Keyboard.Focus(SearchBox);
  817. }
  818. }
  819. }
  820. private void AppWindow_Deactivated(object sender, EventArgs e)
  821. {
  822. AppWindowLostFocus();
  823. }
  824. /// <summary>
  825. /// 备份数据文件
  826. /// </summary>
  827. /// <param name="sender"></param>
  828. /// <param name="e"></param>
  829. [Obsolete]
  830. private void BakDataFile(object sender, RoutedEventArgs e)
  831. {
  832. Thread t = new Thread(() =>
  833. {
  834. CommonCode.BakAppData();
  835. });
  836. t.ApartmentState = ApartmentState.STA;
  837. t.Start();
  838. }
  839. private void AppButton_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  840. {
  841. //点击了面板
  842. RunTimeStatus.APP_BTN_IS_DOWN = true;
  843. new Thread(() =>
  844. {
  845. Thread.Sleep(50);
  846. RunTimeStatus.APP_BTN_IS_DOWN = false;
  847. }).Start();
  848. }
  849. private ICommand _hideCommand;
  850. public ICommand HideCommand
  851. {
  852. get
  853. {
  854. if (_hideCommand == null)
  855. {
  856. _hideCommand = new RelayCommand(
  857. p =>
  858. {
  859. return true;
  860. },
  861. p =>
  862. {
  863. HideAppVis();
  864. });
  865. }
  866. return _hideCommand;
  867. }
  868. }
  869. protected override void OnSourceInitialized(EventArgs e)
  870. {
  871. base.OnSourceInitialized(e);
  872. HwndSource hwndSource = PresentationSource.FromVisual(this) as HwndSource;
  873. if (hwndSource != null)
  874. {
  875. IntPtr handle = hwndSource.Handle;
  876. hwndSource.AddHook(new HwndSourceHook(WndProc));
  877. }
  878. }
  879. IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
  880. {
  881. if (msg == MessageUtil.WM_COPYDATA)
  882. {
  883. MessageUtil.CopyDataStruct cds = (MessageUtil.CopyDataStruct)System.Runtime.InteropServices.Marshal.PtrToStructure(lParam, typeof(MessageUtil.CopyDataStruct));
  884. if ("ShowApp".Equals(cds.msg))
  885. {
  886. ShowApp();
  887. }
  888. }
  889. return hwnd;
  890. }
  891. }
  892. }