2020using StickyHomeworks . Services ;
2121using StickyHomeworks . ViewModels ;
2222using StickyHomeworks . Views ;
23- // using System.Windows.Automation;
23+ using System . Windows . Automation ;
2424using System . Windows . Forms ;
25+ using System . Windows . Threading ;
26+ using Stfu . Linq ;
2527using DataFormats = System . Windows . DataFormats ;
2628
2729namespace StickyHomeworks ;
@@ -44,7 +46,7 @@ public MainWindow(ProfileService profileService,
4446 {
4547 ProfileService = profileService ;
4648 SettingsService = settingsService ;
47- // Automation.AddAutomationFocusChangedEventHandler(OnFocusChangedHandler);
49+ Automation . AddAutomationFocusChangedEventHandler ( OnFocusChangedHandler ) ;
4850 InitializeComponent ( ) ;
4951 ViewModel . PropertyChanged += ViewModelOnPropertyChanged ;
5052 ViewModel . PropertyChanging += ViewModelOnPropertyChanging ;
@@ -71,31 +73,31 @@ private void ViewModelOnPropertyChanged(object? sender, PropertyChangedEventArgs
7173 }
7274 }
7375
74- // private void OnFocusChangedHandler(object sender, AutomationFocusChangedEventArgs e)
75- // {
76- // if (!ViewModel.IsDrawerOpened)
77- // return;
78- // try
79- // {
80-
81- // var element = sender as AutomationElement;
82- // if (element == null)
83- // return;
84- // var hWnd = NativeWindowHelper.GetForegroundWindow();
85- // NativeWindowHelper.GetWindowThreadProcessId(hWnd, out var id);
86- // using var proc = Process.GetProcessById(id);
87- // Debug.WriteLine($"{proc.ProcessName} {e.EventId.ProgrammaticName}");
88- // if (proc.Id != Environment.ProcessId &&
89- // !new List<string>(["ctfmon", "textinputhost", "chsime"]).Contains(proc.ProcessName.ToLower()))
90- // {
91- // Dispatcher.Invoke(() => ExitEditingMode());
92- // }
93- // }
94- // catch
95- // {
96- // // ignored
97- // }
98- // }
76+ private void OnFocusChangedHandler ( object sender , AutomationFocusChangedEventArgs e )
77+ {
78+ if ( ! ViewModel . IsDrawerOpened )
79+ return ;
80+ try
81+ {
82+
83+ var element = sender as AutomationElement ;
84+ if ( element == null )
85+ return ;
86+ var hWnd = NativeWindowHelper . GetForegroundWindow ( ) ;
87+ NativeWindowHelper . GetWindowThreadProcessId ( hWnd , out var id ) ;
88+ using var proc = Process . GetProcessById ( id ) ;
89+ Debug . WriteLine ( $ "{ proc . ProcessName } { e . EventId . ProgrammaticName } ") ;
90+ if ( proc . Id != Environment . ProcessId &&
91+ ! new List < string > ( [ "ctfmon" , "textinputhost" , "chsime" ] ) . Contains ( proc . ProcessName . ToLower ( ) ) )
92+ {
93+ Dispatcher . Invoke ( ( ) => ExitEditingMode ( ) ) ;
94+ }
95+ }
96+ catch
97+ {
98+ // ignored
99+ }
100+ }
99101
100102 private void ExitEditingMode ( bool hard = true )
101103 {
@@ -387,6 +389,8 @@ private async void ButtonExport_OnClick(object sender, RoutedEventArgs e)
387389 }
388390
389391 ExitEditingMode ( ) ;
392+ //MainListView.Background = (Brush)FindResource("MaterialDesignPaper");
393+ await System . Windows . Threading . Dispatcher . Yield ( DispatcherPriority . Render ) ;
390394 var file = dialog . FileName ! ;
391395 var visual = new DrawingVisual ( ) ;
392396 var s = SettingsService . Settings . Scale ;
@@ -396,6 +400,8 @@ private async void ButtonExport_OnClick(object sender, RoutedEventArgs e)
396400 {
397401 Stretch = Stretch . None
398402 } ;
403+ var bg = ( Brush ) FindResource ( "MaterialDesignPaper" ) ;
404+ context . DrawRectangle ( bg , null , new Rect ( 0 , 0 , MainListView . ActualWidth * s , MainListView . ActualHeight * s ) ) ;
399405 context . DrawRectangle ( brush , null , new Rect ( 0 , 0 , MainListView . ActualWidth * s , MainListView . ActualHeight * s ) ) ;
400406 context . Close ( ) ;
401407 }
@@ -425,7 +431,8 @@ private async void ButtonExport_OnClick(object sender, RoutedEventArgs e)
425431 ViewModel . SnackbarMessageQueue . Enqueue ( $ "导出失败:{ ex } ") ;
426432 }
427433
428- done :
434+ done :
435+ //MainListView.Background = null;
429436 dialog . Dispose ( ) ;
430437 ViewModel . IsWorking = false ;
431438 }
0 commit comments