Menu

Virtual Geek

Tales from real IT system administrators world and non-production environment

PowerShell WPF Mouse cursor tracker

Currently I am working on a PowerShell GUI project where I need to track Mouse Cursor. In this article this is a sample code to track Mouse Cursor. As you move mouse on GUI console, it shows the position of mouse.

In this sample code it uses MouseMove event and Windows.Input.Mouse position to get information of cursor position. You can download this Get-Mouse-Poistion.ps1 script or it is also available on github.com.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Add-Type -AssemblyName PresentationCore,PresentationFramework

# Create the WPF window
$window = New-Object System.Windows.Window
$window.Title = "Mouse Tracker"
$window.Width = 400
$window.Height = 300
$window.WindowStartupLocation = "CenterScreen"

# Create the label to display mouse position
$label = New-Object System.Windows.Controls.Label
$label.HorizontalAlignment = "Center"
$label.VerticalAlignment = "Center"

# Add the label to the window's content
$window.Content = $label

# Subscribe to the MouseMove event
$window.Add_MouseMove({
    $position = [System.Windows.Input.Mouse]::GetPosition($window)
    $label.Content = "X: $($position.X), Y: $($position.Y)"
})

# Show the window
$window.ShowDialog() | Out-Null

Useful Articles
Powershell PoshGUI: Convert user to SID and vice versa using
Microsoft Powershell GUI: Change Internet Options connections Lan settings proxy server grayed out
Powshell GUI Date and Time converter tool
Powershell wpf gui, date and time converter select timezone get-date, timzone standard time ps1 script utc time, now.png

Powershell WPF GUI: ToolBox control Textbox watermark placeholder demo
Powershell WPF GUI: LinkLabel hyperlink demo
PowerShell WPF before and after image slider
POWERSHELL: USE PARAMETERS AND CONFIGURATION FROM INI FILE, USE AS SPLATTING
POWERSHELL CREATE HASHTABLE FROM MICROSOFT EXCEL

Go Back

Comment

Blog Search

Page Views

11393785

Follow me on Blogarama