日期:2014-05-18  浏览次数:20972 次

捕捉鼠标长按消息
不论界面是否打开,是否当前显示。
当鼠标左键在指定的屏幕区域按下5s后再放开,这时程序会触发相应的处理。
如果一般的左键按下,很快就放开,则不会有相应的处理。

请问,
1.怎样检测在屏幕指定的区域按下。
2.怎样检测鼠标长按5s。

不知道哪位大侠知道,万分感谢。(能有代码最好)

------解决方案--------------------
没有现成代码

要用到 鼠标钩子

可以监控鼠标的行为 不管当前程序是否在最前


------解决方案--------------------
不论界面是否打开,是否当前显示
只能用钩子,我有一个检测鼠标移动坐标的,如下:
VB.NET code

Imports System.Runtime.InteropServices

Public Class Form1
    Public Sub New()
        InitializeComponent()
        Me.webhtml.Url = New System.Uri(Application.StartupPath + "\test.html", System.UriKind.Absolute)
    End Sub


    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click

        Me.webhtml.ShowPageSetupDialog()
        Me.Text = ("ShowPageSetupDialog")

    End Sub

    Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click
        Me.webhtml.ShowPrintPreviewDialog()
    End Sub

    Protected Overrides Sub WndProc(ByRef m As Message)
        MyBase.WndProc(m)
    End Sub

    Dim hook As New Win32Hook()
  
    Private Sub hook_onMouseChange(ByVal sender As Object, ByVal e As EventArgs)
        Try
            Me.Text = Cursor.Position.ToString()
        Catch ex As Exception
        End Try
    End Sub

    Private Sub ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton3.Click
        AddHandler hook.onMouseChange, AddressOf hook_onMouseChange
        hook.SetHook()
    End Sub

    Private Sub ToolStripButton4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton4.Click
        RemoveHandler hook.onMouseChange, AddressOf hook_onMouseChange
    End Sub

End Class

Public Class Win32Hook

    <DllImport("kernel32")> _
    Public Shared Function GetCurrentThreadId() As Integer
    End Function

    <DllImport("user32", CharSet:=CharSet.Auto, CallingConvention:=CallingConvention.StdCall)> _
    Public Shared Function SetWindowsHookEx(ByVal idHook As HookType, ByVal lpfn As HOOKPROC, ByVal hmod As Integer, ByVal dwThreadId As Integer) As Integer
    End Function

    Public Enum HookType
        WH_GETMESSAGE = 5 '系统都会调用WH_CBT   Hook子程,这些事件包括
    End Enum

    Public Delegate Function HOOKPROC(ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer

    Public Event onMouseChange As System.EventHandler

    Public Sub SetHook()
        SetWindowsHookEx(HookType.WH_GETMESSAGE, New HOOKPROC(AddressOf Me.MyKeyboardProc), 0, GetCurrentThreadId())
    End Sub

    Public Function MyKeyboardProc(ByVal nCode As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
        RaiseEvent onMouseChange(Nothing, Nothing)
        Return 0
    End Function

End Class

------解决方案--------------------
Timer,tick事件计时!在mousedown事件里触发tick事件!