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

标签 c# focus splash-screen

我是大家。我目前在使用启动画面时注意力不集中。我正在使用 VS2008 和 .NET Framework 2.0。此外,我已将我的项目与 VisualBasic.dll 链接起来,因为我使用 ApplicationServices 来管理我的单实例应用程序和启动画面。

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

namespace MyProject
{
    public class Bootstrap
    {
        /// <summary>
        /// Main entry point of the application. It creates a default 
        /// Configuration bean and then creates and show the MDI
        /// Container.
        /// </summary>
        [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();
        }
        /// <summary>
        /// This is called for additional instances. The application model will call this 
        /// function, and terminate the additional instance when this returns.
        /// </summary>
        /// <param name="eventArgs"></param>
        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);
        }
    }
}

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

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

有什么想法吗?

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

编辑:添加了更多代码。现在您已经完全了解了我的 Bootstrap 。

最佳答案

问题不详细。

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

2)SplashScreen如何自动关闭?

我确定这里的问题是主窗体已经开始在后台加载,而 SplashScreen 尚未关闭。由于时机不对,主窗体在后台加载,SplashScreen 卸载...因此任务栏中出现了 flash。

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

编辑:

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

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

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

3) 在您的初始屏幕表单中,编写一个处理程序来捕获关闭初始屏幕表单的自定义卸载事件处理程序。

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

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

关于c# - 启动画面不会将焦点返回到主窗体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3884200/

相关文章:

java - 仅在从启动器启动应用程序时显示启动画面

c# - ASP.NET:如何创建可扩展的空 TreeNode

c# - 如何在本地控制台上使用 C# SSH.NET 执行的远程主机 (Raspberry Pi) 上运行的 Python 程序连续写入输出?

javascript - body 对焦不起作用

javascript - 如何强制关注第一个无效输入?

android - 如何减少在冷启动时启动应用程序所需的时间

C#内存不足异常-警告策略

c# - 如何将 List<> 绑定(bind)到 ListView 中的列

css - CSS outline-color 属性在 firefox 中不起作用吗?例如: input:focus {outline-color:#9aadee; }

android - 当我尝试通过线程启动另一个 Intent 时,我的初始屏幕背景没有显示