41

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 sample will be appreciated.

Ani
  • 111,048
  • 26
  • 262
  • 307
Farid-ur-Rahman
  • 1,809
  • 9
  • 31
  • 47

11 Answers11

53

Use Control.PointToClient to convert a point from screen-relative coords to control-relative coords. If you need to go the other way, use PointToScreen.

BrendanMcK
  • 14,252
  • 45
  • 54
40

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;      
} 
Ani
  • 111,048
  • 26
  • 262
  • 307
24

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);
Justas
  • 5,718
  • 2
  • 34
  • 36
5

One can use following methods for getting the relative from absolute and absolute from relative coordinates:

Point Control.PointToClient(Point point);

Point Control.PointToScreen(Point point);
TLama
  • 75,147
  • 17
  • 214
  • 392
Chris
  • 51
  • 1
  • 2
4
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
    Text = panel1.PointToClient(Cursor.Position).ToString();    
} 
3

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.

ndukan
  • 64
  • 2
2

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

Simply subtract from the cursor position the Left and Top coordinates of the control:

this.Text = Convert.ToString(
    Cursor.Position.X - this.Left + ":" +
    Cursor.Position.Y - this.Top);
BlackBear
  • 22,411
  • 10
  • 48
  • 86
  • 4
    May work for top-level Forms, but won't work for controls within one: Control.Left is the control's position relative to its *parent's client area*, not the screen. – BrendanMcK Nov 20 '11 at 12:40
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);
}

Where "lienzo" is my canvas panel

bensiu
  • 24,660
  • 56
  • 77
  • 117
Pichitron
  • 159
  • 5
0

Snippet code as following:

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

Where the button is one of many buttons in a hosting panel.

Tomex Ou
  • 79
  • 1
  • 2
0

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.

JanBorup
  • 5,337
  • 1
  • 29
  • 17