55using System . Runtime . InteropServices ;
66using System . IO ;
77using System . Drawing ;
8+ using System . Text ;
89using WindowTextExtractor . Extensions ;
910using WindowTextExtractor . Utils ;
1011
@@ -19,6 +20,7 @@ public partial class MainForm : Form, IMessageFilter
1920 private readonly int _messageId ;
2021 private bool _isButtonTargetMouseDown ;
2122 private string _64BitFilePath ;
23+ private string _fileName ;
2224
2325 public MainForm ( )
2426 {
@@ -27,6 +29,7 @@ public MainForm()
2729 _processId = Process . GetCurrentProcess ( ) . Id ;
2830 _messageId = NativeMethods . RegisterWindowMessage ( "WINDOW_TEXT_EXTRACTOR_HOOK" ) ;
2931 _64BitFilePath = "" ;
32+ _fileName = "" ;
3033 }
3134
3235 protected override void OnLoad ( EventArgs e )
@@ -36,6 +39,7 @@ protected override void OnLoad(EventArgs e)
3639 Application . AddMessageFilter ( this ) ;
3740
3841 menuItemAlwaysOnTop_Click ( this , EventArgs . Empty ) ;
42+ OnTextContentChanged ( ) ;
3943
4044 var font = new Font ( DEFAULT_FONT_NAME , DEFAULT_FONT_SIZE , FontStyle . Regular , GraphicsUnit . Point ) ;
4145 if ( font . Name == DEFAULT_FONT_NAME )
@@ -104,12 +108,32 @@ private void btnTarget_MouseDown(object sender, MouseEventArgs e)
104108
105109 private void txtContent_TextChanged ( object sender , EventArgs e )
106110 {
107- UpdateStatusBar ( ) ;
111+ OnTextContentChanged ( ) ;
108112 }
109113
110114 private void txtContent_MultilineChanged ( object sender , EventArgs e )
111115 {
112- UpdateStatusBar ( ) ;
116+ OnTextContentChanged ( ) ;
117+ }
118+
119+ private void menuItemSaveFileAs_Click ( object sender , EventArgs e )
120+ {
121+ var dialog = new SaveFileDialog
122+ {
123+ OverwritePrompt = true ,
124+ ValidateNames = true ,
125+ Title = "Save As" ,
126+ FileName = File . Exists ( _fileName ) ? Path . GetFileName ( _fileName ) : "*.txt" ,
127+ DefaultExt = "txt" ,
128+ RestoreDirectory = false ,
129+ Filter = "Text Documents (.txt)|*.txt"
130+ } ;
131+
132+ if ( dialog . ShowDialog ( ) != System . Windows . Forms . DialogResult . Cancel )
133+ {
134+ _fileName = dialog . FileName ;
135+ File . WriteAllText ( _fileName , txtContent . Text , Encoding . UTF8 ) ;
136+ }
113137 }
114138
115139 private void menuItemExit_Click ( object sender , EventArgs e )
@@ -151,7 +175,7 @@ protected override void WndProc(ref Message m)
151175 var password = Marshal . PtrToStringAuto ( cds . lpData ) ;
152176 txtContent . Text = password ;
153177 txtContent . ScrollTextToEnd ( ) ;
154- UpdateStatusBar ( ) ;
178+ OnTextContentChanged ( ) ;
155179 }
156180 break ;
157181 }
@@ -214,7 +238,7 @@ public bool PreFilterMessage(ref Message m)
214238 var text = element . GetTextFromConsole ( ) ?? element . GetTextFromWindow ( ) ;
215239 txtContent . Text = text == null ? "" : text . TrimEnd ( ) . TrimEnd ( Environment . NewLine ) ;
216240 txtContent . ScrollTextToEnd ( ) ;
217- UpdateStatusBar ( ) ;
241+ OnTextContentChanged ( ) ;
218242 }
219243 }
220244 }
@@ -229,10 +253,11 @@ public bool PreFilterMessage(ref Message m)
229253 return false ;
230254 }
231255
232- private void UpdateStatusBar ( )
256+ private void OnTextContentChanged ( )
233257 {
234258 lblTotalChars . Text = "Total Chars: " + txtContent . Text . Length ;
235259 lblTotalLines . Text = "Total Lines: " + txtContent . Text . Split ( new string [ ] { Environment . NewLine } , StringSplitOptions . None ) . Length ;
260+ menuItemSaveFileAs . Enabled = txtContent . Text . Length > 0 ;
236261 }
237262 }
238263}
0 commit comments