MainWindow.xaml.cs 38 KB

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