I am making an XNA 4.0 game in visual studio c# 2010 express. In the game i am using a winForm for some properties that the user can change. I want the winForm window to appear exactly right to the main xna game window (for clarity and better interface). And my question is, how can i get the coordinates of the xna game window? How can i change the coordinates of the winForm window so the window appears exactly right of the main game window?
Asked
Active
Viewed 1,633 times
3
-
Do you want to return the position of where the mouse is clicked, or what position are you looking for? The position of the xna window relative to the rest of the OS? – sec_goat Jan 26 '13 at 21:04
-
Welcome to StackOverflow, where accepted answers are selected by pressing the `v` sign next to them :) – user1306322 Jan 26 '13 at 21:50
2 Answers
3
Use this:
using wf = System.Windows.Forms;
wf.Form MyGameForm = (wf.Form)wf.Form.FromHandle(Window.Handle);
var bounds = MyGameForm.Bounds;
bounds
will be a System.Drawing.Rectangle
, you can access its X, Y, Width and Height.

user1306322
- 8,561
- 18
- 61
- 122
0
You need to set the StartPosition property to FormStartPosition.Manual before setting the new position...
Here you are a detailed example:
var sc = System.Windows.Forms.Screen.AllScreens;
var xnaForm = (Form) Forms.Control.FromHandle( Editor.Game.Window.Handle );
// Set the xnaForm position at the desired screen (for multimonitor systems)
xnaForm.StartPosition = Forms.FormStartPosition.Manual;
xnaForm.Location = new Point(
sc[pref.AdapterIndex].Bounds.Left,
sc[pref.AdapterIndex].Bounds.Top);
int W = sc[pref.AdapterIndex].WorkingArea.Width;
int H = sc[pref.AdapterIndex].WorkingArea.Height;
W -= Editor.Game.Window.ClientBounds.Width;
// Set the editor form position to the right of xnaForm
MapForm.StartPosition = Forms.FormStartPosition.Manual;
MapForm.DesktopLocation = new System.Drawing.Point(
xnaForm.DesktopBounds.Right,
xnaForm.DesktopBounds.Top);
MapForm.Width = W;

Blau
- 5,742
- 1
- 18
- 27