本文介绍了与 SystemParametersInfo 和 Booleans pvParam 参数混淆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 SystemParametersInfo 函数来设置布尔参数,这只是我将设置的布尔参数之一的示例:

I'm trying to use SystemParametersInfo function to set a Boolean parameter, this is just an example of one of the Booleans parameters that I will set:

' I try to set a boolean parameter, but no matter if I use False or True,
' the parameter on the SO is always activated.
SystemParametersInfo(SPI.SPI_SETCURSORSHADOW, 0UI, False, SPIF.None)

' I check the modified parameter but always is positive.
Dim MyBoolean As Boolean = False
SystemParametersInfo(SPI.SPI_GETCURSORSHADOW, 0UI, MyBoolean, SPIF.None)

MsgBox(MyBoolean) ' Always is 'True',
' and in the Advanced properties of the SO;
' the 'Show shadow under mouse' option is always checked.

这是另一个例子:

' Enable Accelerator Keys Visibility ( The underlined keys of the ContextMenu )
NativeMethods.SystemParametersInfo(SPI.SPI_SETKEYBOARDCUES, 0UI, True, SPIF.None)

MSDN 中两者的文档说使用 False 布尔值来禁用参数:

The documentation for both in MSDN says to use a False boolean to disable the parameter:

''' <summary>
''' Enables or disables a shadow around the cursor.
''' The pvParam parameter is a BOOL variable.
''' Set pvParam to TRUE to enable the shadow or FALSE to disable the shadow.
''' This effect appears only if the system has a color depth of more than 256 colors.
''' Windows NT, Windows Me/98/95:  This value is not supported.
''' </summary>

但事实是,一旦启用它们,我就找不到禁用它们的方法,我尝试直接使用布尔值,也使用布尔变量,以及使用 SPIF 参数的组合,没办法, 如果我激活一个参数我无法将其关闭,我认为我做错了什么但是......什么是?.

But the truth is that I can't find the way to Disable them once they are enabled, I've tried to use directly a boolean value, also with a boolean variable, and with combinations of the SPIF parameter, no way, if I activate a parameter I can't turn it off, I supose that I'm doing something wrong but ...what is?.

PS:我使用的是 Windows 8 x64

PS: I'm using Windows 8 x64

这是SystemParametersInfo部分:

<DllImport("user32.dll", SetLastError:=True)>
Friend Shared Function SystemParametersInfo(
              ByVal uiAction As SPI,
              ByVal uiParam As UInteger,
              ByRef pvParam As Boolean,
              ByVal fWinIni As SPIF
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function

<Description("SPI System-wide parameter - Used in SystemParametersInfo function")>
Public Enum SPI As UInteger

    SPI_SETKEYBOARDCUES = &H100B
    SPI_GETCURSORSHADOW = &H101A
    SPI_SETCURSORSHADOW = &H101B

End Enum

<Description("SPIF System-wide parameter - Used in SystemParametersInfo function")>
<Flags>
Enum SPIF

    ''' <summary>
    ''' None
    ''' </summary>
    None = &H0

    ''' <summary>
    ''' Writes the new system-wide parameter setting to the user profile.
    ''' </summary>
    SPIF_UPDATEINIFILE = &H1

    ''' <summary>
    ''' Broadcasts the WM_SETTINGCHANGE message after updating the user profile.
    ''' </summary>
    SPIF_SENDCHANGE = &H2

    ''' <summary>
    ''' Same as SPIF_SENDCHANGE.
    ''' </summary>
    SPIF_SENDWININICHANGE = &H2

End Enum

推荐答案

所有 Win32 API 的东西都倾向于面向 C/C++,其中 True 为 1(或者实际上只是不为零的 IIRC).您需要将 MyBoolean 更改为 Integer 并使用 0 或 1.您还必须更改 API 签名:

All the Win32 API stuff tends to be geared for C/C++ where True is 1 (or actually just not zero IIRC). You need to change MyBoolean to Integer and use 0 or 1. You'll also have to change the API signature:

<DllImport("user32.dll")> _
 Private Shared Sub SystemParametersInfo(uiAction As UInteger,
       uiParam As UInteger, ByRef pvParam As Integer, fWinIni As UInteger)

 End Sub

这也是为什么您看到 0 和 1 以及您正在处理的许多 WndProc 消息的返回.很多情况下是T/F返回消息是否被处理.

This is also why you saw 0 and 1 and the return for many, many of those WndProc messages you were messing with. Its the T/F return whether the message was handled in many cases.

这篇关于与 SystemParametersInfo 和 Booleans pvParam 参数混淆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 06:10