Get cursor position with respect to the control - C#

前端 未结 11 2338
迷失自我
迷失自我 2020-12-05 23:23

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

相关标签:
11条回答
  • 2020-12-05 23:30

    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;      
    } 
    
    0 讨论(0)
  • 2020-12-05 23:30
    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);
    }
    

    Where "lienzo" is my canvas panel

    0 讨论(0)
  • 2020-12-05 23:31

    Create af standard Project C# WinForms

    Place 2 Textboxes named X and Y, and a Timer object from the toolbox to the Design page

    Press [F7] and replace all code with below.

    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();
            }
        }
    }
    

    Set Timer.Tick action to "Form1_MouseCaptureChanged"

    [F5] Run - and now you have an MosusePos app.

    0 讨论(0)
  • 2020-12-05 23:41

    The following will give you mouse coordinates relative to your control. For example, this results in (0,0) if mouse is over top left corner of the control:

    var coordinates = yourControl.PointToClient(Cursor.Position);
    
    0 讨论(0)
  • 2020-12-05 23:42

    I use MouseLocation and PointToClient to check. And then use it in a timer!

    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;
            }
    
    0 讨论(0)
  • 2020-12-05 23:46

    Cursor.Position return Point on Screen, but Control.PointToClient(Cursor.Position) returns point on control (e.g. control -> panel). In your case, you have e.Locate which return point on control.

    0 讨论(0)
提交回复
热议问题