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!! :)

Dani AI

Generated

If the goal is simply "0,0 is the PictureBox top-left," let the PictureBox give you mouse coordinates directly. WinForms already reports MouseEventArgs.Location in the control's client space, so you avoid timers and screen-to-client math entirely. Building on 's offset idea, wire the event on the PictureBox instead of the Form:

pbGameBox.MouseMove += (s, e) =>
{
    // e.Location is already relative to pbGameBox (0,0 = top-left of the PictureBox)
    var p = e.Location;

    // Example: show in title or use p.X/p.Y in your editor logic
    this.Text = $"{p.X}, {p.Y}";
};

If you really do need to poll from somewhere else (e.g., a game loop) rather than react to events, convert the screen cursor to PictureBox client coordinates and clamp:

Point p = pbGameBox.PointToClient(Cursor.Position);
bool inside = pbGameBox.ClientRectangle.Contains(p);
int x = inside ? p.X : -1;
int y = inside ? p.Y : -1;
// use x,y; -1 indicates out of bounds

One more gotcha that often bites map editors: scaling. If what you draw is letterboxed or scaled inside the PictureBox (e.g., virtual 1024x768 backbuffer rendered into a larger/smaller control), convert PictureBox coords to game-space by undoing the scale and offset:

int gameW = 1024, gameH = 768;
var pb = pbGameBox.ClientSize;

float scale = Math.Min(pb.Width / (float)gameW, pb.Height / (float)gameH);
float viewW = gameW * scale, viewH = gameH * scale;
float offsetX = (pb.Width - viewW) * 0.5f;
float offsetY = (pb.Height - viewH) * 0.5f;

Point p = pbGameBox.PointToClient(Cursor.Position);
int gx = (int)Math.Floor((p.X - offsetX) / scale);
int gy = (int)Math.Floor((p.Y - offsetY) / scale);
// check 0 <= gx < gameW and 0 <= gy < gameH before using

That gives you stable, top-left-origin coordinates within the PictureBox, and correct game-space positions if the content is scaled.

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.