MainWindow.xaml.cs 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140
  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. try
  901. {
  902. //防止影响主程序进程
  903. if (CheckShouldShowApp())
  904. {
  905. ShowApp();
  906. }
  907. GrayBorder.Visibility = Visibility.Visible;
  908. GuideSwitch(0);
  909. GuideCard.Visibility = Visibility.Visible;
  910. }
  911. catch (Exception) { }
  912. }
  913. private void GuideSwitch(int index)
  914. {
  915. GuideNum.Text = Convert.ToString(index + 1);
  916. GuideTitle1.Text = GuideInfoList.mainWindowGuideList[index].Title1;
  917. GuideTitle2.Text = GuideInfoList.mainWindowGuideList[index].Title2;
  918. GuideText.Text = GuideInfoList.mainWindowGuideList[index].GuideText;
  919. if (index == 0)
  920. {
  921. PreviewGuideBtn.Visibility = Visibility.Collapsed;
  922. NextGuideBtn.Content = "下一步";
  923. } else if (index > 0 && index < GuideInfoList.mainWindowGuideList.Count - 1)
  924. {
  925. PreviewGuideBtn.Visibility = Visibility.Visible;
  926. NextGuideBtn.Content = "下一步";
  927. } else
  928. {
  929. NextGuideBtn.Content = "完成";
  930. }
  931. switch (index)
  932. {
  933. default: //0 //右侧列表区域
  934. Point point = RightCard.TransformToAncestor(this).Transform(new Point(0, 0));
  935. //内部中上
  936. GrayBoderClip(point.X, point.Y, RightCard.ActualWidth, RightCard.ActualHeight,
  937. new Thickness(point.X + RightCard.ActualWidth / 2 - GuideCard.ActualWidth / 2, point.Y, 0, 0));
  938. break;
  939. case 1: //左侧菜单
  940. Point leftCardPoint = LeftCard.TransformToAncestor(this).Transform(new Point(0, 0));
  941. GrayBoderClip(leftCardPoint.X , leftCardPoint.Y , LeftCard.ActualWidth, LeftCard.ActualHeight,
  942. // 外部中下侧
  943. new Thickness(leftCardPoint.X + LeftCard.ActualWidth,
  944. leftCardPoint.Y + LeftCard.ActualHeight / 2 - GuideCard.ActualHeight / 2, 0, 0));
  945. break;
  946. case 2: //头部拖拽栏
  947. GrayBoderClip(0, 0, this.Width, 50,
  948. // 外部中下侧
  949. new Thickness(this.Width / 2 - GuideCard.ActualWidth / 2, 50, 0, 0));
  950. break;
  951. case 3:
  952. Point mainBtnPoint = MainBtnPanel.TransformToAncestor(this).Transform(new Point(0, 0));
  953. GrayBoderClip(mainBtnPoint.X, mainBtnPoint.Y, MainBtnPanel.ActualWidth, MainBtnPanel.ActualHeight,
  954. // 外部左下侧
  955. new Thickness(mainBtnPoint.X - GuideCard.Width,
  956. mainBtnPoint.Y, 0, 0));
  957. break;
  958. }
  959. }
  960. private void GrayBoderClip(double x, double y, double w, double h, Thickness margin)
  961. {
  962. PathGeometry borGeometry = new PathGeometry();
  963. RectangleGeometry rg = new RectangleGeometry();
  964. rg.Rect = new Rect(0, 0, this.Width, this.Height);
  965. borGeometry = Geometry.Combine(borGeometry, rg, GeometryCombineMode.Union, null);
  966. GrayBorder.Clip = borGeometry;
  967. RectangleGeometry rg1 = new RectangleGeometry();
  968. rg1.Rect = new Rect(x - 20, y - 20, w, h);
  969. borGeometry = Geometry.Combine(borGeometry, rg1, GeometryCombineMode.Exclude, null);
  970. GuideCard.Margin = margin;
  971. GrayBorder.Clip = borGeometry;
  972. }
  973. private void PreviewGuideBtn_Click(object sender, RoutedEventArgs e)
  974. {
  975. int index = Convert.ToInt32(GuideNum.Text.ToString()) - 1;
  976. int previewIndex = index - 1;
  977. GuideSwitch(previewIndex);
  978. }
  979. private void NextGuideBtn_Click(object sender, RoutedEventArgs e)
  980. {
  981. if ("完成".Equals(NextGuideBtn.Content.ToString())) {
  982. GrayBorder.Visibility = Visibility.Collapsed;
  983. GuideCard.Visibility = Visibility.Collapsed;
  984. return;
  985. }
  986. int index = Convert.ToInt32(GuideNum.Text.ToString()) - 1;
  987. int nextIndex = index + 1;
  988. GuideSwitch(nextIndex);
  989. }
  990. #endregion
  991. private void Guide_Click(object sender, RoutedEventArgs e)
  992. {
  993. Guide();
  994. }
  995. }
  996. }