Skip to content

Commit 4c2cda5

Browse files
committed
Added "Cursor Information" section to "Information" tab
1 parent 4c1f1e8 commit 4c2cda5

File tree

7 files changed

+108
-8
lines changed

7 files changed

+108
-8
lines changed

WindowTextExtractor/Forms/MainForm.cs

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ private void btnShowHide_Click(object sender, EventArgs e)
429429
var cmdShow = button.Text == "Show" ? ShowWindowCommands.SW_SHOW : ShowWindowCommands.SW_HIDE;
430430
User32.ShowWindow(windowHandle, cmdShow);
431431
button.Text = User32.IsWindowVisible(windowHandle) ? "Hide" : "Show";
432-
var windowInformation = WindowUtils.GetWindowInformation(windowHandle);
432+
var windowInformation = WindowUtils.GetWindowInformation(windowHandle, Cursor.Position);
433433
FillInformation(windowInformation);
434434
}
435435

@@ -683,7 +683,7 @@ public bool PreFilterMessage(ref Message m)
683683
FillImage(newImage);
684684
}
685685
}
686-
var windowInformation = WindowUtils.GetWindowInformation(windowHandle);
686+
var windowInformation = WindowUtils.GetWindowInformation(windowHandle, cursorPosition);
687687
FillInformation(windowInformation);
688688
if (previousProcessId != _windowProcessId)
689689
{
@@ -889,6 +889,23 @@ private void FillInformation(WindowInformation windowInformation)
889889
gvInformation.Rows.Clear();
890890
gvInformation.Tag = null;
891891

892+
if (windowInformation.CursorDetails.Keys.Any())
893+
{
894+
var indexHeader = gvInformation.Rows.Add();
895+
var rowHeader = gvInformation.Rows[indexHeader];
896+
rowHeader.Cells[0].Value = "Cursor Information";
897+
rowHeader.Cells[0].Style.BackColor = Color.LightGray;
898+
rowHeader.Cells[1].Style.BackColor = Color.LightGray;
899+
}
900+
901+
foreach (var cursorDetailKey in windowInformation.CursorDetails.Keys)
902+
{
903+
var index = gvInformation.Rows.Add();
904+
var row = gvInformation.Rows[index];
905+
row.Cells[0].Value = cursorDetailKey;
906+
row.Cells[1].Value = windowInformation.CursorDetails[cursorDetailKey];
907+
}
908+
892909
if (windowInformation.WindowDetails.Keys.Any())
893910
{
894911
var indexHeader = gvInformation.Rows.Add();

WindowTextExtractor/Native/Constants.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ static class Constants
2727
public const int DWL_DLGPROC = 4;
2828
public const int DWL_USER = 8;
2929

30+
// MonitorFromWindow
31+
public const uint MONITOR_DEFAULTTONULL = 0;
32+
public const uint MONITOR_DEFAULTTOPRIMARY = 1;
33+
public const uint MONITOR_DEFAULTTONEAREST = 2;
34+
3035
public const int DWMWA_EXTENDED_FRAME_BOUNDS = 9;
3136

3237
public const int EVENT_TYPE = 1;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System.Runtime.InteropServices;
2+
3+
namespace WindowTextExtractor.Native.Structs
4+
{
5+
[StructLayout(LayoutKind.Sequential)]
6+
struct MonitorInfo
7+
{
8+
public uint cbSize;
9+
public Rect rcMonitor;
10+
public Rect rcWork;
11+
public uint dwFlags;
12+
13+
public void Init()
14+
{
15+
cbSize = (uint)Marshal.SizeOf(this);
16+
}
17+
}
18+
}

WindowTextExtractor/Native/User32.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,5 +108,11 @@ static class User32
108108

109109
[DllImport("user32.dll")]
110110
public static extern bool DrawIconEx(IntPtr hdc, int xLeft, int yTop, IntPtr hIcon, int cxWidth, int cyHeight, int istepIfAniCur, IntPtr hbrFlickerFreeDraw, int diFlags);
111+
112+
[DllImport("user32")]
113+
public static extern bool GetMonitorInfo(IntPtr hMonitor, ref MonitorInfo info);
114+
115+
[DllImport("user32.dll")]
116+
public static extern IntPtr MonitorFromWindow(IntPtr hwnd, uint dwFlags);
111117
}
112118
}

WindowTextExtractor/Utils/WindowUtils.cs

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Diagnostics;
88
using System.Runtime.InteropServices;
99
using System.Management;
10+
using System.Windows.Forms;
1011
using mshtml;
1112
using WindowTextExtractor.Extensions;
1213
using WindowTextExtractor.Native;
@@ -51,7 +52,7 @@ public static IList<string> GetPasswordsFromHtmlPage(IntPtr handle)
5152
return result;
5253
}
5354

54-
public static WindowInformation GetWindowInformation(IntPtr handle)
55+
public static WindowInformation GetWindowInformation(IntPtr handle, Point cursorPosition)
5556
{
5657
var text = GetWindowText(handle);
5758
var wmText = GetWmGettext(handle);
@@ -73,6 +74,18 @@ public static WindowInformation GetWindowInformation(IntPtr handle)
7374
var dwlDlgproc = User32.GetClassLong(handle, Constants.DWL_DLGPROC);
7475
var dwlUser = User32.GetClassLong(handle, Constants.DWL_USER);
7576

77+
var cursorDetailes = new Dictionary<string, string>();
78+
cursorDetailes.Add("Position", $"X = {cursorPosition.X}, Y = {cursorPosition.Y}");
79+
80+
var monitorInfo = GetMonitorInfo(handle);
81+
cursorDetailes.Add("Monitor Position", $"X = {cursorPosition.X - monitorInfo.rcMonitor.Left}, Y = {cursorPosition.Y - monitorInfo.rcMonitor.Top}");
82+
83+
var monitorNumber = GetMonitorNumber(cursorPosition);
84+
cursorDetailes.Add("Monitor", monitorNumber.ToString());
85+
86+
var color = GetColorUnderCursor(cursorPosition);
87+
cursorDetailes.Add("Color Picker", ColorTranslator.ToHtml(color));
88+
7689
var windowDetailes = new Dictionary<string, string>();
7790
windowDetailes.Add("GetWindowText", text);
7891
windowDetailes.Add("WM_GETTEXT", wmText);
@@ -236,7 +249,7 @@ public static WindowInformation GetWindowInformation(IntPtr handle)
236249
{
237250
}
238251

239-
return new WindowInformation(windowDetailes, processDetailes);
252+
return new WindowInformation(cursorDetailes, windowDetailes, processDetailes);
240253
}
241254

242255
public static Bitmap CaptureWindow(IntPtr handle, bool captureCursor = false)
@@ -387,7 +400,6 @@ private static Rect GetFrameBounds(IntPtr handle)
387400
};
388401
}
389402

390-
391403
private static Process GetProcessByIdSafely(int pId)
392404
{
393405
try
@@ -445,6 +457,34 @@ private static int EnumWindows(IntPtr handle, ref IntPtr lParam)
445457
return result;
446458
}
447459

460+
private static Color GetColorUnderCursor(Point cursorPosition)
461+
{
462+
using (var bmp = new Bitmap(1, 1))
463+
using (var graphics = Graphics.FromImage(bmp))
464+
{
465+
graphics.CopyFromScreen(cursorPosition, Point.Empty, new Size(1, 1));
466+
var color = bmp.GetPixel(0, 0);
467+
return color;
468+
}
469+
}
470+
471+
private static int GetMonitorNumber(Point cursorPosition)
472+
{
473+
var screenFromPoint = Screen.FromPoint(cursorPosition);
474+
var screens = Screen.AllScreens.Select((x, i) => new { Index = i + 1, Item = x }).ToList();
475+
var screen = screens.FirstOrDefault(x => x.Item.Equals(screenFromPoint));
476+
return screen?.Index ?? 0;
477+
}
478+
479+
private static MonitorInfo GetMonitorInfo(IntPtr handle)
480+
{
481+
var monitorHandle = User32.MonitorFromWindow(handle, Constants.MONITOR_DEFAULTTONEAREST);
482+
var monitorInfo = new MonitorInfo();
483+
monitorInfo.Init();
484+
User32.GetMonitorInfo(monitorHandle, ref monitorInfo);
485+
return monitorInfo;
486+
}
487+
448488
private class WmiProcessInfo
449489
{
450490
public string CommandLine { get; set; }

WindowTextExtractor/WindowInformation.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,19 @@ namespace WindowTextExtractor
77
{
88
class WindowInformation
99
{
10+
public IDictionary<string, string> CursorDetails { get; private set; }
11+
1012
public IDictionary<string, string> WindowDetails { get; private set; }
1113

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

14-
public WindowInformation() : this(new Dictionary<string, string>(), new Dictionary<string, string>())
16+
public WindowInformation() : this(new Dictionary<string, string>(), new Dictionary<string, string>(), new Dictionary<string, string>())
1517
{
1618
}
1719

18-
public WindowInformation(IDictionary<string, string> windowDetails, IDictionary<string, string> processDetails)
20+
public WindowInformation(IDictionary<string, string> cursorDetails, IDictionary<string, string> windowDetails, IDictionary<string, string> processDetails)
1921
{
22+
CursorDetails = cursorDetails;
2023
WindowDetails = windowDetails;
2124
ProcessDetails = processDetails;
2225
}
@@ -25,7 +28,17 @@ public override string ToString()
2528
{
2629
const int paddingSize = 25;
2730
var builder = new StringBuilder(1024);
28-
31+
32+
if (CursorDetails.Keys.Any())
33+
{
34+
builder.AppendFormat($"Cursor Information {Environment.NewLine}");
35+
}
36+
37+
foreach (var cursorDetailKey in CursorDetails.Keys)
38+
{
39+
builder.AppendFormat($"{cursorDetailKey.PadRight(paddingSize)}: {CursorDetails[cursorDetailKey]}{Environment.NewLine}");
40+
}
41+
2942
if (WindowDetails.Keys.Any())
3043
{
3144
builder.AppendFormat($"Window Information {Environment.NewLine}");

WindowTextExtractor/WindowTextExtractor.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@
150150
</Compile>
151151
<Compile Include="Native\Structs\CURSORINFO.cs" />
152152
<Compile Include="Native\Structs\ICONINFO.cs" />
153+
<Compile Include="Native\Structs\MonitorInfo.cs" />
153154
<Compile Include="Native\Winmm.cs" />
154155
<Compile Include="Utils\FileUtils.cs" />
155156
<Compile Include="Utils\ImageUtils.cs" />

0 commit comments

Comments
 (0)