1616using WindowTextExtractor . Extensions ;
1717using WindowTextExtractor . Utils ;
1818using WindowTextExtractor . Diagnostics ;
19+ using WindowTextExtractor . Settings ;
1920using WindowTextExtractor . Native ;
2021using WindowTextExtractor . Native . Enums ;
2122using WindowTextExtractor . Native . Structs ;
@@ -25,14 +26,6 @@ namespace WindowTextExtractor.Forms
2526{
2627 public partial class MainForm : Form , IMessageFilter
2728 {
28- private const string DEFAULT_VIDEO_FILE_NAME = "Window.avi" ;
29- private const string DEFAULT_FONT_NAME = "Courier New" ;
30- private const int DEFAULT_FONT_SIZE = 10 ;
31- private const int DEFAULT_FPS = 12 ;
32- private const decimal DEFAULT_SCALE = 1 ;
33- private const int DEFAULT_BORDER_WIDTH = 10 ;
34- private readonly Color DEAFULT_BORDER_COLOR = Color . Blue ;
35-
3629 private readonly int _processId ;
3730 private readonly int _messageId ;
3831 private bool _isButtonTargetMouseDown ;
@@ -46,18 +39,13 @@ public partial class MainForm : Form, IMessageFilter
4639 private IntPtr _windowHandle ;
4740 private int _windowProcessId ;
4841 private string _listText ;
49- private int _fps ;
50- private decimal _scale ;
51- private bool _refreshImage ;
5242 private bool _imageTab ;
5343 private bool _isRecording ;
54- private bool _captureCursor ;
5544 private DateTime ? _startRecordingTime ;
5645 private Bitmap _image ;
5746 private Graphics _graphics ;
5847 private Pen _pen ;
59- private int _borderWidth ;
60- private Color _borderColor ;
48+ private ApplicationSettings _settings ;
6149 private readonly object _lockObject ;
6250 private readonly VideoFileWriter _videoWriter ;
6351
@@ -84,24 +72,13 @@ public MainForm()
8472 _textFileName = string . Empty ;
8573 _textListFileName = string . Empty ;
8674 _imageFileName = string . Empty ;
87- _videoFileName = Path . Combine ( AssemblyUtils . AssemblyDirectory , DEFAULT_VIDEO_FILE_NAME ) ;
8875 _environmentFileName = string . Empty ;
8976 _windowHandle = IntPtr . Zero ;
9077 _windowProcessId = 0 ;
9178 _listText = string . Empty ;
92- _refreshImage = true ;
93- _captureCursor = true ;
9479 _imageTab = false ;
9580 _isRecording = false ;
9681 _startRecordingTime = null ;
97- _fps = DEFAULT_FPS ;
98- _scale = DEFAULT_SCALE ;
99- _borderWidth = DEFAULT_BORDER_WIDTH ;
100- _borderColor = DEAFULT_BORDER_COLOR ;
101- numericFps . Value = DEFAULT_FPS ;
102- numericScale . Value = DEFAULT_SCALE ;
103- cmbRefresh . SelectedIndex = 0 ;
104- cmbCaptureCursor . SelectedIndex = 0 ;
10582 _image = null ;
10683 _videoWriter = new VideoFileWriter ( ) ;
10784 }
@@ -110,21 +87,15 @@ protected override void OnLoad(EventArgs e)
11087 {
11188 base . OnLoad ( e ) ;
11289
113- Application . AddMessageFilter ( this ) ;
114-
115- MenuItemAlwaysOnTopClick ( this , EventArgs . Empty ) ;
116- OnContentChanged ( ) ;
117-
118- var font = new Font ( DEFAULT_FONT_NAME , DEFAULT_FONT_SIZE , FontStyle . Regular , GraphicsUnit . Point ) ;
119- if ( font . Name == DEFAULT_FONT_NAME )
90+ var isSettingsLoaded = LoadSettings ( ) ;
91+ if ( ! isSettingsLoaded )
12092 {
121- txtContent . Font = font ;
122- }
123- else
124- {
125- font . Dispose ( ) ;
93+ Application . Exit ( ) ;
12694 }
12795
96+ Application . AddMessageFilter ( this ) ;
97+ OnContentChanged ( ) ;
98+
12899 try
129100 {
130101 BindLanguages ( ) ;
@@ -148,7 +119,7 @@ protected override void OnLoad(EventArgs e)
148119 }
149120#endif
150121
151- InitTimers ( _fps ) ;
122+ InitTimers ( _settings . FPS ) ;
152123 _updateButtonTimer = new AccurateTimer ( UpdateButtonCallback , 500 ) ;
153124 }
154125
@@ -436,34 +407,76 @@ private void MenuItemFontClick(object sender, EventArgs e)
436407
437408 if ( dialog . ShowDialog ( ) != DialogResult . Cancel )
438409 {
439- txtContent . Font = dialog . Font ;
410+ lock ( _lockObject )
411+ {
412+ txtContent . Font = dialog . Font ;
413+ _settings . FontName = dialog . Font . Name ;
414+ _settings . FontSize = dialog . Font . Size ;
415+ _settings . FontStyle = dialog . Font . Style ;
416+ _settings . FontUnit = dialog . Font . Unit ;
417+ SaveSettings ( _settings ) ;
418+ }
440419 }
441420 }
442421
443422 private void MenuItemAlwaysOnTopClick ( object sender , EventArgs e )
444423 {
445- TopMost = ! TopMost ;
446- menuItemAlwaysOnTop . Checked = TopMost ;
424+ lock ( _lockObject )
425+ {
426+ TopMost = ! TopMost ;
427+ menuItemAlwaysOnTop . Checked = TopMost ;
428+ _settings . AlwaysOnTop = TopMost ;
429+ SaveSettings ( _settings ) ;
430+ }
447431 }
448432
449433 private void MenuItemCheckedClick ( object sender , EventArgs e )
450434 {
451- var menuItem = ( ToolStripMenuItem ) sender ;
452- menuItem . Checked = ! menuItem . Checked ;
435+ lock ( _lockObject )
436+ {
437+ var menuItem = ( ToolStripMenuItem ) sender ;
438+ menuItem . Checked = ! menuItem . Checked ;
439+ switch ( menuItem . Name )
440+ {
441+ case "menuItemAlwaysRefreshTabs" :
442+ {
443+ _settings . AlwaysRefreshTabs = menuItem . Checked ;
444+ }
445+ break ;
446+
447+ case "menuItemShowEmptyItems" :
448+ {
449+ _settings . ShowEmptyItems = menuItem . Checked ;
450+ }
451+ break ;
452+
453+ case "menuItemNotRepeated" :
454+ {
455+ _settings . NotRepeatedNewItems = menuItem . Checked ;
456+ }
457+ break ;
458+ }
459+ SaveSettings ( _settings ) ;
460+ }
453461 }
454462
455463 private void MenuItemShowTextListClick ( object sender , EventArgs e )
456464 {
457- menuItemShowTextList . Checked = ! menuItemShowTextList . Checked ;
458- if ( menuItemShowTextList . Checked )
459- {
460- splitTextContainer . Panel2Collapsed = false ;
461- splitTextContainer . Panel2 . Show ( ) ;
462- }
463- else
465+ lock ( _lockObject )
464466 {
465- splitTextContainer . Panel2Collapsed = true ;
466- splitTextContainer . Panel2 . Hide ( ) ;
467+ menuItemShowTextList . Checked = ! menuItemShowTextList . Checked ;
468+ if ( menuItemShowTextList . Checked )
469+ {
470+ splitTextContainer . Panel2Collapsed = false ;
471+ splitTextContainer . Panel2 . Show ( ) ;
472+ }
473+ else
474+ {
475+ splitTextContainer . Panel2Collapsed = true ;
476+ splitTextContainer . Panel2 . Hide ( ) ;
477+ }
478+ _settings . ShowTextList = menuItemShowTextList . Checked ;
479+ SaveSettings ( _settings ) ;
467480 }
468481 }
469482
@@ -480,22 +493,30 @@ private void MenuItemBorderColorClick(object sender, EventArgs e)
480493 AllowFullOpen = true ,
481494 AnyColor = true ,
482495 FullOpen = true ,
483- Color = _borderColor
496+ Color = ColorTranslator . FromHtml ( _settings . BorderColor )
484497 } ;
485498
486499 if ( dialog . ShowDialog ( ) != DialogResult . Cancel )
487500 {
488- _borderColor = dialog . Color ;
501+ lock ( _lockObject )
502+ {
503+ _settings . BorderColor = ColorTranslator . ToHtml ( dialog . Color ) ;
504+ SaveSettings ( _settings ) ;
505+ }
489506 }
490507 }
491508
492509 private void MenuItemBorderWidthClick ( object sender , EventArgs e )
493510 {
494- var borderWidthForm = new BorderWidthForm ( _borderWidth ) ;
511+ var borderWidthForm = new BorderWidthForm ( _settings . BorderWidth ) ;
495512 var result = borderWidthForm . ShowDialog ( this ) ;
496513 if ( result == DialogResult . OK )
497514 {
498- _borderWidth = borderWidthForm . BorderWidth ;
515+ lock ( _lockObject )
516+ {
517+ _settings . BorderWidth = borderWidthForm . BorderWidth ;
518+ SaveSettings ( _settings ) ;
519+ }
499520 }
500521 }
501522
@@ -567,7 +588,7 @@ private void ButtonRecordClick(object sender, EventArgs e)
567588 {
568589 try
569590 {
570- _videoWriter . Open ( _videoFileName , _image . Width , _image . Height , _fps , VideoCodec . Raw ) ;
591+ _videoWriter . Open ( _videoFileName , _image . Width , _image . Height , _settings . FPS , VideoCodec . Raw ) ;
571592 }
572593 catch ( Exception ex )
573594 {
@@ -660,31 +681,38 @@ private void ButtonBrowseFileClick(object sender, EventArgs e)
660681
661682 private void NumericFpsValueChanged ( object sender , EventArgs e )
662683 {
663- _fps = ( int ) ( ( NumericUpDown ) sender ) . Value ;
664- InitTimers ( _fps ) ;
684+ lock ( _lockObject )
685+ {
686+ _settings . FPS = ( int ) ( ( NumericUpDown ) sender ) . Value ;
687+ SaveSettings ( _settings ) ;
688+ InitTimers ( _settings . FPS ) ;
689+ }
665690 }
666691
667692 private void NumericScaleValueChanged ( object sender , EventArgs e )
668693 {
669694 lock ( _lockObject )
670695 {
671- _scale = ( ( NumericUpDown ) sender ) . Value ;
696+ _settings . Scale = ( ( NumericUpDown ) sender ) . Value ;
697+ SaveSettings ( _settings ) ;
672698 }
673699 }
674700
675701 private void ComboBoxRefreshSelectedIndexChanged ( object sender , EventArgs e )
676702 {
677703 lock ( _lockObject )
678704 {
679- _refreshImage = ( ( ComboBox ) sender ) . SelectedIndex == 0 ;
705+ _settings . RefreshImage = ( ( ComboBox ) sender ) . SelectedIndex == 0 ;
706+ SaveSettings ( _settings ) ;
680707 }
681708 }
682709
683710 private void ComboBoxCaptureCursorSelectedIndexChanged ( object sender , EventArgs e )
684711 {
685712 lock ( _lockObject )
686713 {
687- _captureCursor = ( ( ComboBox ) sender ) . SelectedIndex == 0 ;
714+ _settings . CaptureCursor = ( ( ComboBox ) sender ) . SelectedIndex == 0 ;
715+ SaveSettings ( _settings ) ;
688716 }
689717 }
690718
@@ -755,8 +783,8 @@ public bool PreFilterMessage(ref Message m)
755783 _graphics ? . Dispose ( ) ;
756784 _pen ? . Dispose ( ) ;
757785 _graphics = Graphics . FromHwnd ( windowHandle ) ;
758- _pen = new Pen ( _borderColor , _borderWidth ) ;
759- if ( _borderWidth > 0 )
786+ _pen = new Pen ( ColorTranslator . FromHtml ( _settings . BorderColor ) , _settings . BorderWidth ) ;
787+ if ( _settings . BorderWidth > 0 )
760788 {
761789 _graphics . DrawBorder ( windowHandle , _pen ) ;
762790 }
@@ -814,8 +842,8 @@ public bool PreFilterMessage(ref Message m)
814842 {
815843 _windowHandle = windowHandle ;
816844 _windowProcessId = element . Current . ProcessId ;
817- scale = _scale ;
818- captureCursor = _captureCursor ;
845+ scale = _settings . Scale ;
846+ captureCursor = _settings . CaptureCursor ;
819847 }
820848 if ( scale == 1m )
821849 {
@@ -880,11 +908,11 @@ private void CaptureWindowCallback()
880908
881909 lock ( _lockObject )
882910 {
883- scale = _scale ;
911+ scale = _settings . Scale ;
884912 windowHandle = _windowHandle ;
885913 imageTab = _imageTab ;
886914 isRecording = _isRecording ;
887- captureCursor = _captureCursor ;
915+ captureCursor = _settings . CaptureCursor ;
888916 }
889917
890918 var newImage = ( Bitmap ) null ;
@@ -941,7 +969,7 @@ private void UpdatePictureBoxCallback()
941969
942970 lock ( _lockObject )
943971 {
944- if ( _imageTab && _refreshImage )
972+ if ( _imageTab && _settings . RefreshImage )
945973 {
946974 image = ( Bitmap ) _image . Clone ( ) ;
947975 }
@@ -1159,6 +1187,46 @@ private void BindLanguages()
11591187 cmbLanguages . DataSource = OcrEngine . AvailableRecognizerLanguages . Select ( x => new { Text = x . DisplayName , Value = x . LanguageTag } ) . ToList ( ) ;
11601188 }
11611189
1190+ private bool LoadSettings ( )
1191+ {
1192+ try
1193+ {
1194+ _settings = ApplicationSettingsFile . Load ( ) ;
1195+ }
1196+ catch ( Exception e )
1197+ {
1198+ MessageBox . Show ( $ "Failed to read the settings.{ Environment . NewLine } { e . Message } ", "Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
1199+ return false ;
1200+ }
1201+
1202+ _videoFileName = Path . Combine ( AssemblyUtils . AssemblyDirectory , _settings . VideoFileName ) ;
1203+
1204+ numericFps . Value = _settings . FPS ;
1205+ numericScale . Value = _settings . Scale ;
1206+ TopMost = _settings . AlwaysOnTop ;
1207+ menuItemAlwaysOnTop . Checked = _settings . AlwaysOnTop ;
1208+ menuItemShowEmptyItems . Checked = _settings . ShowEmptyItems ;
1209+ menuItemShowTextList . Checked = _settings . ShowTextList ;
1210+ menuItemNotRepeated . Checked = _settings . NotRepeatedNewItems ;
1211+ menuItemAlwaysRefreshTabs . Checked = _settings . AlwaysRefreshTabs ;
1212+ cmbRefresh . SelectedIndex = _settings . RefreshImage ? 0 : 1 ;
1213+ cmbCaptureCursor . SelectedIndex = _settings . CaptureCursor ? 0 : 1 ;
1214+ txtContent . Font = new Font ( _settings . FontName , _settings . FontSize , _settings . FontStyle , _settings . FontUnit ) ;
1215+ return true ;
1216+ }
1217+
1218+ private void SaveSettings ( ApplicationSettings settings )
1219+ {
1220+ try
1221+ {
1222+ ApplicationSettingsFile . Save ( settings ) ;
1223+ }
1224+ catch ( Exception e )
1225+ {
1226+ MessageBox . Show ( $ "Failed to save the settings.{ Environment . NewLine } { e . Message } ", "Error" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
1227+ }
1228+ }
1229+
11621230 private void OnCurrentDomainUnhandledException ( object sender , UnhandledExceptionEventArgs e )
11631231 {
11641232 var ex = e . ExceptionObject as Exception ?? new Exception ( "OnCurrentDomainUnhandledException" ) ;
0 commit comments