【问题标题】:Refocus main window after closing OpenFileDialog in Monogame在 Monogame 中关闭 OpenFileDialog 后重新聚焦主窗口
【发布时间】:2018-08-15 20:46:53
【问题描述】:

我让用户使用 OpenFileDialog 选择一个文件。选择文件并关闭对话框后,主窗口是 alt-tabbing 时的最后一个应用程序。我想让主窗口重新获得焦点并处于前台,而不必在关闭对话框窗口后按 alt+tab 键。这可能吗?如果可以,怎么办?

public class Game1 : Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    Class1 c1;

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
    }

    protected override void Initialize()
    {
        c1 = c1.Instance;
        base.Initialize();
    }

    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);
        c1.Load(Content);

        c1.GetFile();
    }
}


public sealed class Class1
{
    private static Class1 _instance = null;
    private static readonly object _padlock = new object();

    private Class1()
    {
    }

    public static Class1 Instance
    {
        get
        {
            lock (_padlock)
            {
                if (_instance == null)
                {
                    _instance = new Class1();
                }
                return _instance;
            }
        }
    }

    public void GetFile()
    {
        string path;
        OpenFileDialog ofdSelectLayout = new OpenFileDialog();
        if(ofdSelectLayout.ShowDialog() == DialogResult.OK)
        {
            path = ofdSelectLayout.FileName;
        }
        //some code
    }

    public void Load(ContentManager content)
    {
        //Loading some textures
    }
}

【问题讨论】:

    标签: c# monogame openfiledialog


    【解决方案1】:

    我认为会有一个简单的方法。
    (如Game.IsActive,仅检查游戏是否失焦)

    一段时间后,我找到了这个,你可以将它放在FileDialog正在关闭的方法中:

    using System.Windows.Forms;
    
    Form myForm = (Form)Form.FromHandle(this.Window.Handle);
    myForm.Activate();
    

    不过,这确实使用了System.Windows.Forms,这意味着您的游戏必须是 Windows 独有的。但是,如果您正在使用 Windows 窗体,您现在可以将其视为一种解决方法。

    【讨论】:

      猜你喜欢
      • 2011-08-04
      • 1970-01-01
      • 1970-01-01
      • 2020-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-05
      相关资源
      最近更新 更多