MainWindow.xaml.cs 32 KB

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