locked
Mouse Down /Drag Event Help RRS feed

  • Question

  • I am trying to drag an marker to an different location on the screen. At the moment before I start the drag event, the marker goes to the exact pointer location on the screen and then it starts dragging. That's the unexpected behavior.


    The expected behavior us to click the marker and then drag. Can someone assist me?

     private void myMap_MouseDown(object sender, MouseEventArgs e)
            {

     if (e.Button == MouseButtons.Left)
                {

                if (marker.IsVisible)
                {
                    marker.Position = myMap.FromLocalToLatLng(e.X, e.Y);

                    var px = myMap.MapProvider.Projection.FromLatLngToPixel(marker.Position.Lat, marker.Position.Lng, (int)myMap.Zoom);
                    var tile = myMap.MapProvider.Projection.FromPixelToTileXY(px);

                }

    }

    }

     private void myMap_MouseUp(object sender, MouseEventArgs e)
            {
                if (e.Button == MouseButtons.Left)
                {
                    IsMouseDown = false;

                }

    }

     internal void MouseMove(bool IsMouseDown, int x, int y)
            {
                myForm.Invoke((Action)(() =>
                {
                    if (marker == null)
                    {
                        if (marker.IsVisible)
                        {
                            marker.Position = myMap.FromLocalToLatLng(x, y);
                        }
                    }
                    else
                    {
                        PointLatLng p= myMap.FromLocalToLatLng(x, y);
                     
                        if (marker.IsVisible)
                        {
                            marker.Position = p;
                        }
                    }

       
                }));
            }


    pianoboyCoder

    Friday, July 15, 2016 1:52 AM

Answers

  • Don't change the marker position in the MouseDown.

    Instead, work out the difference between the current marker position and the clicked position. Then when you get a MouseMove, set the marker position to the new mouse position minus that difference.

    • Marked as answer by DotNet Wang Tuesday, July 26, 2016 1:57 AM
    Friday, July 15, 2016 7:00 AM
  • Could you give me an example if there is one out. I am new to the mouse events.

    pianoboyCoder

    Hi,

    Welcome to MSDN forum.

    Here is an example how to detect mouse button event.  I hope the reply would be helpful to you.

    If yes, please mark it. If no, please unmark it.

    Best Regards,

    Hart

    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    • Proposed as answer by Hart Wang Tuesday, July 19, 2016 9:32 AM
    • Marked as answer by DotNet Wang Tuesday, July 26, 2016 1:57 AM
    Tuesday, July 19, 2016 9:31 AM

All replies

  • Don't change the marker position in the MouseDown.

    Instead, work out the difference between the current marker position and the clicked position. Then when you get a MouseMove, set the marker position to the new mouse position minus that difference.

    • Marked as answer by DotNet Wang Tuesday, July 26, 2016 1:57 AM
    Friday, July 15, 2016 7:00 AM
  • Could you give me an example if there is one out. I am new to the mouse events.

    pianoboyCoder

    Friday, July 15, 2016 7:02 AM
  • Could you give me an example if there is one out. I am new to the mouse events.

    pianoboyCoder

    Hi,

    Welcome to MSDN forum.

    Here is an example how to detect mouse button event.  I hope the reply would be helpful to you.

    If yes, please mark it. If no, please unmark it.

    Best Regards,

    Hart

    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; Therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    • Proposed as answer by Hart Wang Tuesday, July 19, 2016 9:32 AM
    • Marked as answer by DotNet Wang Tuesday, July 26, 2016 1:57 AM
    Tuesday, July 19, 2016 9:31 AM