C# 在Monogame中关闭OpenFileDialog后重新聚焦主窗口
我让用户使用OpenFileDialog选择一个文件。选择文件并关闭对话框后,alt制表时,主窗口是最后一个应用程序。我想让主窗口重新获得焦点,并且在关闭对话框窗口后不必按住alt+tab键就可以在前景上。这可能吗?如果可能,如何实现C# 在Monogame中关闭OpenFileDialog后重新聚焦主窗口,c#,monogame,openfiledialog,C#,Monogame,Openfiledialog,我让用户使用OpenFileDialog选择一个文件。选择文件并关闭对话框后,alt制表时,主窗口是最后一个应用程序。我想让主窗口重新获得焦点,并且在关闭对话框窗口后不必按住alt+tab键就可以在前景上。这可能吗?如果可能,如何实现 public class Game1 : Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; Class1 c1; public Game1()
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
}
}
我想会有一个简单的方法
(如
Game.IsActive
,它只检查游戏是否不对焦)
过了一会儿,我找到了这个,您可以将它放在FileDialog关闭的方法中:
using System.Windows.Forms;
Form myForm = (Form)Form.FromHandle(this.Window.Handle);
myForm.Activate();
然而,这确实使用了System.Windows.Forms
,这意味着您的游戏必须是Windows独占的。但是,如果您在Windows窗体中工作,您现在可以将其视为一种解决方法