So I'm making a map editor with C# and XNA. I have currently got it setup so there is a windows for with a picture box, which the game is being displayed through. The problem is this, I want (0,0) to be the upper left corner of the picture box, not the game window.

At the moment im trying to return the cursor position but its relative to somewhere in the upper-middle portion of the window...the X-coordinates will go negative if the cursor is in the left third of the screen, and the Y-coordinetes will go negative if the cursor is in an upper third of the screen.

So again, how can I set the upper left corner of the pictureBox - which contains the game itself - as coordinates (0,0) of the mouse?

I'll post any code that may be required to help!! Thanks in advance!! :)

I know this is a bit late and you may have come up with a solution already but here is a method you could use.

pbGameBox is a PictureBox object from the C# Toolbox.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace dani_mapEditor
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        void onTick(object sender, EventArgs e)
        {
            Point P = PointToScreen(new Point(pbGameBox.Bounds.Left, pbGameBox.Bounds.Top));
            Int32 X = Cursor.Position.X - P.X;
            Int32 Y = Cursor.Position.Y - P.Y;

            if (X < 0 || Y < 0 || X > pbGameBox.Bounds.Width || Y > pbGameBox.Bounds.Height)
                this.Text = "--, --";
            else
                this.Text = String.Format("{0}, {1}", X, Y);

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Timer aTimer = new Timer();
            aTimer.Interval = 10;
            aTimer.Tick += new EventHandler(onTick);
            aTimer.Enabled = true;

            GC.KeepAlive(aTimer);



        }
    }
}

All this does is updates the title of the window (with the use of a timer) to the X, Y coordinates of the mouse within the PictureBox. If the mouse is out of bounds of the PictureBox then it just displays "--, --".

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.