37

我想获取鼠标相对于鼠标指针所在控件的位置。这意味着当我将光标放在控件的起点(左上角)时,它应该给出(0,0)。我正在使用以下代码:

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

但这给出了相对于屏幕的位置而不是控件。

代码示例将不胜感激。

4

11 回答 11

50

使用Control.PointToClient将点从屏幕相对坐标转换为控制相对坐标。如果您需要另辟蹊径,请使用 PointToScreen。

于 2011-11-20T12:42:45.207 回答
37

您可以直接使用传递给事件处理程序Location的参数的属性。MouseEventArgs

private void panel1_MouseMove(object sender, MouseEventArgs e)
{
    Text = e.Location.X + ":" + e.Location.Y;      
} 
于 2011-11-20T12:32:40.730 回答
21

以下将为您提供相对于控件的鼠标坐标。例如,如果鼠标位于控件的左上角,则结果为 (0,0):

var coordinates = yourControl.PointToClient(Cursor.Position);
于 2014-10-13T04:12:14.610 回答
4

可以使用以下方法从绝对坐标中获取相对值和从相对坐标中获取绝对值:

Point Control.PointToClient(Point point);

Point Control.PointToScreen(Point point);
于 2014-02-19T13:51:15.553 回答
3

Cursor.Position 返回屏幕上的点,但 Control.PointToClient(Cursor.Position) 返回控件上的点(例如控件 -> 面板)。在您的情况下,您有 e.Locate 控制哪个返回点。

于 2016-08-03T15:14:09.347 回答
3
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
    Text = panel1.PointToClient(Cursor.Position).ToString();    
} 
于 2020-03-05T08:15:26.633 回答
2

只需从光标位置减去控件的左坐标和上坐标:

this.Text = Convert.ToString(
    Cursor.Position.X - this.Left + ":" +
    Cursor.Position.Y - this.Top);
于 2011-11-20T11:58:01.800 回答
2

我使用 MouseLocation 和 PointToClient 进行检查。然后在计时器中使用它!

bool IsMouseHover(Control c, Control container)
        {
            Point p = Control.MousePosition;
            Point p1 = c.PointToClient(p);
            Point p2 = container.PointToClient(p);
            if (c.DisplayRectangle.Contains(p1) && container.DisplayRectangle.Contains(p2))
            {
                return true;
            }
            return false;
        }
于 2015-10-31T03:25:49.683 回答
0
private void lienzo_MouseLeftButtonDown_1(object sender, MouseButtonEventArgs e)
{
    Point coordenadas = new Point();
    coordenadas = Mouse.GetPosition(lienzo);

    string msg = "Coordenadas mouse :" + coordenadas.X + "," + coordenadas.Y;
    MessageBoxResult resultado;
    string titulo = "Informacion";
    MessageBoxButton botones = MessageBoxButton.OK;
    MessageBoxImage icono = MessageBoxImage.Information;

    resultado = MessageBox.Show(msg, titulo, botones, icono);
}

“lienzo”是我的画布面板

于 2012-12-30T18:50:27.307 回答
0

片段代码如下:

private void Control_MouseMove(object sender, MouseEventArgs e)
{
    var btn = sender as Button;
    var point = e.Location;
    point.X += btn.Location.X;
    point.Y += btn.Location.Y;

    Control findTarget = btn.Parent.GetChildAtPoint(point, GetChildAtPointSkip.Invisible) as Button;
    if (findTarget != null)
    {
        // TO DO
    }
}

该按钮是托管面板中的众多按钮之一。

于 2020-08-23T12:48:48.023 回答
0

创建标准项目 C# WinForms

将 2 个名为 X 和 Y 的文本框以及工具箱中的 Timer 对象放置到设计页面

按 [F7] 并将所有代码替换为以下代码。

using System;
using System.Windows.Forms;

namespace MousePos
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            timer1.Start();
        }

        private void Form1_MouseCaptureChanged(object sender, EventArgs e)
        {
            X.Text = MousePosition.X.ToString();
            Y.Text = MousePosition.Y.ToString();
        }
    }
}

将 Timer.Tick 动作设置为“Form1_MouseCaptureChanged”

[F5] 运行 - 现在你有一个 MosusePos 应用程序。

于 2020-09-08T13:45:50.920 回答