“ BlockInput”功能在程序中不起作用

时间:2019-09-20 02:37:21

标签: vb.net visual-studio-2017

我正在创建一个程序,它将在用户要求的任何位置单击屏幕。但是我想在程序执行单击时禁用鼠标移动/单击,否则,如果用户在程序执行单击时移动鼠标,则可能会将鼠标从原本应单击的位置拖出。

目前我尝试使用:

Private Declare Function BlockInput Lib "user32" Alias "BlockInput" (ByVal fBlock As Boolean) As Boolean

Private Declare Function BlockInput Lib "user32" Alias "BlockInput" (ByVal fBlock As Integer) As Integer

然后根据我声明`BlockInput'函数的方式调用BlockInput(True/False)BlockInput(1/0)

当前,当我在屏幕上拖动鼠标时,都无法阻止鼠标移动。

我是否需要在代码中包含某些内容以使此功能正常工作?我知道以这种方式禁用鼠标也会使程序本身也无法执行点击操作,但由于无法做到这一点,我什至无法进行测试。

感谢您的帮助,谢谢!

1 个答案:

答案 0 :(得分:1)

经过测试,让我重新启动:)小心。争取胜利10

Imports System.Runtime.InteropServices

Public Class Form1

<DllImport("user32.dll", EntryPoint:="BlockInput")> _
Private Shared Function BlockInput(<MarshalAs(UnmanagedType.Bool)> ByVal fBlockIt As Boolean) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    BlockInput(True)
End Sub

End Class