powershell - SPI_GETCURSORSHADOW 0x101A - 无法获取布尔值
问题描述
这是底部的 PowerShell 代码,我尝试编写但未能检索光标阴影状态。
我在这里查看了帮助:https ://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-systemparametersinfow
它指出:
SPI_GETCURSORSHADOW 0x101A
确定光标周围是否有阴影。pvParam 参数必须指向一个 BOOL 变量,如果启用阴影,则接收 TRUE,如果禁用,则接收 FALSE。仅当系统的色深超过 256 色时才会出现此效果。
我的代码尝试可以保存为 .ps1 文件并在 PS ISE 中测试
$CSharpSig = @'
[DllImport("user32.dll", EntryPoint = "SystemParametersInfo")]
public static extern bool SystemParametersInfo(
int uiAction, uint uiParam, uint pvParam, uint fWinIni);
'@
$SPI_GETCURSORSHADOW = 0x101A
$CursorRefresh = Add-Type -MemberDefinition $CSharpSig -Name WinAPICall -Namespace SystemParamInfo -PassThru
# SPI_GETCURSORSHADOW - pvParam 0 or 1 (3rd argument)
$CursorRefresh::SystemParametersInfo($SPI_GETCURSORSHADOW, 0, $BOOLTOGGLE, 0)
write-output $BOOLTOGGLE
False
即使不是这种情况,它也会一直作为状态返回。
即使在阅读了与 PowerShell 没有直接关联的类似线程后,我也不知道如何得到这个: Messed with SystemParametersInfo and Booleans pvParam parameter
编辑,新问题:
我对代码的全部意图是尝试切换光标阴影的设置,所以这是我最近的尝试。我已将与问题相关的注释放在代码中。
此代码适用于我最初的问题,但当我在底部添加注释代码时无效。
# More info here: https://docs.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-systemparametersinfow
# Get the original setting
$CSharpSigGet = @'
[DllImport("user32.dll")]
public static extern bool SystemParametersInfo(
int uiAction, uint uiParam, out bool pvParam, uint fWinIni);
'@
$SPI_GETCURSORSHADOW = 0x101A
$CursorGet = Add-Type -MemberDefinition $CSharpSigGet -Name WinAPICall -Namespace SystemParamInfo -PassThru
[bool] $getBool = $false
$CursorGet::SystemParametersInfo($SPI_GETCURSORSHADOW, 0, [ref] $getBool, 0).value # Stores the boolean.
$cursorShadowBool = ([ref] $getBool).value
write-output $cursorShadowBool
# Toggle the original setting
# This code is now not working as it throws an error when this code below is uncommented..
# Error below:
# TYPE_ALREADY_EXISTS,Microsoft.PowerShell.Commands.AddTypeCommand
# $SPI_SETCURSORSHADOW = 0x101B
# $CSharpSigSet = @'
# [DllImport("user32.dll", EntryPoint = "SystemParametersInfo")]
# public static extern bool SystemParametersInfo(
# int uiAction, uint uiParam, uint pvParam, uint fWinIni);
# '@
# $CursorSet = Add-Type -MemberDefinition $CSharpSigSet -Name WinAPICall -Namespace SystemParamInfo -PassThru
# $CursorSet::SystemParametersInfo($SPI_SETCURSORSHADOW, 0, -not ([ref] $getBool).value, 0)
解决方案
根据文档:
SPI_GETCURSORSHADOW 0x101A
确定光标周围是否有阴影。该
pvParam
参数必须指向一个BOOL
变量,TRUE
如果启用了阴影,则接收该变量,FALSE
如果它被禁用。仅当系统的色深超过 256 色时才会出现此效果。
所以你的签名不正确;第三个参数必须指向a BOOL
。假设您只需要针对这种特定情况调用它,并且我们不需要为泛型而烦恼IntPtr
:
$CSharpSig = @'
[DllImport("user32.dll")]
public static extern bool SystemParametersInfo(
int uiAction, uint uiParam, out bool pvParam, uint fWinIni);
'@
[bool] $BOOLTOGGLE = $false
if ($CursorRefresh::SystemParametersInfo($SPI_GETCURSORSHADOW, 0, [ref] $BOOLTOGGLE, 0)) {
write-output $BOOLTOGGLE
}
推荐阅读
- python - 熊猫:从选定的列创建新的数据框
- sql - 查询性能增强
- php - Stancl/Tenancy - 创建新租户时如何自动运行迁移?
- html - 将使用 :before & :after 创建的 div 与其他元素水平对齐
- javascript - 需要帮助删除 IMG 并将其替换为带有 javascript 的位置的文本
- html - 如何使用 CSS 自定义 Bootstrap 5 配色方案?
- html - 弹性容器不包装物品
- reactjs - Apache/React 路由器子域到路径
- angular - Angular Reactive 表单控制所有报告 ng-valid 和父表单报告为 ng-valid 但提交仍然禁用,直到单击表单控件
- javascript - 将数据从 JSON 传递到 React 组件 Apexchart 时间线