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
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.
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
Text = panel1.PointToClient(Cursor.Position).ToString();
}
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);
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);
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.