与SystemParametersInfo和Booleans pvParam参数混淆

时间:2014-01-13 18:51:03

标签: .net vb.net windows api winapi

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

' 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布尔来禁用参数:

''' <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参数的组合,没办法,如果我激活一个参数我无法将其关闭,我会说我做错了但是......是什么?。

PS:我使用的是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

1 个答案:

答案 0 :(得分:1)

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

<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返回是否在许多情况下处理了消息。