【问题标题】:Get cursor position with respect to the control - C#获取相对于控件的光标位置 - C#
【发布时间】:2011-11-20 11:49:35
【问题描述】:

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

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

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

代码示例将不胜感激。

【问题讨论】:

    标签: c# winforms mouse mouse-position


    【解决方案1】:

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

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        【解决方案3】:

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

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

        【讨论】:

          【解决方案4】:

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

          Point Control.PointToClient(Point point);
          
          Point Control.PointToScreen(Point point);
          

          【讨论】:

            【解决方案5】:

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

            【讨论】:

              【解决方案6】:
              private void panel1_MouseMove(object sender, MouseEventArgs e)
              {
                  Text = panel1.PointToClient(Cursor.Position).ToString();    
              } 
              

              【讨论】:

                【解决方案7】:

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

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

                【讨论】:

                • 可能适用于顶级窗体,但不适用于其中的控件:Control.Left 是控件相对于其父客户区的位置,而不是屏幕.
                【解决方案8】:

                我使用 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;
                        }
                

                【讨论】:

                  【解决方案9】:
                  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”是我的画布面板

                  【讨论】:

                    【解决方案10】:

                    片段代码如下:

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

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

                    【讨论】:

                      【解决方案11】:

                      创建标准项目 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 应用程序。

                      【讨论】:

                        猜你喜欢
                        • 2011-01-18
                        • 1970-01-01
                        • 2021-02-15
                        • 1970-01-01
                        • 1970-01-01
                        • 1970-01-01
                        • 2020-06-24
                        • 2023-02-12
                        • 1970-01-01
                        相关资源
                        最近更新 更多