I am implementing an application which can be drag and drop images in a panel and so I want to make sure that the image is placed within the panel and it is visible the whole image when is dropped.In that case I want to get the current cursor position when I doing drag and drop event. So how can I get the cursor location related to panel?
Here is the method of panel dragdrop event.
private void panel1_DragDrop(object sender, DragEventArgs e)
{
Control c = e.Data.GetData(e.Data.GetFormats()[0]) as Control;
if (c != null)
{
if (e.X < 429 && e.X > 0 && e.Y<430 && e.Y>0)
{
c.Location = this.panel1.PointToClient((new Point(e.X, e.Y)));**
this.panel1.Controls.Add(c);
}
}
}
You can get cursor coordinates using
Cursor.Position, this gets you screen coordinates. you can then pass these intoPointToClient(Point p)Though, I am fairly certain that
DragEventArgs.XandDragEventArgs.Yare already screen coordinates. Your problem probably lies inThis looks like it would be checking against Panel coordinates, whereas
e.Xande.Yare screen coordinates at that point. Instead, thansform it into panel coords before checking against bounds :