鼠标座标 [英] Mouse coordinates

查看:98
本文介绍了鼠标座标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我是C#编程的新手,我知道对大多数人来说这很简单,但是我正在学习并且找不到答案.这是问题所在.我有一个表单,上面有很多对象,例如标签,图片框等.我需要获取鼠标坐标,并且可以使用
来获取它

Ok I am new to programming C# and I know this is simple to most of you but I am learning and couldn''t find an answer out there. Here is the problem. I have a form and has bunch of objects on it like labels, picturebox and etc. I need to get the mouse coordinates and I am able to get it using

private void TestApp_MouseClick(object sender, MouseEventArgs e)
        {

            Point p = new Point(e.X, e.Y);
            Point p2 = PointToClient(p);

            Clipboard.SetText("X = " + p2.X + ", Y = " + p2.Y);
        }


并复制到剪贴板.问题是当它在对象上时,它不给我坐标:(我希望坐标为表单左上角的(0,0).如果有人可以帮助我,我将不胜感激. br/>
谢谢您,


and copy it a clipboard. The problem is this doesn''t give me the coordinates when it is on an object :( I want the coordinates as (0,0) top left of the form. If some one can help me out I would really appreciate it.

Thank you,

推荐答案

有两件事要解决.

首先,我认为您在使用该代码时会遇到麻烦,因为PointToClient用于处理相对于屏幕的光标位置.

您可能想要这样做(它将在任何控件上的多个监视器上运行):
There are two things to address.

First, I think you''ll have some trouble with that code as PointToClient is meant to work with cursor location relative to the screen.

You might want this instead (which will work on multiple monitors, from any control):
private void TestApp_MouseClick(object sender, MouseEventArgs e)
{
    Point p2 = PointToClient(Cursor.Position);
    string coords = string.Format("({0},{1})", p2.X, p2.Y);
    Clipboard.SetText(coords);
}



其次,您将需要一个事件处理程序来处理表单上的每个控件,或者需要一个事件处理程序来处理所有控件.和第二个一起去...方法如下:


    • 转到表单设计器,单击表单(不在控件上),然后按CTRL-A.这将选择所有控件.


    • Secondly, you''ll need an event handler for each of the controls on your form, or one event handler to handle them all. Go with the second one...here''s how:


        • Go to your form designer, click in the form (not on a control) and press CTRL-A. This will select all controls.
          • 在属性窗口中,单击闪电以查看事件.
          • 单击MouseClick事件以获取下拉列表.它应该包含您已经编写的函数的名称.
          • 选择您编写的功能!
          this.button1.MouseClick += new System.Windows.Forms.MouseEventHandler(this. TestApp_MouseClick);



          基本上,当您从下拉列表中选择方法时,它将为您连接事件处理程序.

          干杯!



          Basically, it wires up an event handler for you when you select a method from the drop down.

          Cheers!


          嘿!不用担心!发表问题没什么不好的!

          您的问题是这样的:当您在表单区域上单击鼠标时,会将 Click事件发送到表单,但是如果通过子控件( Label TextBox 等),该事件将发送到该对象,并且如果您希望父窗体捕获该事件,则需要为每个控件显式绑定该事件.

          设计器上,选择表单上的每个控件,然后在属性窗口中选择动作,然后为每个控件为 Click事件添加一个处理程序.默认情况下,设计器为名为controlName_Click的每个控件添加一个不同的 handler ,但是您可以设置相同的 handler 通过在事件名称右侧的文本框中手动键入 handler 名称来获得所有控件.
          Hey! Don''t worry! There is nothing bad in posting a question!

          Your problem is this: when you click the mouse on an area of your form the Click event is sent to the form, but if you do it over a child control (Label, TextBox, etc.) the event is sent to that object, and if you want the parent form to catch the event you need to explicitly bind the event for each control.

          On the designer select each control on your form and in the property window select Actions, then for each control add an handler for the Click event. By default the designer add a different handler for each control named controlName_Click, but you can set the same handler for all the controls by manually typing the handler name in the text-box on the right of the event name.


          这篇关于鼠标座标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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