views:

146

answers:

2

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);
        }
    }  
}
+1  A: 

You can get cursor coordinates using Cursor.Position, this gets you screen coordinates. you can then pass these into PointToClient(Point p)

Point screenCoords = Cursor.Position;
Point controlRelatedCoords = this.panel1.PointToClient(screenCoords);

Though, I am fairly certain that DragEventArgs.X and DragEventArgs.Y are already screen coordinates. Your problem probably lies in

 if (e.X < 429 && e.X > 0 && e.Y<430 && e.Y>0)

This looks like it would be checking against Panel coordinates, whereas e.X and e.Y are screen coordinates at that point. Instead, thansform it into panel coords before checking against bounds :

 Point screenCoords = Cursor.Position;
 Point controlRelatedCoords = this.panel1.PointToClient(screenCoords);
 if (controlRelatedCoords.X < 429 && controlRelatedCoords.X > 0 && 
     controlRelatedCoords.Y < 430 && controlRelatedCoords.Y > 0)
 {

 }
Dynami Le Savard
But in that case there are situations where a part of the image that have dragged may be not visible at border of panel.Because cursor can be pointed anywhere in the panel.Eg:when cursor pointed to almost near to the border.That's why I am checking the dragged point against the panel border.Is there any solution?
C. Karunarathne
A: 

Thank you for your response. I got the point. Further I want to check whether images are overlapping when I have dragged. How can I achieve that.Thanks

C. Karunarathne
How can I check cursor coordinates lie in another control? Is there any clue?Please.Thank you
C. Karunarathne