Skip to content

Commit

Permalink
Added "Cursor Information" section to "Information" tab
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderPro committed Apr 2, 2023
1 parent 4c1f1e8 commit 4c2cda5
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 8 deletions.
21 changes: 19 additions & 2 deletions WindowTextExtractor/Forms/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ private void btnShowHide_Click(object sender, EventArgs e)
var cmdShow = button.Text == "Show" ? ShowWindowCommands.SW_SHOW : ShowWindowCommands.SW_HIDE;
User32.ShowWindow(windowHandle, cmdShow);
button.Text = User32.IsWindowVisible(windowHandle) ? "Hide" : "Show";
var windowInformation = WindowUtils.GetWindowInformation(windowHandle);
var windowInformation = WindowUtils.GetWindowInformation(windowHandle, Cursor.Position);
FillInformation(windowInformation);
}

Expand Down Expand Up @@ -683,7 +683,7 @@ public bool PreFilterMessage(ref Message m)
FillImage(newImage);
}
}
var windowInformation = WindowUtils.GetWindowInformation(windowHandle);
var windowInformation = WindowUtils.GetWindowInformation(windowHandle, cursorPosition);
FillInformation(windowInformation);
if (previousProcessId != _windowProcessId)
{
Expand Down Expand Up @@ -889,6 +889,23 @@ private void FillInformation(WindowInformation windowInformation)
gvInformation.Rows.Clear();
gvInformation.Tag = null;

if (windowInformation.CursorDetails.Keys.Any())
{
var indexHeader = gvInformation.Rows.Add();
var rowHeader = gvInformation.Rows[indexHeader];
rowHeader.Cells[0].Value = "Cursor Information";
rowHeader.Cells[0].Style.BackColor = Color.LightGray;
rowHeader.Cells[1].Style.BackColor = Color.LightGray;
}

foreach (var cursorDetailKey in windowInformation.CursorDetails.Keys)
{
var index = gvInformation.Rows.Add();
var row = gvInformation.Rows[index];
row.Cells[0].Value = cursorDetailKey;
row.Cells[1].Value = windowInformation.CursorDetails[cursorDetailKey];
}

if (windowInformation.WindowDetails.Keys.Any())
{
var indexHeader = gvInformation.Rows.Add();
Expand Down
5 changes: 5 additions & 0 deletions WindowTextExtractor/Native/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ static class Constants
public const int DWL_DLGPROC = 4;
public const int DWL_USER = 8;

// MonitorFromWindow
public const uint MONITOR_DEFAULTTONULL = 0;
public const uint MONITOR_DEFAULTTOPRIMARY = 1;
public const uint MONITOR_DEFAULTTONEAREST = 2;

public const int DWMWA_EXTENDED_FRAME_BOUNDS = 9;

public const int EVENT_TYPE = 1;
Expand Down
18 changes: 18 additions & 0 deletions WindowTextExtractor/Native/Structs/MonitorInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Runtime.InteropServices;

namespace WindowTextExtractor.Native.Structs
{
[StructLayout(LayoutKind.Sequential)]
struct MonitorInfo
{
public uint cbSize;
public Rect rcMonitor;
public Rect rcWork;
public uint dwFlags;

public void Init()
{
cbSize = (uint)Marshal.SizeOf(this);
}
}
}
6 changes: 6 additions & 0 deletions WindowTextExtractor/Native/User32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,5 +108,11 @@ static class User32

[DllImport("user32.dll")]
public static extern bool DrawIconEx(IntPtr hdc, int xLeft, int yTop, IntPtr hIcon, int cxWidth, int cyHeight, int istepIfAniCur, IntPtr hbrFlickerFreeDraw, int diFlags);

[DllImport("user32")]
public static extern bool GetMonitorInfo(IntPtr hMonitor, ref MonitorInfo info);

[DllImport("user32.dll")]
public static extern IntPtr MonitorFromWindow(IntPtr hwnd, uint dwFlags);
}
}
46 changes: 43 additions & 3 deletions WindowTextExtractor/Utils/WindowUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Management;
using System.Windows.Forms;
using mshtml;
using WindowTextExtractor.Extensions;
using WindowTextExtractor.Native;
Expand Down Expand Up @@ -51,7 +52,7 @@ public static IList<string> GetPasswordsFromHtmlPage(IntPtr handle)
return result;
}

public static WindowInformation GetWindowInformation(IntPtr handle)
public static WindowInformation GetWindowInformation(IntPtr handle, Point cursorPosition)
{
var text = GetWindowText(handle);
var wmText = GetWmGettext(handle);
Expand All @@ -73,6 +74,18 @@ public static WindowInformation GetWindowInformation(IntPtr handle)
var dwlDlgproc = User32.GetClassLong(handle, Constants.DWL_DLGPROC);
var dwlUser = User32.GetClassLong(handle, Constants.DWL_USER);

var cursorDetailes = new Dictionary<string, string>();
cursorDetailes.Add("Position", $"X = {cursorPosition.X}, Y = {cursorPosition.Y}");

var monitorInfo = GetMonitorInfo(handle);
cursorDetailes.Add("Monitor Position", $"X = {cursorPosition.X - monitorInfo.rcMonitor.Left}, Y = {cursorPosition.Y - monitorInfo.rcMonitor.Top}");

var monitorNumber = GetMonitorNumber(cursorPosition);
cursorDetailes.Add("Monitor", monitorNumber.ToString());

var color = GetColorUnderCursor(cursorPosition);
cursorDetailes.Add("Color Picker", ColorTranslator.ToHtml(color));

var windowDetailes = new Dictionary<string, string>();
windowDetailes.Add("GetWindowText", text);
windowDetailes.Add("WM_GETTEXT", wmText);
Expand Down Expand Up @@ -236,7 +249,7 @@ public static WindowInformation GetWindowInformation(IntPtr handle)
{
}

return new WindowInformation(windowDetailes, processDetailes);
return new WindowInformation(cursorDetailes, windowDetailes, processDetailes);
}

public static Bitmap CaptureWindow(IntPtr handle, bool captureCursor = false)
Expand Down Expand Up @@ -387,7 +400,6 @@ private static Rect GetFrameBounds(IntPtr handle)
};
}


private static Process GetProcessByIdSafely(int pId)
{
try
Expand Down Expand Up @@ -445,6 +457,34 @@ private static int EnumWindows(IntPtr handle, ref IntPtr lParam)
return result;
}

private static Color GetColorUnderCursor(Point cursorPosition)
{
using (var bmp = new Bitmap(1, 1))
using (var graphics = Graphics.FromImage(bmp))
{
graphics.CopyFromScreen(cursorPosition, Point.Empty, new Size(1, 1));
var color = bmp.GetPixel(0, 0);
return color;
}
}

private static int GetMonitorNumber(Point cursorPosition)
{
var screenFromPoint = Screen.FromPoint(cursorPosition);
var screens = Screen.AllScreens.Select((x, i) => new { Index = i + 1, Item = x }).ToList();
var screen = screens.FirstOrDefault(x => x.Item.Equals(screenFromPoint));
return screen?.Index ?? 0;
}

private static MonitorInfo GetMonitorInfo(IntPtr handle)
{
var monitorHandle = User32.MonitorFromWindow(handle, Constants.MONITOR_DEFAULTTONEAREST);
var monitorInfo = new MonitorInfo();
monitorInfo.Init();
User32.GetMonitorInfo(monitorHandle, ref monitorInfo);
return monitorInfo;
}

private class WmiProcessInfo
{
public string CommandLine { get; set; }
Expand Down
19 changes: 16 additions & 3 deletions WindowTextExtractor/WindowInformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@ namespace WindowTextExtractor
{
class WindowInformation
{
public IDictionary<string, string> CursorDetails { get; private set; }

public IDictionary<string, string> WindowDetails { get; private set; }

public IDictionary<string, string> ProcessDetails { get; private set; }

public WindowInformation() : this(new Dictionary<string, string>(), new Dictionary<string, string>())
public WindowInformation() : this(new Dictionary<string, string>(), new Dictionary<string, string>(), new Dictionary<string, string>())
{
}

public WindowInformation(IDictionary<string, string> windowDetails, IDictionary<string, string> processDetails)
public WindowInformation(IDictionary<string, string> cursorDetails, IDictionary<string, string> windowDetails, IDictionary<string, string> processDetails)
{
CursorDetails = cursorDetails;
WindowDetails = windowDetails;
ProcessDetails = processDetails;
}
Expand All @@ -25,7 +28,17 @@ public override string ToString()
{
const int paddingSize = 25;
var builder = new StringBuilder(1024);


if (CursorDetails.Keys.Any())
{
builder.AppendFormat($"Cursor Information {Environment.NewLine}");
}

foreach (var cursorDetailKey in CursorDetails.Keys)
{
builder.AppendFormat($"{cursorDetailKey.PadRight(paddingSize)}: {CursorDetails[cursorDetailKey]}{Environment.NewLine}");
}

if (WindowDetails.Keys.Any())
{
builder.AppendFormat($"Window Information {Environment.NewLine}");
Expand Down
1 change: 1 addition & 0 deletions WindowTextExtractor/WindowTextExtractor.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@
</Compile>
<Compile Include="Native\Structs\CURSORINFO.cs" />
<Compile Include="Native\Structs\ICONINFO.cs" />
<Compile Include="Native\Structs\MonitorInfo.cs" />
<Compile Include="Native\Winmm.cs" />
<Compile Include="Utils\FileUtils.cs" />
<Compile Include="Utils\ImageUtils.cs" />
Expand Down

0 comments on commit 4c2cda5

Please sign in to comment.