Skip to content

Commit 186bac0

Browse files
committed
Added "Show/Hide" button
1 parent 22e86ed commit 186bac0

File tree

5 files changed

+115
-63
lines changed

5 files changed

+115
-63
lines changed

WindowTextExtractor/Forms/MainForm.Designer.cs

Lines changed: 62 additions & 48 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: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public partial class MainForm : Form, IMessageFilter
2626
private string _informationFileName;
2727
private string _textFileName;
2828
private string _imageFileName;
29+
private IntPtr _windowHandle;
2930

3031
public MainForm()
3132
{
@@ -37,6 +38,7 @@ public MainForm()
3738
_informationFileName = string.Empty;
3839
_textFileName = string.Empty;
3940
_imageFileName = string.Empty;
41+
_windowHandle = IntPtr.Zero;
4042
}
4143

4244
protected override void OnLoad(EventArgs e)
@@ -206,6 +208,17 @@ private void menuItemAbout_Click(object sender, EventArgs e)
206208
dialog.ShowDialog(this);
207209
}
208210

211+
private void btnShowHide_Click(object sender, EventArgs e)
212+
{
213+
var button = (Button)sender;
214+
var cmdShow = button.Text == "Show" ? ShowWindowCommands.SW_SHOW : ShowWindowCommands.SW_HIDE;
215+
NativeMethods.ShowWindow(_windowHandle, cmdShow);
216+
button.Text = NativeMethods.IsWindowVisible(_windowHandle) ? "Hide" : "Show";
217+
var windowInformation = WindowUtils.GetWindowInformation(_windowHandle);
218+
FillInformation(windowInformation);
219+
}
220+
221+
209222
protected override void WndProc(ref Message m)
210223
{
211224
switch (m.Msg)
@@ -253,16 +266,16 @@ public bool PreFilterMessage(ref Message m)
253266
var element = AutomationElement.FromPoint(new System.Windows.Point(cursorPosition.X, cursorPosition.Y));
254267
if (element != null && element.Current.ProcessId != _processId)
255268
{
256-
var windowHandle = new IntPtr(element.Current.NativeWindowHandle);
257-
windowHandle = windowHandle == IntPtr.Zero ? NativeMethods.WindowFromPoint(new Point(cursorPosition.X, cursorPosition.Y)) : windowHandle;
269+
_windowHandle = new IntPtr(element.Current.NativeWindowHandle);
270+
_windowHandle = _windowHandle == IntPtr.Zero ? NativeMethods.WindowFromPoint(new Point(cursorPosition.X, cursorPosition.Y)) : _windowHandle;
258271
if (element.Current.IsPassword)
259272
{
260273
var process = Process.GetProcessById(element.Current.ProcessId);
261274
if (process.ProcessName.ToLower() == "iexplore")
262275
{
263-
if (windowHandle != IntPtr.Zero)
276+
if (_windowHandle != IntPtr.Zero)
264277
{
265-
var passwords = WindowUtils.GetPasswordsFromHtmlPage(windowHandle);
278+
var passwords = WindowUtils.GetPasswordsFromHtmlPage(_windowHandle);
266279
if (passwords.Any())
267280
{
268281
txtContent.Text = passwords.Count > 1 ? string.Join(Environment.NewLine, passwords.Select((x, i) => "Password " + (i + 1) + ": " + x)) : passwords[0];
@@ -280,9 +293,9 @@ public bool PreFilterMessage(ref Message m)
280293
}
281294
else
282295
{
283-
NativeMethods.SetHook(Handle, windowHandle, _messageId);
296+
NativeMethods.SetHook(Handle, _windowHandle, _messageId);
284297
NativeMethods.QueryPasswordEdit();
285-
NativeMethods.UnsetHook(Handle, windowHandle);
298+
NativeMethods.UnsetHook(Handle, _windowHandle);
286299
}
287300
}
288301
else
@@ -295,11 +308,18 @@ public bool PreFilterMessage(ref Message m)
295308
pbContent.Image.Dispose();
296309
pbContent.Image = null;
297310
}
298-
pbContent.Image = WindowUtils.PrintWindow(windowHandle);
299-
var windowInformation = WindowUtils.GetWindowInformation(windowHandle);
311+
pbContent.Image = WindowUtils.PrintWindow(_windowHandle);
312+
var windowInformation = WindowUtils.GetWindowInformation(_windowHandle);
300313
FillInformation(windowInformation);
301314
OnContentChanged();
302315
}
316+
317+
btnShowHide.Text = NativeMethods.IsWindowVisible(_windowHandle) ? "Hide" : "Show";
318+
btnShowHide.Visible = true;
319+
}
320+
else
321+
{
322+
btnShowHide.Visible = false;
303323
}
304324
}
305325
}

WindowTextExtractor/Native/NativeMethods.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,18 @@ static class NativeMethods
117117
[DllImport("user32.dll")]
118118
public static extern bool GetLayeredWindowAttributes(IntPtr hwnd, out uint crKey, out Byte bAlpha, out uint dwFlags);
119119

120-
[DllImport("user32.dll", SetLastError = true)]
120+
[DllImport("user32.dll")]
121121
[return: MarshalAs(UnmanagedType.Bool)]
122122
public static extern bool GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
123123

124+
[DllImport("user32.dll")]
125+
[return: MarshalAs(UnmanagedType.Bool)]
126+
public static extern bool ShowWindow(IntPtr hWnd, ShowWindowCommands nCmdShow);
127+
128+
[DllImport("user32.dll")]
129+
[return: MarshalAs(UnmanagedType.Bool)]
130+
public static extern bool IsWindowVisible(IntPtr hWnd);
131+
124132
[DllImport("kernel32.dll")]
125133
public static extern bool QueryFullProcessImageName([In] IntPtr hProcess, [In] uint dwFlags, [Out] StringBuilder lpExeName, [In, Out] ref uint lpdwSize);
126134

WindowTextExtractor/Native/NativeTypes.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,17 @@ struct WINDOWPLACEMENT
123123

124124
enum ShowWindowCommands : int
125125
{
126-
Hide = 0,
127-
Normal = 1,
128-
Minimized = 2,
129-
Maximized = 3,
126+
SW_HIDE = 0,
127+
SW_SHOWNORMAL = 1,
128+
SW_SHOWMINIMIZED = 2,
129+
SW_SHOWMAXIMIZED = 3,
130+
SW_SHOWNOACTIVATE = 4,
131+
SW_SHOW = 5,
132+
SW_MINIMIZE = 6,
133+
SW_SHOWMINNOACTIVE = 7,
134+
SW_SHOWNA = 8,
135+
SW_RESTORE = 9,
136+
SW_SHOWDEFAULT = 10,
137+
SW_FORCEMINIMIZE = 11
130138
}
131139
}

WindowTextExtractor/Utils/WindowUtils.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ public static WindowInformation GetWindowInformation(IntPtr hWnd)
5858
var realWindowClass = RealGetWindowClass(hWnd);
5959
var hWndParent = NativeMethods.GetParent(hWnd);
6060
var size = GetWindowSize(hWnd);
61-
var clientSize = GetWindowClientSize(hWnd);
61+
var clientSize = GetWindowClientSize(hWnd);
62+
var isVisible = NativeMethods.IsWindowVisible(hWnd);
6263
var placement = GetWindowPlacement(hWnd);
6364
var threadId = NativeMethods.GetWindowThreadProcessId(hWnd, out var processId);
6465
var process = GetProcessByIdSafely(processId);
@@ -86,7 +87,8 @@ public static WindowInformation GetWindowInformation(IntPtr hWnd)
8687

8788
windowDetailes.Add("Window Handle", $"0x{hWnd.ToInt64():X}");
8889
windowDetailes.Add("Parent Window Handle", hWndParent == IntPtr.Zero ? "-" : $"0x{hWndParent.ToInt64():X}");
89-
windowDetailes.Add("Window Placement", placement.showCmd.ToString());
90+
windowDetailes.Add("Is Window Visible", isVisible.ToString());
91+
windowDetailes.Add("Window Placement (showCmd)", placement.showCmd.ToString());
9092
windowDetailes.Add("Window Size", $"{size.Width}x{size.Height}");
9193
windowDetailes.Add("Window Client Size", $"{clientSize.Width}x{clientSize.Height}");
9294

0 commit comments

Comments
 (0)