捕捉鼠标长按消息

hzfushi201 2012-05-29 04:41:57
不论界面是否打开,是否当前显示。
当鼠标左键在指定的屏幕区域按下5s后再放开,这时程序会触发相应的处理。
如果一般的左键按下,很快就放开,则不会有相应的处理。

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

不知道哪位大侠知道,万分感谢。(能有代码最好)
...全文
146 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
hzfushi201 2012-06-09
  • 打赏
  • 举报
回复
看来只能用钩子,实现虽然已经没有问题,但运行时感觉影响了性能,随便鼠标移动一下,感觉有延迟,很卡,有没什么办法解决挂了钩子后,不影响卡的方案。谢谢
E次奥 2012-05-29
  • 打赏
  • 举报
回复
Timer,tick事件计时!在mousedown事件里触发tick事件!
caoqinghua 2012-05-29
  • 打赏
  • 举报
回复
不论界面是否打开,是否当前显示
只能用钩子,我有一个检测鼠标移动坐标的,如下:

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

请叫我卷福 2012-05-29
  • 打赏
  • 举报
回复
没有现成代码

要用到 鼠标钩子

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

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧