MainWindow.xaml.cs 38 KB

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