获取光标位置相对于控制 - C# [英] Get cursor position with respect to the control - C#

查看:112
本文介绍了获取光标位置相对于控制 - C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要得到鼠标的位置相对于该鼠标指针present控制。这意味着,当我将光标放在起点(左上角)控制的也应该给(0,0)。我使用下面的code:

I want to get the mouse position with respect to the control in which mouse pointer is present. That means when I place the cursor to the starting point (Top-Left corner) of control it should give (0,0). I am using the following code:

    private void panel1_MouseMove(object sender, MouseEventArgs e)
    {
        this.Text = Convert.ToString(Cursor.Position.X + ":" + Cursor.Position.Y);         
    } 

但是,这给出关于位置屏幕不控制。

But this gives the position with respect to the screen not to the control.

code样品将AP preciated。

Code sample will be appreciated.

推荐答案

您可以直接使用<$c$c>Location传递到您的事件处理程序的 MouseEventArgs 参数的属性。

You can directly use the Location property of the MouseEventArgs argument passed to your event-handler.

private void panel1_MouseMove(object sender, MouseEventArgs e)
{
    Text = e.Location.X + ":" + e.Location.Y;      
} 

这篇关于获取光标位置相对于控制 - C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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