新建ps1文件并复制粘贴以下代码,用powershell运行即可
$ErrorActionPreference = 'Stop'
Set-StrictMode -Version Latest
if (-not ('CursorSchemeNative' -as [type])) {
Add-Type @'
using System;
using System.Runtime.InteropServices;
public static class CursorSchemeNative
{
[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern IntPtr LoadCursorFromFile(string fileName);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool SetSystemCursor(IntPtr cursor, uint cursorId);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool DestroyCursor(IntPtr cursor);
}
'@
}
$cursorSettings = Get-ItemProperty `
-LiteralPath 'Registry::HKEY_CURRENT_USER\Control Panel\Cursors'
$cursorMappings = @(
[pscustomobject]@{ Role = 'Arrow'; CursorId = [uint32]32512 }
[pscustomobject]@{ Role = 'IBeam'; CursorId = [uint32]32513 }
[pscustomobject]@{ Role = 'Wait'; CursorId = [uint32]32514 }
[pscustomobject]@{ Role = 'Crosshair'; CursorId = [uint32]32515 }
[pscustomobject]@{ Role = 'UpArrow'; CursorId = [uint32]32516 }
[pscustomobject]@{ Role = 'NWPen'; CursorId = [uint32]32631 }
[pscustomobject]@{ Role = 'SizeNWSE'; CursorId = [uint32]32642 }
[pscustomobject]@{ Role = 'SizeNESW'; CursorId = [uint32]32643 }
[pscustomobject]@{ Role = 'SizeWE'; CursorId = [uint32]32644 }
[pscustomobject]@{ Role = 'SizeNS'; CursorId = [uint32]32645 }
[pscustomobject]@{ Role = 'SizeAll'; CursorId = [uint32]32646 }
[pscustomobject]@{ Role = 'No'; CursorId = [uint32]32648 }
[pscustomobject]@{ Role = 'Hand'; CursorId = [uint32]32649 }
[pscustomobject]@{ Role = 'AppStarting';CursorId = [uint32]32650 }
[pscustomobject]@{ Role = 'Help'; CursorId = [uint32]32651 }
[pscustomobject]@{ Role = 'Pin'; CursorId = [uint32]32671 }
[pscustomobject]@{ Role = 'Person'; CursorId = [uint32]32672 }
)
foreach ($mapping in $cursorMappings) {
Write-Host ''
Write-Host "========== $($mapping.Role) =========="
$configuredPath = $cursorSettings.($mapping.Role)
if ([string]::IsNullOrWhiteSpace([string]$configuredPath)) {
Write-Host 'Registry path is empty.' -ForegroundColor Yellow
continue
}
$cursorPath = [Environment]::ExpandEnvironmentVariables(
[string]$configuredPath
).Trim('"')
Write-Host "Path: $cursorPath"
Write-Host "Cursor ID: $($mapping.CursorId)"
if (-not (Test-Path -LiteralPath $cursorPath -PathType Leaf)) {
Write-Host 'FILE NOT FOUND' -ForegroundColor Red
continue
}
Write-Host 'File exists.'
$cursorHandle = [CursorSchemeNative]::LoadCursorFromFile(
$cursorPath
)
if ($cursorHandle -eq [IntPtr]::Zero) {
$errorCode = [Runtime.InteropServices.Marshal]::GetLastWin32Error()
Write-Host "LOAD FAILED - Win32 Error: $errorCode" `
-ForegroundColor Red
continue
}
Write-Host "LoadCursorFromFile: SUCCESS"
Write-Host "Handle: $cursorHandle"
$result = [CursorSchemeNative]::SetSystemCursor(
$cursorHandle,
$mapping.CursorId
)
if ($result) {
Write-Host "SetSystemCursor: SUCCESS" `
-ForegroundColor Green
# IMPORTANT:
# SetSystemCursor takes ownership of the cursor handle.
# Do not call DestroyCursor after success.
}
else {
$errorCode = [Runtime.InteropServices.Marshal]::GetLastWin32Error()
Write-Host "SetSystemCursor: FAILED" `
-ForegroundColor Red
Write-Host "Win32 Error: $errorCode"
[void][CursorSchemeNative]::DestroyCursor(
$cursorHandle
)
}
}
Write-Host ''
Write-Host '============================================================'
Write-Host 'Test finished.'
Write-Host 'NO SPI_SETCURSORS WAS CALLED.'
Write-Host '============================================================'