启动画面不会将焦点返回到主窗体

我是大家。 使用启动画面时,我目前的焦点有问题。 我使用的是VS2008,带有.NET framework 2.0。 此外,我已将我的项目与VisualBasic.dll链接,因为我使用ApplicationServices来管理我的单实例应用程序和启动画面。

这是一个简化了我尝试调试的代码片段。

namespace MyProject { public class Bootstrap { ///  /// Main entry point of the application. It creates a default /// Configuration bean and then creates and show the MDI /// Container. ///  [STAThread] static void Main(string[] args) { // Creates a new App that manages the Single Instance background work Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); App myApp = new App(); myApp.Run(args); } } public class App : WindowsFormsApplicationBase { public App() : base() { // Make this a single-instance application this.IsSingleInstance = true; this.EnableVisualStyles = true; // There are some other things available in the VB application model, for // instance the shutdown style: this.ShutdownStyle = Microsoft.VisualBasic.ApplicationServices.ShutdownMode.AfterMainFormCloses; // Add StartupNextInstance handler this.StartupNextInstance += new StartupNextInstanceEventHandler(this.SIApp_StartupNextInstance); } protected override void OnCreateSplashScreen() { this.SplashScreen = new MyMainForm(); this.SplashScreen.StartPosition = FormStartPosition.CenterScreen; } protected override void OnCreateMainForm() { // Do your initialization here //... System.Threading.Thread.Sleep(5000); // Test // Then create the main form, the splash screen will automatically close this.MainForm = new Form1(); } ///  /// This is called for additional instances. The application model will call this /// function, and terminate the additional instance when this returns. ///  ///  protected void SIApp_StartupNextInstance(object sender, StartupNextInstanceEventArgs eventArgs) { // Copy the arguments to a string array string[] args = new string[eventArgs.CommandLine.Count]; eventArgs.CommandLine.CopyTo(args, 0); // Create an argument array for the Invoke method object[] parameters = new object[2]; parameters[0] = this.MainForm; parameters[1] = args; // Need to use invoke to b/c this is being called from another thread. this.MainForm.Invoke(new MyMainForm.ProcessParametersDelegate( ((MyMainForm)this.MainForm).ProcessParameters), parameters); } } } 

现在,当我启动应用程序时,Splash Screen会按预期显示,但是当它被销毁时,它不会将焦点返回到主窗体(测试中的Form1)。 MainForm只在任务栏中闪烁橙色。 如果我从IDE(VS2008)启动应用程序,焦点工作正常。 我正在使用XP专业版。 此外,主窗体不在每个其他窗口的顶部。 如果我注释掉OnCreateSplashScreen()方法,应用程序将正常获得焦点。

为了测试正常执行,我使用VS命令提示符来启动我的应用程序。 我正在使用我的项目的发布版本。

有任何想法吗?

编辑:我还处理StartUpNextInstance事件以将命令行参数发送到我的主窗体。 出于测试目的,它已在此处删除。

编辑:添加了更多代码。 现在你有我的引导程序的全部范围。

问题不详细。

1)SplashScreen与应用程序主要forms之间的关系是什么?

2)SplashScreen如何自动关闭?

我确定这里的问题是主要表单已经开始在后台加载,而SplashScreen还没有关闭。 由于时间不好,主窗体在后台加载,SplashScreen卸载…因此任务栏中的闪存。

使它们以串行控制的方式出现。 有很多方法。 由于几乎没有提供任何细节,我无法确切地建议如何。 就像VB在项目中所做的那样,有多少线程正在工作,这里使用的自定义表单是什么……等等。

编辑:

实现启动画面的算法(恕我直言):)

1)创建自定义表单 – 启动画面

2)在单独的线程上运行它。 随心所欲地实现它的行为。

3)在启动画面窗体中,编写处理程序以捕获关闭启动画面窗体的自定义卸载事件处理程序。

4)现在,回到主线程,创建主应用程序表单。 将其Visible属性设置为false。

5)编写主窗体的Load事件的偶数处理程序。 在此处理程序中,触发事件以启动屏幕以卸载。 然后,使主窗体可见。

您需要在主窗体上调用Activate()方法才能正确显示它。

在我在不同线程上的一些启动表单实现中,我也看到了这种情况。

通常如果我称之为this.Activate(); 作为我在表单加载中的第一行代码,它往往按预期工作。 隐藏飞溅后放置这个通常无法正常工作。

其他可以提供帮助的方法。

 this.BringToFront(); this.TopMost = true; //setting this to true and back to false sometimes can help 

如果在Form1 Load事件中明确设置焦点该怎么办?

由于一些不明原因,焦点似乎正常。 我将尝试将我的主程序的更多正常执行结合到MyMainForm中,并尝试找到真正导致焦点更改失败的原因。 我会把你们全部贴出去。

尝试在this.MainForm = new Form1();之后调用form1.show() this.MainForm = new Form1(); 它对我有用。

我遇到了同样的问题,希望能够轻松解决这个问题。

我试过上面但没有运气,但从上面的有用信息的想法。

我尝试了我的技巧,它对我有用。

这是我的Programs.cs代码,工作正常:

 static class Program { ///  /// The main entry point for the application. ///  [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); var mainWindow = new frmMain(); splashScreen.DisplaySplashScreen(mainWindow, 10000); **mainWindow.TopMost = true;** Application.Run(mainWindow); } } 

注意mainWindow.TopMost = true;

好的,但是我不想让mainWindow保持领先。

所以在Shown事件中我添加了this.TopMost = false;

  private void frmMain_Shown(object sender, EventArgs e) { this.TopMost = false; } 

现在我的应用程序正在运行,因为我希望它能够工作。

注意:刚刚发布,以帮助面临同样问题的任何其他用户。

快乐编码:)