Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 我需要两个不同的Pinvoke来获取和设置鼠标速度吗?_.net_Vb.net_Api_Pinvoke - Fatal编程技术网

.net 我需要两个不同的Pinvoke来获取和设置鼠标速度吗?

.net 我需要两个不同的Pinvoke来获取和设置鼠标速度吗?,.net,vb.net,api,pinvoke,.net,Vb.net,Api,Pinvoke,我想得到当前鼠标指针的速度,我想设置它 为了得到它,我使用 Public Declare Function SystemParametersInfo Lib "user32.dll" Alias "SystemParametersInfoA" (ByVal uAction As Int32, ByVal uParam As Int32, ByRef lpvParam As Int32, ByVal fuWinIni As Int32) As Int32 Dim Result As In

我想得到当前鼠标指针的速度,我想设置它

为了得到它,我使用

Public Declare Function SystemParametersInfo Lib "user32.dll" Alias "SystemParametersInfoA" (ByVal uAction As Int32, ByVal uParam As Int32, ByRef lpvParam As Int32, ByVal fuWinIni As Int32) As Int32

    Dim Result As Int32 = 0
    Dim iSuccess As Integer = SystemParametersInfo(SPI_GETMOUSESPEED, 0, Result, 0)
   Public Declare Function SystemParametersInfo Lib "user32.dll" Alias "SystemParametersInfoA" (ByVal uAction As Int32, ByVal uParam As Int32, ByVal lpvParam As Int32, ByVal fuWinIni As Int32) As Int32

    Dim iVal As Integer = 10
    Dim iSuccess As Integer = SystemParametersInfo(SPI_SETMOUSESPEED, 0, iVal, 0)
要设置它,我使用

Public Declare Function SystemParametersInfo Lib "user32.dll" Alias "SystemParametersInfoA" (ByVal uAction As Int32, ByVal uParam As Int32, ByRef lpvParam As Int32, ByVal fuWinIni As Int32) As Int32

    Dim Result As Int32 = 0
    Dim iSuccess As Integer = SystemParametersInfo(SPI_GETMOUSESPEED, 0, Result, 0)
   Public Declare Function SystemParametersInfo Lib "user32.dll" Alias "SystemParametersInfoA" (ByVal uAction As Int32, ByVal uParam As Int32, ByVal lpvParam As Int32, ByVal fuWinIni As Int32) As Int32

    Dim iVal As Integer = 10
    Dim iSuccess As Integer = SystemParametersInfo(SPI_SETMOUSESPEED, 0, iVal, 0)
请注意同一函数的不同声明

如果我将ByRef更改为ByVal或反之亦然,则其中一个函数不起作用

我真的必须以不同的方式声明相同的函数吗? 还是我犯了什么错误?
如果我这样做了,有人能告诉我如何正确执行吗?

核心问题是不能用这种方式在VB中声明重载,因为正如错误消息所说,它们
不能互相重载,因为只有ByRef和ByVal声明的参数不同(您没有提到错误)。这在C#中不会发生,因为您将使用
in
out
来更改签名

您有几个选项,一个是使用
别名

' declaration for all ByVal args:
Declare Function SetSysParam Lib "user32.dll" Alias "SystemParametersInfoA"...

' one parm ByRef for getting the speed:
Declare Function GetSysParam Lib "user32.dll" Alias "SystemParametersInfoA"...
如果不明显,您可以调用它们作为
SetSysParam
GetSysParam.

代码分析/FxCop可能会抱怨您使用这些函数的方式,因此下面介绍如何实现API调用,从而避免这种情况。从名为
NativeMethods
的类开始:

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

' alias a ByRef version for GETting the speed, but this is not
' needed since NET offers a cleaner way
<DllImport("user32.dll", EntryPoint:="SystemParametersInfo", SetLastError:=True)>
Private Shared Function SystemParametersInfoPVByRef(uiAction As SPI,
        uiParam As UInteger,
        ByRef pvParam As IntPtr,
        fWinIni As SPIF) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function

 ' other stuff for the last param
<Flags>
 Enum SPIF
     None = &H0
     SPIF_UPDATEINIFILE = &H1
     SPIF_SENDCHANGE = &H2
     SPIF_SENDWININICHANGE = &H2
 End Enum

' there are lots and lots of Sys Params, so use an Enum to group them
Enum SPI
    SETMOUSESPEED = &H71
    GETMOUSESPEED = &H70
End Enum

' exposed "wrapper" for setting the value to allow a more
' meaningful name and hiding gory details of Msg values etc
Friend Shared Function SetMouseSpeed(speed As Integer) As Boolean
     ' somewhat optional error checking 
     If speed < 1 Then speed = 1
     If speed > 20 Then speed = 20

     Return SystemParametersInfo(SPI.SETMOUSESPEED, 0, New IntPtr(speed), SPIF.None)
End Function

Friend Shared Function GetMouseSpeed() As Integer
    Dim speed As New IntPtr(0)

    ' the caller will have to evaluate the return to see if this
    ' succeeded
    If SystemParametersInfoPVByRef(SPI.GETMOUSESPEED, 0, speed, SPIF.None) Then
        Return speed.ToInt32
    Else
        Return -1           ' magic number that API call failed
    End If

End Function
“包装器”不仅可以在代码中隐藏API不整洁的细节,还可以解决API调用失败时该怎么办。在获取速度的情况下,它计算出如果/当调用失败时返回什么值而不是当前速度,不太可能被误读为速度值(比如-1)。正如代码中所指出的,有一种很好的方法可以在.NET中获得鼠标速度,从而不需要重载:

mySpeed = System.Windows.Forms.SystemInformation.MouseSpeed

最后,我要提到的是,单方面更改用户系统上用户鼠标的设置不是一个好主意。他们选择这个值是有原因的。如果有充分的理由这样做,那么当应用程序退出时,应该恢复原始值


技术说明:我通过CA运行了这个程序,以确保所有内容的大小,作为@puropoix answer的补充,我将分享我过去出于各种目的需要编写的所有
系统参数INFO
声明

用法示例:

' Set Mouse Speed 
' ( Minimum: 1, Maximum: 20 )
 SystemParametersInfo(SPI.SPI_SETMOUSESPEED, False, 15UI, SPIF.None)
注意:我没有修复一些在API数据类型声明中抛出VS代码分析工具的可移植警告

''' <summary>
''' ( For setting a Boolean pvParam )
''' Retrieves or sets the value of one of the system-wide parameters.
''' This function can also update the user profile while setting a parameter.
''' </summary>
''' <param name="uiAction">
''' The system-wide parameter to be retrieved or set.
''' </param>
''' <param name="uiParam">
''' A parameter whose usage and format depends on the system parameter being queried or set. 
''' For more information about system-wide parameters, see the uiAction parameter. 
''' If not otherwise indicated, you must specify zero for this parameter.
''' </param>
''' <param name="pvParam">
''' A parameter whose usage and format depends on the system parameter being queried or set. 
''' For more information about system-wide parameters, see the uiAction parameter. 
''' If not otherwise indicated, you must specify NULL for this parameter.
''' For information on the PVOID datatype, see Windows Data Types.</param>
''' <param name="fWinIni">
''' If a system parameter is being set, specifies whether the user profile is to be updated, 
''' and if so, whether the WM_SETTINGCHANGE message is to be broadcast to all top-level windows to 
''' notify them of the change.
''' This parameter can be zero if you do not want to update the user profile or broadcast the WM_SETTINGCHANGE message, 
''' or it can be one or more of the following values.
''' </param>
<DllImport("user32.dll", SetLastError:=True)>
Friend Shared Function SystemParametersInfo(
              ByVal uiAction As SPI,
              ByVal uiParam As UInteger,
              ByVal pvParam As Boolean,
              ByVal fWinIni As SPIF
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function

''' <summary>
''' ( For getting a Byref Boolean pvParam )
''' Retrieves or sets the value of one of the system-wide parameters.
''' This function can also update the user profile while setting a parameter.
''' </summary>
''' <param name="uiAction">
''' The system-wide parameter to be retrieved or set.
''' </param>
''' <param name="uiParam">
''' A parameter whose usage and format depends on the system parameter being queried or set. 
''' For more information about system-wide parameters, see the uiAction parameter. 
''' If not otherwise indicated, you must specify zero for this parameter.
''' </param>
''' <param name="pvParam">
''' A parameter whose usage and format depends on the system parameter being queried or set. 
''' For more information about system-wide parameters, see the uiAction parameter. 
''' If not otherwise indicated, you must specify NULL for this parameter.
''' For information on the PVOID datatype, see Windows Data Types.</param>
''' <param name="fWinIni">
''' If a system parameter is being set, specifies whether the user profile is to be updated, 
''' and if so, whether the WM_SETTINGCHANGE message is to be broadcast to all top-level windows to 
''' notify them of the change.
''' This parameter can be zero if you do not want to update the user profile or broadcast the WM_SETTINGCHANGE message, 
''' or it can be one or more of the following values.
''' </param>
<DllImport("user32.dll", EntryPoint:="SystemParametersInfo", SetLastError:=True)>
Friend Shared Function SystemParametersInfoByRefpv(
              ByVal uiAction As SPI,
              ByVal uiParam As UInteger,
              ByRef pvParam As Boolean,
              ByVal fWinIni As SPIF
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function

''' <summary>
''' ( For setting a Boolean uiParam and Boolean pvParam parameter )
''' Retrieves or sets the value of one of the system-wide parameters.
''' This function can also update the user profile while setting a parameter.
''' </summary>
''' <param name="uiAction">
''' The system-wide parameter to be retrieved or set.
''' </param>
''' <param name="uiParam">
''' A parameter whose usage and format depends on the system parameter being queried or set. 
''' For more information about system-wide parameters, see the uiAction parameter. 
''' If not otherwise indicated, you must specify zero for this parameter.
''' </param>
''' <param name="pvParam">
''' A parameter whose usage and format depends on the system parameter being queried or set. 
''' For more information about system-wide parameters, see the uiAction parameter. 
''' If not otherwise indicated, you must specify NULL for this parameter.
''' For information on the PVOID datatype, see Windows Data Types.</param>
''' <param name="fWinIni">
''' If a system parameter is being set, specifies whether the user profile is to be updated, 
''' and if so, whether the WM_SETTINGCHANGE message is to be broadcast to all top-level windows to 
''' notify them of the change.
''' This parameter can be zero if you do not want to update the user profile or broadcast the WM_SETTINGCHANGE message, 
''' or it can be one or more of the following values.
''' </param>
<DllImport("user32.dll", SetLastError:=True)>
Friend Shared Function SystemParametersInfo(
              ByVal uiAction As SPI,
              ByVal uiParam As Boolean,
              ByVal pvParam As Boolean,
              ByVal fWinIni As SPIF
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function

''' <summary>
''' ( For setting a IntPtr pvParam parameter )
''' Retrieves or sets the value of one of the system-wide parameters.
''' This function can also update the user profile while setting a parameter.
''' </summary>
''' <param name="uiAction">
''' The system-wide parameter to be retrieved or set.
''' </param>
''' <param name="uiParam">
''' A parameter whose usage and format depends on the system parameter being queried or set. 
''' For more information about system-wide parameters, see the uiAction parameter. 
''' If not otherwise indicated, you must specify zero for this parameter.
''' </param>
''' <param name="pvParam">
''' A parameter whose usage and format depends on the system parameter being queried or set. 
''' For more information about system-wide parameters, see the uiAction parameter. 
''' If not otherwise indicated, you must specify NULL for this parameter.
''' For information on the PVOID datatype, see Windows Data Types.</param>
''' <param name="fWinIni">
''' If a system parameter is being set, specifies whether the user profile is to be updated, 
''' and if so, whether the WM_SETTINGCHANGE message is to be broadcast to all top-level windows to 
''' notify them of the change.
''' This parameter can be zero if you do not want to update the user profile or broadcast the WM_SETTINGCHANGE message, 
''' or it can be one or more of the following values.
''' </param>
<DllImport("user32.dll", SetLastError:=True)>
Friend Shared Function SystemParametersInfo(
              ByVal uiAction As SPI,
              ByVal uiParam As UInteger,
              ByVal pvParam As IntPtr,
              ByVal fWinIni As SPIF
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function

''' <summary>
''' ( For setting an UInteger uiParam and pvParam parameters )
''' Retrieves or sets the value of one of the system-wide parameters.
''' This function can also update the user profile while setting a parameter.
''' </summary>
''' <param name="uiAction">
''' The system-wide parameter to be retrieved or set.
''' </param>
''' <param name="uiParam">
''' A parameter whose usage and format depends on the system parameter being queried or set. 
''' For more information about system-wide parameters, see the uiAction parameter. 
''' If not otherwise indicated, you must specify zero for this parameter.
''' </param>
''' <param name="pvParam">
''' A parameter whose usage and format depends on the system parameter being queried or set. 
''' For more information about system-wide parameters, see the uiAction parameter. 
''' If not otherwise indicated, you must specify NULL for this parameter.
''' For information on the PVOID datatype, see Windows Data Types.</param>
''' <param name="fWinIni">
''' If a system parameter is being set, specifies whether the user profile is to be updated, 
''' and if so, whether the WM_SETTINGCHANGE message is to be broadcast to all top-level windows to 
''' notify them of the change.
''' This parameter can be zero if you do not want to update the user profile or broadcast the WM_SETTINGCHANGE message, 
''' or it can be one or more of the following values.
''' </param>
<DllImport("user32.dll", SetLastError:=True)>
Friend Shared Function SystemParametersInfo(
              ByVal uiAction As SPI,
              ByVal uiParam As UInteger,
              ByVal pvParam As UInteger,
              ByVal fWinIni As SPIF
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function

''' <summary>
''' ( For setting an Boolean uiParam and UInteger pvParam )
''' Retrieves or sets the value of one of the system-wide parameters.
''' This function can also update the user profile while setting a parameter.
''' </summary>
''' <param name="uiAction">
''' The system-wide parameter to be retrieved or set.
''' </param>
''' <param name="uiParam">
''' A parameter whose usage and format depends on the system parameter being queried or set. 
''' For more information about system-wide parameters, see the uiAction parameter. 
''' If not otherwise indicated, you must specify zero for this parameter.
''' </param>
''' <param name="pvParam">
''' A parameter whose usage and format depends on the system parameter being queried or set. 
''' For more information about system-wide parameters, see the uiAction parameter. 
''' If not otherwise indicated, you must specify NULL for this parameter.
''' For information on the PVOID datatype, see Windows Data Types.</param>
''' <param name="fWinIni">
''' If a system parameter is being set, specifies whether the user profile is to be updated, 
''' and if so, whether the WM_SETTINGCHANGE message is to be broadcast to all top-level windows to 
''' notify them of the change.
''' This parameter can be zero if you do not want to update the user profile or broadcast the WM_SETTINGCHANGE message, 
''' or it can be one or more of the following values.
''' </param>
<DllImport("user32.dll", SetLastError:=True)>
Friend Shared Function SystemParametersInfo(
              ByVal uiAction As SPI,
              ByVal uiParam As Boolean,
              ByVal pvParam As UInteger,
              ByVal fWinIni As SPIF
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function

''' <summary>
''' ( For setting a String pvParam parameter )
''' Retrieves or sets the value of one of the system-wide parameters.
''' This function can also update the user profile while setting a parameter.
''' </summary>
''' <param name="uiAction">
''' The system-wide parameter to be retrieved or set.
''' </param>
''' <param name="uiParam">
''' A parameter whose usage and format depends on the system parameter being queried or set. 
''' For more information about system-wide parameters, see the uiAction parameter. 
''' If not otherwise indicated, you must specify zero for this parameter.
''' </param>
''' <param name="pvParam">
''' A parameter whose usage and format depends on the system parameter being queried or set. 
''' For more information about system-wide parameters, see the uiAction parameter. 
''' If not otherwise indicated, you must specify NULL for this parameter.
''' For information on the PVOID datatype, see Windows Data Types.</param>
''' <param name="fWinIni">
''' If a system parameter is being set, specifies whether the user profile is to be updated, 
''' and if so, whether the WM_SETTINGCHANGE message is to be broadcast to all top-level windows to 
''' notify them of the change.
''' This parameter can be zero if you do not want to update the user profile or broadcast the WM_SETTINGCHANGE message, 
''' or it can be one or more of the following values.
''' </param>
<DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True, BestFitMapping:=False)>
Friend Shared Function SystemParametersInfo(
              ByVal uiAction As UInteger,
              ByVal uiParam As UInteger,
              ByVal pvParam As String,
              ByVal fWinIni As SPIF
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function

''' <summary>
''' ( For reading a String pvParam parameter )
''' Retrieves or sets the value of one of the system-wide parameters.
''' This function can also update the user profile while setting a parameter.
''' </summary>
''' <param name="uiAction">
''' The system-wide parameter to be retrieved or set.
''' </param>
''' <param name="uiParam">
''' A parameter whose usage and format depends on the system parameter being queried or set. 
''' For more information about system-wide parameters, see the uiAction parameter. 
''' If not otherwise indicated, you must specify zero for this parameter.
''' </param>
''' <param name="pvParam">
''' A parameter whose usage and format depends on the system parameter being queried or set. 
''' For more information about system-wide parameters, see the uiAction parameter. 
''' If not otherwise indicated, you must specify NULL for this parameter.
''' For information on the PVOID datatype, see Windows Data Types.</param>
''' <param name="fWinIni">
''' If a system parameter is being set, specifies whether the user profile is to be updated, 
''' and if so, whether the WM_SETTINGCHANGE message is to be broadcast to all top-level windows to 
''' notify them of the change.
''' This parameter can be zero if you do not want to update the user profile or broadcast the WM_SETTINGCHANGE message, 
''' or it can be one or more of the following values.
''' </param>
<DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True, BestFitMapping:=False)>
Friend Shared Function SystemParametersInfo(
              ByVal uiAction As UInteger,
              ByVal uiParam As UInteger,
              ByVal pvParam As StringBuilder,
              ByVal fWinIni As SPIF
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function

''' <summary>
''' ( For setting a AnimationInfo pvParam parameter )
''' Retrieves or sets the value of one of the system-wide parameters.
''' This function can also update the user profile while setting a parameter.
''' </summary>
''' <param name="uiAction">
''' The system-wide parameter to be retrieved or set.
''' </param>
''' <param name="uiParam">
''' A parameter whose usage and format depends on the system parameter being queried or set. 
''' For more information about system-wide parameters, see the uiAction parameter. 
''' If not otherwise indicated, you must specify zero for this parameter.
''' </param>
''' <param name="pvParam">
''' A parameter whose usage and format depends on the system parameter being queried or set. 
''' For more information about system-wide parameters, see the uiAction parameter. 
''' If not otherwise indicated, you must specify NULL for this parameter.
''' For information on the PVOID datatype, see Windows Data Types.</param>
''' <param name="fWinIni">
''' If a system parameter is being set, specifies whether the user profile is to be updated, 
''' and if so, whether the WM_SETTINGCHANGE message is to be broadcast to all top-level windows to 
''' notify them of the change.
''' This parameter can be zero if you do not want to update the user profile or broadcast the WM_SETTINGCHANGE message, 
''' or it can be one or more of the following values.
''' </param>
<DllImport("user32.dll", SetLastError:=True)> _
Friend Shared Function SystemParametersInfo(
              ByVal uiAction As SPI,
              ByVal uiParam As UInteger,
              ByRef pvParam As AnimationInfo,
              ByVal fWinIni As SPIF
) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
“”
''(用于设置布尔pvParam)
''检索或设置一个系统范围参数的值。
''此功能还可以在设置参数时更新用户配置文件。
''' 
''' 
''要检索或设置的系统范围参数。
''' 
''' 
''一个参数,其用法和格式取决于所查询或设置的系统参数。
''有关系统范围参数的更多信息,请参阅uiAction参数。
''如果未另行指示,则必须为此参数指定零。
''' 
''' 
''一个参数,其用法和格式取决于所查询或设置的系统参数。
''有关系统范围参数的更多信息,请参阅uiAction参数。
''如果未另行指示,则必须为此参数指定NULL。
''有关PVOID数据类型的信息,请参阅Windows数据类型。
''' 
''如果正在设置系统参数,则指定是否更新用户配置文件,
'',如果是,是否将WM_设置更改消息广播到所有顶级窗口,以
''通知他们变更。
''如果您不想更新用户配置文件或广播WM_SETTINGCHANGE消息,此参数可以为零,
''或它可以是以下一个或多个值。
''' 
朋友共享函数系统参数信息(
ByVal uiAction作为SPI,
ByVal uiParam作为UInteger,
ByVal pvParam作为布尔值,
ByVal fWinIni作为SPIF
)作为布尔值
端函数
''' 
''(用于获取Byref布尔pvParam)
''检索或设置一个系统范围参数的值。
''此功能还可以在设置参数时更新用户配置文件。
''' 
''' 
''要检索或设置的系统范围参数。
''' 
''' 
''一个参数,其用法和格式取决于所查询或设置的系统参数。
''有关系统范围参数的更多信息,请参阅uiAction参数。
''如果未另行指示,则必须为此参数指定零。
''' 
''' 
''一个参数,其用法和格式取决于所查询或设置的系统参数。
''有关系统范围参数的更多信息,请参阅uiAction参数。
''如果未另行指示,则必须为此参数指定NULL。
''有关PVOID数据类型的信息,请参阅Windows数据类型。
''' 
''如果正在设置系统参数,则指定是否更新用户配置文件,
'',如果是,是否将WM_设置更改消息广播到所有顶级窗口,以
''通知他们变更。
''如果您不想更新用户配置文件或广播WM_SETTINGCHANGE消息,此参数可以为零,
''或它可以是以下一个或多个值。
''' 
Friend共享函数系统参数sinfoByRefPV(
ByVal uiAction作为SPI,
ByVal uiParam作为UInteger,
ByRef pvParam作为布尔值,
ByVal fWinIni作为SPIF
)作为布尔值
端函数
''' 
''(用于设置布尔uiParam和布尔pvParam参数)
''检索或设置一个系统范围参数的值。
''此功能还可以在设置参数时更新用户配置文件。
''' 
''' 
''要检索或设置的系统范围参数。
''' 
''' 
''一个参数,其用法和格式取决于所查询或设置的系统参数。
''有关系统范围参数的更多信息,请参阅uiAction参数。
''如果未另行指示,则必须为此参数指定零。
''' 
''' 
''一个参数,其用法和格式取决于所查询或设置的系统参数。
'''