获取光标相对于控件-C#的位置

本文关键字:位置 -C# 控件 光标 相对于 获取 | 更新日期: 2023-09-27 18:15:17

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

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

但是,这使得相对于屏幕的位置不属于控件。

代码示例将不胜感激。

获取光标相对于控件-C#的位置

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

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

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

下面将给出鼠标相对于控件的坐标。例如,如果鼠标位于控件的左上角,则会导致(0,0(:

var coordinates = yourControl.PointToClient(Cursor.Position);

可以使用以下方法从绝对坐标获得相对坐标,从相对坐标获得绝对坐标:

Point Control.PointToClient(Point point);
Point Control.PointToScreen(Point point);
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
    Text = panel1.PointToClient(Cursor.Position).ToString();    
} 

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

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

this.Text = Convert.ToString(
    Cursor.Position.X - this.Left + ":" +
    Cursor.Position.Y - this.Top);

我使用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;
        }
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"是我的画布面板

代码段如下:

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
    }
}

其中,该按钮是主机面板中的众多按钮之一。

创建标准项目C#WinForms

将两个名为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应用程序。