如何处理用户在表单外单击鼠标? [英] How to handle user clicking mouse outside a form?

查看:25
本文介绍了如何处理用户在表单外单击鼠标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 VB.NET 中创建一个项目,我们在其中跟踪光标在屏幕上任意位置的位置.
在 Timer 1_ Tick Event 中,我将频率设置为 1 毫秒,以便它可以更频繁地跟踪位置.以下是我在 Timer1_Tick 事件中插入的代码

I am making a project in VB.NET in which we track the cursor's location anywhere on the screen.
In Timer 1_ Tick Event, I have set frequency as 1 millisecond so that it can track the location more frequently. The following is the code I have inserted in Timer1_Tick event

    Me.Cursor = New Cursor(Cursor.Current.Handle)
    Label4.Text = Cursor.Position.X
    Label5.Text = Cursor.Position.Y

我希望用户在表单外单击并拖动以获取所选区域的尺寸(以像素为单位).任何人都可以帮助如何控制表单外的鼠标点击?

I want the user to click and drag outside the form to get dimensions of the selected area in pixels. Can anyone please help how to control mouse clicking outside a form?

提前致谢!

推荐答案

听起来您正在尝试从应用程序外部获取一个区域,您可以尝试使用 Global Hook,例如可从此 CodePlex 项目.

It sounds like you are trying to get an area from outside of your application, you could try using a Global Hook, such as is available from this CodePlex Project.

这是一个简单的例子,它响应 MouseDown 和 MouseUp 事件,创建一个矩形,然后将结果打印到一个 TextBox,它应该让你知道如何继续.

This is a simple example that is responding to the MouseDown and MouseUp events creating a Rectangle then printing the results to a TextBox it should give you an idea how to proceed.

Imports MouseKeyboardActivityMonitor
Imports MouseKeyboardActivityMonitor.WinApi

Public Class Form1
    Private m_MouseHookManager As MouseHookListener
    Dim tempPoint As Point

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        m_MouseHookManager = New MouseHookListener(New GlobalHooker)
        m_MouseHookManager.Enabled = True
        AddHandler m_MouseHookManager.MouseUp, AddressOf HookManager_MouseUp
        AddHandler m_MouseHookManager.MouseDown, AddressOf HookManager_MouseDown
        TextBox1.ScrollBars = ScrollBars.Vertical
    End Sub

    Private Sub HookManager_MouseUp(sender As Object, e As MouseEventArgs)
        Dim r As Rectangle = New Rectangle(tempPoint.X, tempPoint.Y, e.X - tempPoint.X, e.Y - tempPoint.Y)
        TextBox1.Text += "Left: " & r.Left & vbCrLf
        TextBox1.Text += "Right: " & r.Top & vbCrLf
        TextBox1.Text += "Width: " & r.Width & vbCrLf
        TextBox1.Text += "Height: " & r.Height & vbCrLf
        tempPoint = Point.Empty
    End Sub


    Private Sub HookManager_MouseDown(sender As Object, e As MouseEventArgs)
        tempPoint = e.Location
    End Sub
End Class

这篇关于如何处理用户在表单外单击鼠标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆