MainWindow.xaml.cs 37 KB

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