MainWindow.xaml.cs 33 KB

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