user2602079
user2602079

Reputation: 1393

Make DragEventArgs e.X and e.Y relative to Form or Picturebox - NOT the screen

In a method that gets called by the DragDrop event handler (so i pass it DragEventArgs - which i use to get mouse.X and mouse.Y) it is basing it's e.Location relative to the computer screen. This is not working out for my app. I have other similar methods, and the e.X and e.Y is relative to my Picturebox, which is preferable. Even relative to the form would be okay. Why is this relative to the screen when the others were relative to the picturebox? Here is the method:

 public void setMinotaur(DragEventArgs e, List<Cell> cells)
        {
            for (int i = 0; i < cells.Count; i++)
            {
                int[] mapData = myMapController.getMapData(i, cells);
                int column = mapData[0];
                int row = mapData[1];
                int right = mapData[2];
                int bottom = mapData[3];



              //  int column = myMap.boardXPos + myMap.myCells[i].myColumn * myMap.myCellSize;
              //  int row = myMap.boardYPos + myMap.myCells[i].myRow * myMap.myCellSize;


                int pbxLocationX = myMapController.myMap.myForm.pbxMap.Location.X;
                int pbxLocationY = myMapController.myMap.myForm.pbxMap.Location.Y;
                int pnlLocationX = myMapController.myMap.myForm.panel2.Location.X;
                int pnlLocationY = myMapController.myMap.myForm.panel2.Location.Y;
                int offsetX = pbxLocationX + pnlLocationX;
                myMapController.myMap.myForm.label1.Text = offsetX.ToString();
                myMapController.myMap.myForm.label2.Text = e.X.ToString();
                int offsetY = pbxLocationY + pnlLocationY;

                if (e.X > column + offsetX &&
                    e.X < column + myMapController.myMap.myCellSize + offsetX)
                {
                    myMapController.myMap.myCells[i].hasMinotaur = true;
                }
                else
                {
                    myMapController.myMap.myCells[i].hasMinotaur = false;
                }
                myMapController.myMap.myForm.label2.Text = e.X.ToString();
            }
        }

It is called here:

private void pbxMap_DragDrop(object sender, DragEventArgs e)
{
    myDetectMouse.setMinotaur(e, myMap.myCells);
}

Upvotes: 1

Views: 751

Answers (1)

Nicolas Tyler
Nicolas Tyler

Reputation: 10552

just use point to client:

PointToClient(new Point(x,y));

Here is an example:

Point RelativeMouseLoc = PointToClient(Cursor.Position);

Upvotes: 2

Related Questions