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. UpdateTask.Start();
  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. //启动文件备份任务
  334. BakTask.Start();
  335. Keyboard.Focus(SearchBox);
  336. MessageUtil.ChangeWindowMessageFilter(MessageUtil.WM_COPYDATA, 1);
  337. if (!dataFileExist)
  338. {
  339. Guide();
  340. }
  341. }
  342. /// <summary>
  343. /// 注册当前窗口的热键
  344. /// </summary>
  345. public static void RegisterHotKey(bool first)
  346. {
  347. try
  348. {
  349. if (appData.AppConfig.HotkeyModifiers != GlobalHotKey.HotkeyModifiers.None)
  350. {
  351. hotKeyId = GlobalHotKey.RegisterHotKey(appData.AppConfig.HotkeyModifiers, appData.AppConfig.Hotkey, () =>
  352. {
  353. if (RunTimeStatus.MAIN_HOT_KEY_DOWN) return;
  354. RunTimeStatus.MAIN_HOT_KEY_DOWN = true;
  355. new Thread(() =>
  356. {
  357. Thread.Sleep(RunTimeStatus.MAIN_HOT_KEY_TIME);
  358. RunTimeStatus.MAIN_HOT_KEY_DOWN = false;
  359. }).Start();
  360. if (MotionControl.hotkeyFinished)
  361. {
  362. if (CheckShouldShowApp())
  363. {
  364. ShowApp();
  365. }
  366. else
  367. {
  368. HideApp();
  369. }
  370. }
  371. });
  372. if (!first)
  373. {
  374. HandyControl.Controls.Growl.Success("GeekDesk快捷键注册成功(" + appData.AppConfig.HotkeyStr + ")!", "HotKeyGrowl");
  375. }
  376. }
  377. else
  378. {
  379. }
  380. }
  381. catch (Exception)
  382. {
  383. if (first)
  384. {
  385. HandyControl.Controls.Growl.WarningGlobal("GeekDesk启动快捷键已被其它程序占用(" + appData.AppConfig.HotkeyStr + ")!");
  386. }
  387. else
  388. {
  389. HandyControl.Controls.Growl.Warning("GeekDesk启动快捷键已被其它程序占用(" + appData.AppConfig.HotkeyStr + ")!", "HotKeyGrowl");
  390. }
  391. }
  392. }
  393. /// <summary>
  394. /// 注册新建待办的热键
  395. /// </summary>
  396. public static void RegisterCreateToDoHotKey(bool first)
  397. {
  398. try
  399. {
  400. if (appData.AppConfig.HotkeyModifiers != GlobalHotKey.HotkeyModifiers.None)
  401. {
  402. //加载完毕注册热键
  403. toDoHotKeyId = GlobalHotKey.RegisterHotKey(appData.AppConfig.ToDoHotkeyModifiers, appData.AppConfig.ToDoHotkey, () =>
  404. {
  405. if (MotionControl.hotkeyFinished)
  406. {
  407. ToDoWindow.ShowOrHide();
  408. }
  409. });
  410. if (!first)
  411. {
  412. HandyControl.Controls.Growl.Success("新建待办任务快捷键注册成功(" + appData.AppConfig.ToDoHotkeyStr + ")!", "HotKeyGrowl");
  413. }
  414. }
  415. }
  416. catch (Exception)
  417. {
  418. if (first)
  419. {
  420. HandyControl.Controls.Growl.WarningGlobal("新建待办任务快捷键已被其它程序占用(" + appData.AppConfig.ToDoHotkeyStr + ")!");
  421. }
  422. else
  423. {
  424. HandyControl.Controls.Growl.Warning("新建待办任务快捷键已被其它程序占用(" + appData.AppConfig.ToDoHotkeyStr + ")!", "HotKeyGrowl");
  425. }
  426. }
  427. }
  428. /// <summary>
  429. /// 注册新建待办的热键
  430. /// </summary>
  431. public static void RegisterColorPickerHotKey(bool first)
  432. {
  433. try
  434. {
  435. if (appData.AppConfig.HotkeyModifiers != GlobalHotKey.HotkeyModifiers.None)
  436. {
  437. //加载完毕注册热键
  438. colorPickerHotKeyId = GlobalHotKey.RegisterHotKey(appData.AppConfig.ColorPickerHotkeyModifiers, appData.AppConfig.ColorPickerHotkey, () =>
  439. {
  440. if (MotionControl.hotkeyFinished)
  441. {
  442. GlobalColorPickerWindow.CreateNoShow();
  443. }
  444. });
  445. if (!first)
  446. {
  447. HandyControl.Controls.Growl.Success("拾色器快捷键注册成功(" + appData.AppConfig.ColorPickerHotkeyStr + ")!", "HotKeyGrowl");
  448. }
  449. }
  450. }
  451. catch (Exception)
  452. {
  453. if (first)
  454. {
  455. HandyControl.Controls.Growl.WarningGlobal("拾色器快捷键已被其它程序占用(" + appData.AppConfig.ColorPickerHotkeyStr + ")!");
  456. }
  457. else
  458. {
  459. HandyControl.Controls.Growl.Warning("拾色器快捷键已被其它程序占用(" + appData.AppConfig.ColorPickerHotkeyStr + ")!", "HotKeyGrowl");
  460. }
  461. }
  462. }
  463. /// <summary>
  464. /// 程序窗体拖动
  465. /// </summary>
  466. /// <param name="sender"></param>
  467. /// <param name="e"></param>
  468. private void DragMove(object sender, MouseEventArgs e)
  469. {
  470. if (e.LeftButton == MouseButtonState.Pressed)
  471. {
  472. DragMove();
  473. }
  474. }
  475. /// <summary>
  476. /// 关闭按钮单击事件
  477. /// </summary>
  478. /// <param name="sender"></param>
  479. /// <param name="e"></param>
  480. private void CloseButtonClick(object sender, RoutedEventArgs e)
  481. {
  482. HideApp();
  483. }
  484. ///// <summary>
  485. ///// 左侧栏宽度改变 持久化
  486. ///// </summary>
  487. ///// <param name="sender"></param>
  488. ///// <param name="e"></param>
  489. //private void LeftCardResize(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e)
  490. //{
  491. // appData.AppConfig.MenuCardWidth = LeftColumn.Width.Value;
  492. //}
  493. /// <summary>
  494. /// 右键任务栏图标 显示主面板
  495. /// </summary>
  496. /// <param name="sender"></param>
  497. /// <param name="e"></param>
  498. public void ShowApp(object sender, RoutedEventArgs e)
  499. {
  500. ShowApp();
  501. }
  502. public static void ShowApp()
  503. {
  504. //有全屏化应用则不显示
  505. //if (CommonCode.IsPrimaryFullScreen())
  506. //{
  507. // return;
  508. //}
  509. if (MarginHide.ON_HIDE)
  510. {
  511. //修改贴边隐藏状态为未隐藏
  512. MarginHide.IS_HIDE = false;
  513. if (!CommonCode.MouseInWindow(mainWindow))
  514. {
  515. RunTimeStatus.MARGIN_HIDE_AND_OTHER_SHOW = true;
  516. MarginHide.WaitHide(3000);
  517. }
  518. }
  519. if (appData.AppConfig.FollowMouse)
  520. {
  521. ShowWindowFollowMouse.Show(mainWindow, MousePosition.CENTER, 0, 0);
  522. //ShowWindowFollowMouse.FollowMouse(mainWindow);
  523. }
  524. MainWindow.mainWindow.Activate();
  525. mainWindow.Show();
  526. //mainWindow.Visibility = Visibility.Visible;
  527. if (appData.AppConfig.AppAnimation)
  528. {
  529. appData.AppConfig.IsShow = true;
  530. }
  531. else
  532. {
  533. appData.AppConfig.IsShow = null;
  534. //防止永远不显示界面
  535. if (mainWindow.Opacity < 1)
  536. {
  537. mainWindow.Opacity = 1;
  538. }
  539. }
  540. //FadeStoryBoard(1, (int)CommonEnum.WINDOW_ANIMATION_TIME, Visibility.Visible);
  541. Keyboard.Focus(mainWindow);
  542. if (RunTimeStatus.SHOW_MENU_PASSWORDBOX)
  543. {
  544. mainWindow.RightCard.PDDialog.SetFocus();
  545. }
  546. else
  547. {
  548. Keyboard.Focus(mainWindow.SearchBox);
  549. }
  550. }
  551. public static void HideApp()
  552. {
  553. if (appData.AppConfig.AppAnimation)
  554. {
  555. appData.AppConfig.IsShow = false;
  556. }
  557. else
  558. {
  559. appData.AppConfig.IsShow = null;
  560. HideAppVis();
  561. }
  562. }
  563. private static void HideAppVis()
  564. {
  565. //关闭锁定
  566. RunTimeStatus.LOCK_APP_PANEL = false;
  567. if (RunTimeStatus.SEARCH_BOX_SHOW)
  568. {
  569. mainWindow.HidedSearchBox();
  570. }
  571. mainWindow.Visibility = Visibility.Collapsed;
  572. //if (!MarginHide.IS_HIDE)
  573. //{
  574. //}
  575. //else
  576. //{
  577. // ShowApp();
  578. //}
  579. }
  580. /// <summary>
  581. /// 淡入淡出效果
  582. /// </summary>
  583. /// <param name="opacity"></param>
  584. /// <param name="milliseconds"></param>
  585. /// <param name="visibility"></param>
  586. public static void FadeStoryBoard(int opacity, int milliseconds, Visibility visibility)
  587. {
  588. if (appData.AppConfig.AppAnimation)
  589. {
  590. DoubleAnimation opacityAnimation = new DoubleAnimation
  591. {
  592. From = mainWindow.Opacity,
  593. To = opacity,
  594. Duration = new Duration(TimeSpan.FromMilliseconds(milliseconds))
  595. };
  596. opacityAnimation.Completed += (s, e) =>
  597. {
  598. mainWindow.BeginAnimation(OpacityProperty, null);
  599. if (visibility == Visibility.Visible)
  600. {
  601. mainWindow.Opacity = 1;
  602. }
  603. else
  604. {
  605. mainWindow.Opacity = 0;
  606. CommonCode.SortIconList();
  607. }
  608. };
  609. Timeline.SetDesiredFrameRate(opacityAnimation, 60);
  610. mainWindow.BeginAnimation(OpacityProperty, opacityAnimation);
  611. }
  612. else
  613. {
  614. //防止关闭动画后 窗体仍是0透明度
  615. mainWindow.Opacity = 1;
  616. mainWindow.Visibility = visibility;
  617. if (visibility == Visibility.Collapsed)
  618. {
  619. CommonCode.SortIconList();
  620. }
  621. }
  622. }
  623. /// <summary>
  624. /// 图片图标单击事件
  625. /// </summary>
  626. /// <param name="sender"></param>
  627. /// <param name="e"></param>
  628. private void NotifyIcon_Click(object sender, RoutedEventArgs e)
  629. {
  630. if (CheckShouldShowApp())
  631. {
  632. ShowApp();
  633. }
  634. else
  635. {
  636. HideApp();
  637. }
  638. }
  639. private static bool CheckShouldShowApp()
  640. {
  641. return mainWindow.Visibility == Visibility.Collapsed
  642. || mainWindow.Opacity == 0
  643. || MarginHide.IS_HIDE
  644. || !WindowUtil.WindowIsTop(mainWindow);
  645. }
  646. /// <summary>
  647. /// 右键任务栏图标 设置
  648. /// </summary>
  649. /// <param name="sender"></param>
  650. /// <param name="e"></param>
  651. private void ConfigApp(object sender, RoutedEventArgs e)
  652. {
  653. ConfigWindow.Show(appData.AppConfig, this);
  654. }
  655. /// <summary>
  656. /// 右键任务栏图标打开程序目录
  657. /// </summary>
  658. /// <param name="sender"></param>
  659. /// <param name="e"></param>
  660. private void OpenThisDir(object sender, RoutedEventArgs e)
  661. {
  662. Process p = new Process();
  663. p.StartInfo.FileName = "Explorer.exe";
  664. p.StartInfo.Arguments = "/e,/select," + Constants.APP_DIR + Constants.MY_NAME + ".exe";
  665. p.Start();
  666. }
  667. /// <summary>
  668. /// 设置图标
  669. /// </summary>
  670. /// <param name="sender"></param>
  671. /// <param name="e"></param>
  672. private void ConfigButtonClick(object sender, RoutedEventArgs e)
  673. {
  674. SettingMenus.IsOpen = true;
  675. }
  676. /// <summary>
  677. /// 设置菜单点击
  678. /// </summary>
  679. /// <param name="sender"></param>
  680. /// <param name="e"></param>
  681. private void ConfigMenuClick(object sender, RoutedEventArgs e)
  682. {
  683. ConfigWindow.Show(appData.AppConfig, this);
  684. }
  685. /// <summary>
  686. /// 待办任务
  687. /// </summary>
  688. /// <param name="sender"></param>
  689. /// <param name="e"></param>
  690. private void BacklogMenuClick(object sender, RoutedEventArgs e)
  691. {
  692. ToDoWindow.Show();
  693. }
  694. /// <summary>
  695. /// 禁用设置按钮右键菜单
  696. /// </summary>
  697. /// <param name="sender"></param>
  698. /// <param name="e"></param>
  699. private void SettingButton_Initialized(object sender, EventArgs e)
  700. {
  701. SettingButton.ContextMenu = null;
  702. }
  703. private void AppWindowLostFocus()
  704. {
  705. if (appData.AppConfig.AppHideType == AppHideType.LOST_FOCUS
  706. && this.Opacity == 1 && !RunTimeStatus.LOCK_APP_PANEL)
  707. {
  708. //如果开启了贴边隐藏 则窗体不贴边才隐藏窗口
  709. if (!appData.AppConfig.MarginHide || (appData.AppConfig.MarginHide && !MarginHide.IS_HIDE))
  710. {
  711. HideApp();
  712. }
  713. }
  714. }
  715. private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
  716. {
  717. if (this.DataContext != null)
  718. {
  719. AppData appData = this.DataContext as AppData;
  720. appData.AppConfig.WindowWidth = this.Width;
  721. appData.AppConfig.WindowHeight = this.Height;
  722. }
  723. if (guideRun)
  724. {
  725. Guide();
  726. }
  727. }
  728. /// <summary>
  729. /// 右键任务栏图标退出
  730. /// </summary>
  731. /// <param name="sender"></param>
  732. /// <param name="e"></param>
  733. private void ExitApp(object sender, RoutedEventArgs e)
  734. {
  735. if (appData.AppConfig.MouseMiddleShow || appData.AppConfig.SecondsWindow == true)
  736. {
  737. MouseHookThread.Dispose();
  738. }
  739. if (appData.AppConfig.EnableEveryThing == true)
  740. {
  741. EveryThingUtil.DisableEveryThing();
  742. }
  743. Application.Current.Shutdown();
  744. }
  745. /// <summary>
  746. /// 重启
  747. /// </summary>
  748. /// <param name="sender"></param>
  749. /// <param name="e"></param>
  750. public void ReStartApp(object sender, RoutedEventArgs e)
  751. {
  752. ProcessUtil.ReStartApp();
  753. }
  754. /// <summary>
  755. /// 关闭托盘图标
  756. /// </summary>
  757. /// <param name="sender"></param>
  758. /// <param name="e"></param>
  759. private void CloseBarIcon(object sender, RoutedEventArgs e)
  760. {
  761. appData.AppConfig.ShowBarIcon = false;
  762. }
  763. public void OnKeyDown(object sender, KeyEventArgs e)
  764. {
  765. //char c = (char)e.Key;
  766. if (e.Key == Key.Escape)
  767. {
  768. HideApp();
  769. }
  770. if (RunTimeStatus.SEARCH_BOX_SHOW && (e.Key == Key.Up
  771. || e.Key == Key.Down
  772. || e.Key == Key.Tab
  773. || e.Key == Key.Enter
  774. ))
  775. {
  776. if (e.Key == Key.Down || e.Key == Key.Tab)
  777. {
  778. SearchResControl res = RightCard.VerticalCard.Content as SearchResControl;
  779. res.SearchListBoxIndexAdd();
  780. }
  781. else if (e.Key == Key.Up)
  782. {
  783. SearchResControl res = RightCard.VerticalCard.Content as SearchResControl;
  784. res.SearchListBoxIndexSub();
  785. }
  786. else if (e.Key == Key.Enter)
  787. {
  788. SearchResControl res = RightCard.VerticalCard.Content as SearchResControl;
  789. res.StartupSelectionItem();
  790. }
  791. }
  792. }
  793. /// <summary>
  794. /// 为了让修改菜单的textBox失去焦点
  795. /// </summary>
  796. /// <param name="sender"></param>
  797. /// <param name="e"></param>
  798. private void MainWindow_MouseDown(object sender, MouseButtonEventArgs e)
  799. {
  800. SearchBox.Focus();
  801. }
  802. /// <summary>
  803. /// 鼠标进入后
  804. /// </summary>
  805. /// <param name="sender"></param>
  806. /// <param name="e"></param>
  807. private void MainWindow_MouseEnter(object sender, MouseEventArgs e)
  808. {
  809. //防止延迟贴边隐藏
  810. RunTimeStatus.MARGIN_HIDE_AND_OTHER_SHOW = false;
  811. }
  812. /// <summary>
  813. /// 打开屏幕拾色器
  814. /// </summary>
  815. /// <param name="sender"></param>
  816. /// <param name="e"></param>
  817. private void ColorPicker(object sender, RoutedEventArgs e)
  818. {
  819. TaskbarContextMenu.IsOpen = false;
  820. GlobalColorPickerWindow.CreateNoShow();
  821. }
  822. private void Window_GotFocus(object sender, RoutedEventArgs e)
  823. {
  824. // 如果没有在修改菜单 并且不是右键点击了面板
  825. if (!RunTimeStatus.IS_MENU_EDIT
  826. && !RunTimeStatus.SHOW_RIGHT_BTN_MENU
  827. && !RunTimeStatus.APP_BTN_IS_DOWN)
  828. {
  829. if (RunTimeStatus.SHOW_MENU_PASSWORDBOX)
  830. {
  831. //必须在其它文本框没有工作的时候才给密码框焦点
  832. RightCard.PDDialog.SetFocus();
  833. }
  834. else
  835. {
  836. //必须在其它文本框没有工作的时候才给搜索框焦点
  837. Keyboard.Focus(SearchBox);
  838. }
  839. }
  840. }
  841. private void AppWindow_Deactivated(object sender, EventArgs e)
  842. {
  843. AppWindowLostFocus();
  844. }
  845. /// <summary>
  846. /// 备份数据文件
  847. /// </summary>
  848. /// <param name="sender"></param>
  849. /// <param name="e"></param>
  850. [Obsolete]
  851. private void BakDataFile(object sender, RoutedEventArgs e)
  852. {
  853. Thread t = new Thread(() =>
  854. {
  855. CommonCode.BakAppData();
  856. });
  857. t.ApartmentState = ApartmentState.STA;
  858. t.Start();
  859. }
  860. private void AppButton_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
  861. {
  862. //点击了面板
  863. RunTimeStatus.APP_BTN_IS_DOWN = true;
  864. new Thread(() =>
  865. {
  866. Thread.Sleep(50);
  867. RunTimeStatus.APP_BTN_IS_DOWN = false;
  868. }).Start();
  869. }
  870. private ICommand _hideCommand;
  871. public ICommand HideCommand
  872. {
  873. get
  874. {
  875. if (_hideCommand == null)
  876. {
  877. _hideCommand = new RelayCommand(
  878. p =>
  879. {
  880. return true;
  881. },
  882. p =>
  883. {
  884. HideAppVis();
  885. });
  886. }
  887. return _hideCommand;
  888. }
  889. }
  890. protected override void OnSourceInitialized(EventArgs e)
  891. {
  892. base.OnSourceInitialized(e);
  893. HwndSource hwndSource = PresentationSource.FromVisual(this) as HwndSource;
  894. if (hwndSource != null)
  895. {
  896. IntPtr handle = hwndSource.Handle;
  897. hwndSource.AddHook(new HwndSourceHook(WndProc));
  898. }
  899. }
  900. IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
  901. {
  902. if (msg == MessageUtil.WM_COPYDATA)
  903. {
  904. MessageUtil.CopyDataStruct cds = (MessageUtil.CopyDataStruct)System.Runtime.InteropServices.Marshal.PtrToStructure(lParam, typeof(MessageUtil.CopyDataStruct));
  905. if ("ShowApp".Equals(cds.msg))
  906. {
  907. ShowApp();
  908. }
  909. }
  910. return hwnd;
  911. }
  912. #region 新手引导
  913. private int guideIndex = 0;
  914. private bool guideRun = false;
  915. private void Guide()
  916. {
  917. try
  918. {
  919. guideRun = true;
  920. //防止影响主程序进程
  921. if (CheckShouldShowApp())
  922. {
  923. ShowApp();
  924. }
  925. GrayBorder.Visibility = Visibility.Visible;
  926. GuideSwitch(guideIndex);
  927. GuideCard.Visibility = Visibility.Visible;
  928. }
  929. catch (Exception) { guideRun = false; }
  930. }
  931. private void GuideSwitch(int index)
  932. {
  933. guideIndex = index;
  934. GuideNum.Text = Convert.ToString(index + 1);
  935. GuideTitle1.Text = GuideInfoList.mainWindowGuideList[index].Title1;
  936. GuideTitle2.Text = GuideInfoList.mainWindowGuideList[index].Title2;
  937. GuideText.Text = GuideInfoList.mainWindowGuideList[index].GuideText;
  938. if (index == 0)
  939. {
  940. PreviewGuideBtn.Visibility = Visibility.Collapsed;
  941. NextGuideBtn.Content = "下一步";
  942. } else if (index > 0 && index < GuideInfoList.mainWindowGuideList.Count - 1)
  943. {
  944. PreviewGuideBtn.Visibility = Visibility.Visible;
  945. NextGuideBtn.Content = "下一步";
  946. } else
  947. {
  948. NextGuideBtn.Content = "完成";
  949. }
  950. switch (index)
  951. {
  952. default: //0 //右侧列表区域
  953. Point point = RightCard.TransformToAncestor(this).Transform(new Point(0, 0));
  954. //内部中上
  955. GrayBoderClip(point.X, point.Y, RightCard.ActualWidth, RightCard.ActualHeight,
  956. new Thickness(point.X + RightCard.ActualWidth / 2 - GuideCard.ActualWidth / 2, point.Y, 0, 0));
  957. break;
  958. case 1: //左侧菜单
  959. Point leftCardPoint = LeftCard.TransformToAncestor(this).Transform(new Point(0, 0));
  960. GrayBoderClip(leftCardPoint.X , leftCardPoint.Y , LeftCard.ActualWidth, LeftCard.ActualHeight,
  961. // 外部中下侧
  962. new Thickness(leftCardPoint.X + LeftCard.ActualWidth,
  963. leftCardPoint.Y + LeftCard.ActualHeight / 2 - GuideCard.ActualHeight / 2, 0, 0));
  964. break;
  965. case 2: //头部拖拽栏
  966. GrayBoderClip(0, 0, this.Width, 50,
  967. // 外部中下侧
  968. new Thickness(this.Width / 2 - GuideCard.ActualWidth / 2, 50, 0, 0));
  969. break;
  970. case 3:
  971. Point mainBtnPoint = MainBtnPanel.TransformToAncestor(this).Transform(new Point(0, 0));
  972. GrayBoderClip(mainBtnPoint.X, mainBtnPoint.Y, MainBtnPanel.ActualWidth, MainBtnPanel.ActualHeight,
  973. // 外部左下侧
  974. new Thickness(mainBtnPoint.X - GuideCard.Width,
  975. mainBtnPoint.Y, 0, 0));
  976. break;
  977. }
  978. }
  979. private void GrayBoderClip(double x, double y, double w, double h, Thickness margin)
  980. {
  981. PathGeometry borGeometry = new PathGeometry();
  982. RectangleGeometry rg = new RectangleGeometry();
  983. rg.Rect = new Rect(0, 0, this.Width, this.Height);
  984. borGeometry = Geometry.Combine(borGeometry, rg, GeometryCombineMode.Union, null);
  985. GrayBorder.Clip = borGeometry;
  986. RectangleGeometry rg1 = new RectangleGeometry();
  987. rg1.Rect = new Rect(x - 20, y - 20, w, h);
  988. borGeometry = Geometry.Combine(borGeometry, rg1, GeometryCombineMode.Exclude, null);
  989. GuideCard.Margin = margin;
  990. GrayBorder.Clip = borGeometry;
  991. }
  992. private void PreviewGuideBtn_Click(object sender, RoutedEventArgs e)
  993. {
  994. int index = Convert.ToInt32(GuideNum.Text.ToString()) - 1;
  995. int previewIndex = index - 1;
  996. GuideSwitch(previewIndex);
  997. }
  998. private void NextGuideBtn_Click(object sender, RoutedEventArgs e)
  999. {
  1000. if ("完成".Equals(NextGuideBtn.Content.ToString())) {
  1001. GrayBorder.Visibility = Visibility.Collapsed;
  1002. GuideCard.Visibility = Visibility.Collapsed;
  1003. guideIndex = 0;
  1004. guideRun = false;
  1005. return;
  1006. }
  1007. int index = Convert.ToInt32(GuideNum.Text.ToString()) - 1;
  1008. int nextIndex = index + 1;
  1009. GuideSwitch(nextIndex);
  1010. }
  1011. #endregion
  1012. private void Guide_Click(object sender, RoutedEventArgs e)
  1013. {
  1014. Guide();
  1015. }
  1016. }
  1017. }