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

Oly*_*yan 8 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);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

有任何想法吗?

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

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

Nay*_*yan 4

问题不详细。

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

2)SplashScreen如何自动关闭?

我确信这里的问题是主窗体已经开始在后台加载,而 SplashScreen 尚未关闭。由于时机不好,主窗体在后台加载,而 SplashScreen 卸载......因此任务栏中会闪烁。

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

编辑:

实现闪屏的算法(恕我直言):)

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

2)在单独的线程上运行它。按照您的喜好实现它的行为。

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

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

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