11using System ;
2- using System . Collections . Generic ;
3- using System . ComponentModel ;
4- using System . Data ;
5- using System . Drawing ;
6- using System . Linq ;
7- using System . Text ;
82using System . Windows . Forms ;
93using System . Diagnostics ;
104using System . Windows . Automation ;
11- using System . Windows . Automation . Text ;
5+ using System . Runtime . InteropServices ;
126using WindowTextExtractor . Extensions ;
137
148namespace WindowTextExtractor . Forms
159{
1610 public partial class MainForm : Form , IMessageFilter
1711 {
1812 private readonly int _processId ;
19- private bool _isButtonTargetMouseDown ;
20- private Cursor _targetCursor ;
21- private Cursor _currentCursor ;
13+ private readonly int _messageId ;
14+ private bool _isButtonTargetTextMouseDown ;
15+ private bool _isButtonTargetPasswordMouseDown ;
16+ private Cursor _targetTextCursor ;
17+ private Cursor _targetPasswordCursor ;
2218
2319 public MainForm ( )
2420 {
2521 InitializeComponent ( ) ;
26- _isButtonTargetMouseDown = false ;
27- _targetCursor = new Cursor ( Properties . Resources . Target . Handle ) ;
22+ _isButtonTargetTextMouseDown = false ;
23+ _isButtonTargetPasswordMouseDown = false ;
24+ _targetTextCursor = new Cursor ( Properties . Resources . TargetText . Handle ) ;
25+ _targetPasswordCursor = new Cursor ( Properties . Resources . TargetPassword . Handle ) ;
2826 _processId = Process . GetCurrentProcess ( ) . Id ;
27+ _messageId = NativeMethods . RegisterWindowMessage ( "WINDOW_TEXT_EXTRACTOR_HOOK" ) ;
2928 }
3029
3130 protected override void OnLoad ( EventArgs e )
@@ -40,13 +39,25 @@ protected override void OnClosed(EventArgs e)
4039 Application . RemoveMessageFilter ( this ) ;
4140 }
4241
43- private void btnTarget_MouseDown ( object sender , MouseEventArgs e )
42+ private void btnTargetText_MouseDown ( object sender , MouseEventArgs e )
4443 {
45- if ( ! _isButtonTargetMouseDown )
44+ if ( ! _isButtonTargetTextMouseDown )
4645 {
47- _isButtonTargetMouseDown = true ;
48- _currentCursor = Cursor . Current ;
49- Cursor . Current = _targetCursor ;
46+ _isButtonTargetTextMouseDown = true ;
47+ Cursor . Current = _targetTextCursor ;
48+ if ( ! TopMost )
49+ {
50+ SendToBack ( ) ;
51+ }
52+ }
53+ }
54+
55+ private void btnTargetPassword_MouseDown ( object sender , MouseEventArgs e )
56+ {
57+ if ( ! _isButtonTargetPasswordMouseDown )
58+ {
59+ _isButtonTargetPasswordMouseDown = true ;
60+ Cursor . Current = _targetPasswordCursor ;
5061 if ( ! TopMost )
5162 {
5263 SendToBack ( ) ;
@@ -93,16 +104,35 @@ private void menuItemAbout_Click(object sender, EventArgs e)
93104 dialog . ShowDialog ( this ) ;
94105 }
95106
107+ protected override void WndProc ( ref Message m )
108+ {
109+ switch ( m . Msg )
110+ {
111+ case NativeConstants . WM_COPYDATA :
112+ {
113+ var cds = ( CopyDataStruct ) Marshal . PtrToStructure ( m . LParam , typeof ( CopyDataStruct ) ) ;
114+ var password = Marshal . PtrToStringAuto ( cds . lpData ) ;
115+ txtContent . Text = password ;
116+ txtContent . ScrollTextToEnd ( ) ;
117+ UpdateStatusBar ( ) ;
118+ }
119+ break ;
120+ }
121+
122+ base . WndProc ( ref m ) ;
123+ }
124+
96125 public bool PreFilterMessage ( ref Message m )
97126 {
98- if ( _isButtonTargetMouseDown )
127+ if ( _isButtonTargetTextMouseDown || _isButtonTargetPasswordMouseDown )
99128 {
100129 switch ( m . Msg )
101130 {
102131 case NativeConstants . WM_LBUTTONUP :
103132 {
104- _isButtonTargetMouseDown = false ;
105- Cursor . Current = _currentCursor ;
133+ _isButtonTargetTextMouseDown = false ;
134+ _isButtonTargetPasswordMouseDown = false ;
135+ Cursor . Current = Cursors . Default ;
106136 if ( ! TopMost )
107137 {
108138 BringToFront ( ) ;
@@ -115,11 +145,23 @@ public bool PreFilterMessage(ref Message m)
115145 {
116146 var cursorPosition = System . Windows . Forms . Cursor . Position ;
117147 var element = AutomationElement . FromPoint ( new System . Windows . Point ( cursorPosition . X , cursorPosition . Y ) ) ;
118- if ( element != null && ! element . Current . IsPassword && element . Current . ProcessId != _processId )
148+ if ( element != null && element . Current . ProcessId != _processId )
119149 {
120- txtContent . Text = element . GetTextFromConsole ( ) ?? element . GetTextFromWindow ( ) ;
121- txtContent . ScrollTextToEnd ( ) ;
122- UpdateStatusBar ( ) ;
150+ if ( element . Current . IsPassword && _isButtonTargetPasswordMouseDown )
151+ {
152+ var elementHandle = new IntPtr ( element . Current . NativeWindowHandle ) ;
153+ NativeMethods . SetHook ( Handle , elementHandle , _messageId ) ;
154+ NativeMethods . QueryPasswordEdit ( ) ;
155+ NativeMethods . UnsetHook ( Handle , elementHandle ) ;
156+ }
157+
158+ if ( ! element . Current . IsPassword && _isButtonTargetTextMouseDown )
159+ {
160+ var text = element . GetTextFromConsole ( ) ?? element . GetTextFromWindow ( ) ;
161+ txtContent . Text = text ;
162+ txtContent . ScrollTextToEnd ( ) ;
163+ UpdateStatusBar ( ) ;
164+ }
123165 }
124166 }
125167 catch
@@ -128,6 +170,7 @@ public bool PreFilterMessage(ref Message m)
128170 } break ;
129171 }
130172 }
173+
131174 return false ;
132175 }
133176
@@ -137,4 +180,4 @@ private void UpdateStatusBar()
137180 lblTotalLines . Text = "Total Lines: " + txtContent . Text . Split ( new string [ ] { Environment . NewLine } , StringSplitOptions . None ) . Length ;
138181 }
139182 }
140- }
183+ }
0 commit comments