如何在XNA游戏中获取窗口在屏幕上的位置?

3

我正在使用Visual Studio C# 2010 Express开发一个基于XNA 4.0的游戏。在游戏中,我使用了一个winForm来让用户更改一些属性。我希望这个winForm窗口能够恰好出现在主XNA游戏窗口的右边,以便增加清晰度和优化界面。 我的问题是,如何获取XNA游戏窗口的坐标? 我该如何更改winForm窗口的坐标,以便窗口出现在主游戏窗口的右边?


你想返回鼠标点击的位置,还是你正在寻找哪个位置?相对于其他操作系统,您要返回XNA窗口的位置吗? - sec_goat
欢迎来到StackOverflow,在这里,通过点击答案旁边的“v”标志来选择被接受的答案 :) - user1306322
2个回答

3
请使用以下内容:
using wf = System.Windows.Forms;

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

bounds 将会是一个 System.Drawing.Rectangle,你可以访问它的 X、Y、Width 和 Height 属性。


0

在设置新位置之前,您需要将StartPosition属性设置为FormStartPosition.Manual。

这里是一个详细的示例:

  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;  

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接