How do I get the position of a window on the screen of an XNA game?

I am making an XNA 4.0 game in visual studio C # 2010 express. In the game, I use winForm for some properties that the user can change. I want the winForm window to display exactly in the main xna game window (for clarity and a better interface). And my question is, how can I get the coordinates of the game window xna? How can I change the coordinates of the winForm window so that the window is displayed exactly in the main game window?

+3


source to share


2 answers


Use this:

using wf = System.Windows.Forms;

wf.Form MyGameForm = (wf.Form)wf.Form.FromHandle(Window.Handle);
var bounds = MyGameForm.Bounds;

      



bounds

will System.Drawing.Rectangle

, you can access its X, Y, Wider and Height.

+3


source


Before setting a new position, you need to set the StartPosition property to FormStartPosition.Manual ...

Here's 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;  

      

0


source







All Articles