微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

是否有 Hooking 的替代方法来检测 Windows Forms 之外的事件?

如何解决是否有 Hooking 的替代方法来检测 Windows Forms 之外的事件?

我想弄清楚如何从 Windows 窗体外部检测和处理系统事件,例如按下的键和鼠标单击。有没有不需要 user32.dll 挂钩的替代方法

我的意思的粗略示例:

    Public Sub MouseClick(e As System.MouseEventArgs) Handles Mouse.LeftClick // Left mouse click event raised
        ' even if the user clicks on a browser link in google chrome. Ideally,the click would not even
        ' register in the chrome browser,preventing the user from being directed to the link destination 
        Dim bmp As New Bitmap(1,1)
        Using g As Graphics = Graphics.FromImage(bmp)
            g.copyFromScreen(Windows.Forms.Cursor.Position,New Point(0,0),New Drawing.Size(1,1))
        End Using
        Dim pixel As Drawing.Color = bmp.GetPixel(0,0)
        ArgbID.Text = bmp.GetPixel(0,0).ToString
        Dim p As New Point With {.X = ((Me.Width - Me.Splitter1.Width) / 2) + Me.Splitter1.Width - (ArgbID.Width / 2),.Y = ArgbID.Top}
        ArgbID.Location = p
        displayBox.BackColor = pixel
        Me.Invalidate()
    End Sub

我希望能够检测和处理表单之外的事件。如果我必须使用钩子,那么这是最有前途的例子,应该做我想做的事情: Code Project [Processing Global Mouse and Keyboard Hooks in C#]

解决方法

好的,所以我找到了一种方法来连接鼠标来创建我自己的事件处理程序。以下是我所做的总结:
免责声明 - 这种方法仍然使用 System.Windows.Forms,因此您仍将在表单本身内工作。

Private Sub Button_Click(sender As Object,e As EventArgs)
    Select Case True
        Case sender Is Btn_Help
        Case sender Is Btn_Ok
        Case sender Is Btn_GetPoint
            If ColorChooserClicked = False Then
                // Doing stuff here
                Dim ScreenLock As New TestFormNi With {.Opacity = 0.01,.WindowState = FormWindowState.Maximized,.FormBorderStyle = FormBorderStyle.None}
                ScreenLock.ShowDialog()
            Else
                // Other Stuff
            End If
    End Select
End Sub

还有:

Imports System.Windows.Forms

Public Class TestFormNi
    Private time As Integer = 0
    Private Sub LoadFormNi(sender As Object,e As EventArgs) Handles MyBase.Load
        PictureBox1.Image = Cache.Img
        Timer1.Start()
        Me.TopMost = True
        Me.Invalidate()
    End Sub
    Private Sub ClickDetection(sender As Object,e As EventArgs) Handles Me.Click,PictureBox1.Click
        TestingForm.Btn_GetPoint.PerformClick()
        Me.Dispose()
        TestingForm.Focus()
    End Sub
    Private Sub Escape(sender As Object,e As KeyEventArgs) Handles Me.KeyDown,PictureBox1.KeyDown
        If e.KeyCode = Keys.Escape Then Me.Dispose()
    End Sub
    Private Sub AutoClose(sender As Object,e As EventArgs) Handles Timer1.Tick
        If time > 30 Then TestingForm.Btn_GetPoint.PerformClick() : Me.Dispose() : TestingForm.Focus()
        time += 1
    End Sub
End Class

这样我就可以点击“屏幕”上的任何地方,点击事件仍然会在windows窗体中注册,因为很明显,点击位置仍然在windows.forms中;这也为我省去了必须创建句柄以禁用在其他应用程序中注册的点击的麻烦。 (就像在光标悬停在浏览器链接上时被重定向一样)

注意 - 不透明度级别必须大于 0,否则表单将不会注册事件。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?