Skip to content

Commit b02b490

Browse files
committed
Merge branch 'TextFromPasswordWindow'
2 parents 3e564a4 + 67092cd commit b02b490

26 files changed

+1939
-2703
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.vs/
2+
13
## Ignore Visual Studio temporary files, build results, and
24
## files generated by popular Visual Studio add-ons.
35

WindowTextExtractor.sln

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,49 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio 2013
4-
VisualStudioVersion = 12.0.40629.0
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.28307.852
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "WindowTextExtractor", "WindowTextExtractor\WindowTextExtractor.csproj", "{784E5D61-B08C-47E4-936A-03C9F67B71D0}"
77
EndProject
8+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "WindowTextExtractorHook", "WindowTextExtractorHook\WindowTextExtractorHook.vcxproj", "{5A4E4384-EF85-4E57-BFD6-ED0F99386E34}"
9+
EndProject
810
Global
911
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1012
Debug|Any CPU = Debug|Any CPU
13+
Debug|x64 = Debug|x64
14+
Debug|x86 = Debug|x86
1115
Release|Any CPU = Release|Any CPU
16+
Release|x64 = Release|x64
17+
Release|x86 = Release|x86
1218
EndGlobalSection
1319
GlobalSection(ProjectConfigurationPlatforms) = postSolution
1420
{784E5D61-B08C-47E4-936A-03C9F67B71D0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
1521
{784E5D61-B08C-47E4-936A-03C9F67B71D0}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{784E5D61-B08C-47E4-936A-03C9F67B71D0}.Debug|x64.ActiveCfg = Debug|Any CPU
23+
{784E5D61-B08C-47E4-936A-03C9F67B71D0}.Debug|x64.Build.0 = Debug|Any CPU
24+
{784E5D61-B08C-47E4-936A-03C9F67B71D0}.Debug|x86.ActiveCfg = Debug|Any CPU
25+
{784E5D61-B08C-47E4-936A-03C9F67B71D0}.Debug|x86.Build.0 = Debug|Any CPU
1626
{784E5D61-B08C-47E4-936A-03C9F67B71D0}.Release|Any CPU.ActiveCfg = Release|Any CPU
1727
{784E5D61-B08C-47E4-936A-03C9F67B71D0}.Release|Any CPU.Build.0 = Release|Any CPU
28+
{784E5D61-B08C-47E4-936A-03C9F67B71D0}.Release|x64.ActiveCfg = Release|Any CPU
29+
{784E5D61-B08C-47E4-936A-03C9F67B71D0}.Release|x64.Build.0 = Release|Any CPU
30+
{784E5D61-B08C-47E4-936A-03C9F67B71D0}.Release|x86.ActiveCfg = Release|Any CPU
31+
{784E5D61-B08C-47E4-936A-03C9F67B71D0}.Release|x86.Build.0 = Release|Any CPU
32+
{5A4E4384-EF85-4E57-BFD6-ED0F99386E34}.Debug|Any CPU.ActiveCfg = Debug|Win32
33+
{5A4E4384-EF85-4E57-BFD6-ED0F99386E34}.Debug|x64.ActiveCfg = Debug|x64
34+
{5A4E4384-EF85-4E57-BFD6-ED0F99386E34}.Debug|x64.Build.0 = Debug|x64
35+
{5A4E4384-EF85-4E57-BFD6-ED0F99386E34}.Debug|x86.ActiveCfg = Debug|Win32
36+
{5A4E4384-EF85-4E57-BFD6-ED0F99386E34}.Debug|x86.Build.0 = Debug|Win32
37+
{5A4E4384-EF85-4E57-BFD6-ED0F99386E34}.Release|Any CPU.ActiveCfg = Release|Win32
38+
{5A4E4384-EF85-4E57-BFD6-ED0F99386E34}.Release|x64.ActiveCfg = Release|x64
39+
{5A4E4384-EF85-4E57-BFD6-ED0F99386E34}.Release|x64.Build.0 = Release|x64
40+
{5A4E4384-EF85-4E57-BFD6-ED0F99386E34}.Release|x86.ActiveCfg = Release|Win32
41+
{5A4E4384-EF85-4E57-BFD6-ED0F99386E34}.Release|x86.Build.0 = Release|Win32
1842
EndGlobalSection
1943
GlobalSection(SolutionProperties) = preSolution
2044
HideSolutionNode = FALSE
2145
EndGlobalSection
46+
GlobalSection(ExtensibilityGlobals) = postSolution
47+
SolutionGuid = {E103EC59-F7FB-42AF-8658-F6AD6B0D979B}
48+
EndGlobalSection
2249
EndGlobal

WindowTextExtractor/Extensions/AutomationElementExtensions.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System.Runtime.InteropServices;
44
using System.ComponentModel;
55
using System.Windows.Automation;
6-
using System.Windows.Automation.Text;
76

87
namespace WindowTextExtractor.Extensions
98
{

WindowTextExtractor/Extensions/TextBoxExtensions.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Linq;
4-
using System.Text;
5-
using System.Windows.Forms;
1+
using System.Windows.Forms;
62

73
namespace WindowTextExtractor.Extensions
84
{

WindowTextExtractor/Forms/MainForm.Designer.cs

Lines changed: 28 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

WindowTextExtractor/Forms/MainForm.cs

Lines changed: 68 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,30 @@
11
using 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;
82
using System.Windows.Forms;
93
using System.Diagnostics;
104
using System.Windows.Automation;
11-
using System.Windows.Automation.Text;
5+
using System.Runtime.InteropServices;
126
using WindowTextExtractor.Extensions;
137

148
namespace 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

Comments
 (0)