【问题标题】:How to get the window location on the screen of an XNA game?如何获取 XNA 游戏屏幕上的窗口位置?
【发布时间】:2013-01-10 13:53:57
【问题描述】:

我正在 Visual Studio C# 2010 Express 中制作 XNA 4.0 游戏。在游戏中,我将 winForm 用于用户可以更改的某些属性。我希望 winForm 窗口正好出现在 xna 主游戏窗口的右侧(为了清晰和更好的界面)。 我的问题是,我怎样才能得到 xna 游戏窗口的坐标? 如何更改 winForm 窗口的坐标,使窗口恰好出现在主游戏窗口的右侧?

【问题讨论】:

  • 你想返回鼠标点击的位置,还是你在找什么位置? xna 窗口相对于操作系统其余部分的位置?
  • 欢迎来到 StackOverflow,通过按旁边的 v 符号来选择接受的答案 :)

标签: c# winforms xna window location


【解决方案1】:

使用这个:

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、宽度和高度。

【讨论】:

    【解决方案2】:

    在设置新位置之前,您需要将 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;  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-17
      • 2013-01-09
      • 1970-01-01
      • 1970-01-01
      • 2013-05-23
      • 2014-12-10
      相关资源
      最近更新 更多