当我的鼠标点击某处时,如何获得鼠标坐标? [英] How to get mouse cordinates when my mouse click somewhere?

查看:78
本文介绍了当我的鼠标点击某处时,如何获得鼠标坐标?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检索鼠标指针的x,y当我的鼠标点击屏幕中的任何地方时,它应该独立于窗体。

ex。如果我在记事本中单击鼠标它应该给我cordinates,我试着跟随它但只有当我点击myform时才给出了协调。



请建议我解决这个问题。

谢谢。



我的尝试:



I want to retrieve the mouse pointer's x , y cordinates when my mouse clicks anywhere in the screen , it should be independent of windows form .
ex. if i click mouse in notepad it should give me cordinates , i tried following but it gives cordinated only when i clicked in myform.

please suggest me way out of this issue.
thanks.

What I have tried:

private void Form1_MouseClick(object sender, MouseEventArgs e)
{
string x_cord = Cursor.Position.X.ToString();
string y_cord = Cursor.Position.Y.ToString();
MessageBox.Show("cordinates are " + x_cord +","+ y_cord);
}

推荐答案

这不好,完全没有。你所说的是拦截那些不适用于你的表格的鼠标动作,而这并不是它应该工作的方式。

因此,这很难。非常困难。并且也很危险......

你可以做到,但你需要一个Global Hook:全球Windows挂钩 [ ^ ]

请注意:如果你弄错了 - 甚至稍微 - 你可以使你的整个系统不稳定并可能导致数据丢失。

在你这样做之前要三思而后行:你可能不得不在开发中重启你的PC几次,我建议你做一个比我平常更严格的备份程序(这来自某人的系统每小时增量 ...
That's not nice, not at all. You are talking about intercepting mouse actions that aren;t intended for your form, and that's not the way it's all supposed to work.
As a result, it's difficult. Very difficult. And also dangerous...
You can do it, but you need a Global Hook: Global Windows Hooks[^]
Be aware: if you get it wrong - even slightly - you can render your whole system unstable and may "cause data loss".
Think twice before you do this: you will probably have to reboot your PC quite a few times in development, and I'd recommend an even more rigorous routine of backups than I normally would (and this from someone whose system does incrementals every hour...


要获得鼠标位置,请检查



鼠标.GetPosition方法(IInputElement)(System.Windows.Input) [ ^ ]
To get mouse position,please check

Mouse.GetPosition Method (IInputElement) (System.Windows.Input)[^]


这篇关于当我的鼠标点击某处时,如何获得鼠标坐标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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