在C#中单击鼠标获取鼠标坐标

发布于 2024-09-07 10:04:20 字数 394 浏览 4 评论 0原文

我想为自己构建一个应用程序,该应用程序将在后台(系统托盘)运行,并在单击左按钮或右键时记录鼠标的坐标。我在网上找到了这段代码,通过触发的事件获取鼠标坐标:

protected override void OnMouseMove(MouseEventArgs mouseEv) 
{ 
    txtMouseX.Text = mouseEv.X.ToString(); 
    txtMouseY.Text = mouseEv.Y.ToString(); 
}

这为我指明了正确的方向,我现在知道如何获取鼠标的 X 和 Y 坐标,但我不知道如果应用程序在后台运行,如何通过鼠标单击触发功能或其他内容。另外,这段代码似乎只适用于它运行时的形式,而不适用于整个屏幕?

任何帮助表示赞赏,干杯。

I want to build myself an application that will run in the background (system tray) and log the co-ordinates of my mouse whenever I click the left or right buttons. I found this code online to get the mouse co-ordinates through an event that triggers:

protected override void OnMouseMove(MouseEventArgs mouseEv) 
{ 
    txtMouseX.Text = mouseEv.X.ToString(); 
    txtMouseY.Text = mouseEv.Y.ToString(); 
}

This points me in the right direction, I now know how I can get the X and Y co-ordinates of the mouse, but I don't know how to trigger a function or whatever on a mouse click if the application is running in the background. Also, it seems that this code only works in the form it is running in, not for the entire screen?

Any help is appreciated, cheers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

放飞的风筝 2024-09-14 10:04:20

你需要一个全局钩子。这是一篇代码项目文章,其中包含一个可以满足您需求的库: 全局鼠标和键盘库

You'll need a global hook. Here is a code project article with a library that does what you are looking for: Global Mouse and Keyboard Library

不乱于心 2024-09-14 10:04:20

您可以随时使用 GetCursorPos win32 API 获取鼠标的位置。以下是示例代码:-

Dim MyPointAPI As POINTAPI

私有类型 POINTAPI
X只要
只要Y
End Type

Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long

Public Sub Timer1_Timer()
l = GetCursorPos(MyPointAPI)
Label1.Caption = CStr(MyPointAPI.X) & ”、“ & CStr(MyPointAPI.Y)
结束子

You can use GetCursorPos win32 API to get the position of your Mouse at any time. Here is the sample code for this:-

Dim MyPointAPI As POINTAPI

Private Type POINTAPI
X As Long
Y As Long
End Type

Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long

Public Sub Timer1_Timer()
l = GetCursorPos(MyPointAPI)
Label1.Caption = CStr(MyPointAPI.X) & ", " & CStr(MyPointAPI.Y)
End Sub

酒与心事 2024-09-14 10:04:20

看一下这个库 http://globalmousekeyhook.codeplex.com/
即使您的应用程序处于非活动状态(在托盘中),它也允许您在整个范围内跟踪鼠标坐标系。

Take a look at this library http://globalmousekeyhook.codeplex.com/.
It allows you to track mouse coordinate system wide even if your application is inactive (in tray).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文