Skip to main content
GameDev.net gamedev.net
🔒 Locked

mouse coordinats in game-window

Started by net777 Mar 12, 2014 at 9:40 AM 1 replies 1.4k views
Original Post
net777
net777

Hi! I have a winform and a picturebox where my game is.

I want to get the mouse-coordinats for only the game-window and not the entire window.

How can I do this?

Zedmonkeyboy
Zedmonkeyboy

What language are you using?

Edit-

Ok after looking at a one of your past topics I figure that you're using c#.

After some research I found this it may help you. wink.png

ta0soft
ta0soft

There's 2 ways you can accomplish this.

You can use the Control.PointToClient() function, this will convert the screen coordinates that you specify into client coordinates.

For example if your PictureBox is placed at X=10, Y=10 inside the Form, and the mouse location is X=20, Y=20, myPictureBox.PointToClient(new Point(20, 20)) will return X=10, Y=10.

Another option is by computing the coordinates yourself. This is basically what PointToClient() does behind the scenes.

Point clientLocation = new Point(mousePosition.X - myPictureBox.Left, mousePosition.Y - myPictureBox.Top);

You can also use the the built-in mouse event handlers for controls (MouseDown, MouseMove, MouseUp, MouseLeave, MouseWheel). These events will automatically convert the mouse position to client coordinates, so you don't have to convert them yourself.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.