开发者

Getting mouse co-ordinates on a mouse click in C#

开发者 https://www.devze.com 2023-01-05 09:12 出处:网络
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

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 d开发者_StackOverflow社区on'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.


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


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


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消