初始屏幕不会将焦点返回到主窗体 [英] Splash screen does not return focus to main form

查看:159
本文介绍了初始屏幕不会将焦点返回到主窗体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我每个人。使用启动画面时,我目前遇到问题。我正在使用VS2008,与.NET框架2.0。另外,由于我使用ApplicationServices来管理我的单一实例应用程序和启动画面,因此我已经将项目与VisualBasic.dll链接在一起。



以下是简化的代码片段

  namespace MyProject 
{
public class Bootstrap
{
///< summary>
///应用程序的主要入口点。它创建一个默认的
///配置bean,然后创建并显示MDI
/// Container。
///< / summary>
[STAThread]
static void Main(string [] args)
{
//创建一个管理单实例后台工作的新应用
Application.EnableVisualStyles );
Application.SetCompatibleTextRenderingDefault(false);
App myApp = new App();
myApp.Run(args);


$ b $ public class App:WindowsFormsApplicationBase
{
public App()
:base()
{
//将其设为单实例应用程序
this.IsSingleInstance = true;
this.EnableVisualStyles = true;

//在VB应用程序模型中还有一些其他的东西可用:
//实例的关闭风格:
this.ShutdownStyle = Microsoft.VisualBasic.ApplicationServices.ShutdownMode。 AfterMainFormCloses;

//添加StartupNextInstance处理程序
this.StartupNextInstance + = new StartupNextInstanceEventHandler(this.SIApp_StartupNextInstance);


protected override void OnCreateSplashScreen()
{
this.SplashScreen = new MyMainForm();
this.SplashScreen.StartPosition = FormStartPosition.CenterScreen;

protected override void OnCreateMainForm()
{
//在这里进行初始化
// ...
System.Threading.Thread.Sleep (5000); //测试
//然后创建主窗体,初始屏幕会自动关闭
this.MainForm = new Form1();
}
///< summary>
///这被称为额外的实例。应用程序模型将调用
///函数,并在返回时终止附加实例。
///< / summary>
///< param name =eventArgs>< / param>
protected void SIApp_StartupNextInstance(object sender,
StartupNextInstanceEventArgs eventArgs)
{
//将参数复制到字符串数组
string [] args = new string [eventArgs。 CommandLine.Count];
eventArgs.CommandLine.CopyTo(args,0);

//为Invoke方法创建一个参数数组
object [] parameters = new object [2];
参数[0] = this.MainForm;
参数[1] = args;

//需要使用invoke来从另一个线程调用b / c。
this.MainForm.Invoke(new MyMainForm.ProcessParametersDelegate(
((MyMainForm)this.MainForm).ProcessParameters),
parameters);





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



为了测试正常执行,我使用VS命令提示符来启动我的应用程序。

任何想法?



编辑:
我是使用我的项目的发行版本。还处理StartUpNextInstance事件发送我的命令行参数到我的主窗体。出于测试目的,它在这里被删除。


编辑:增加了一些代码。现在你已经完全引导了我的引导了。

解决方案


1)SplashScreen和应用程序的主要形式之间有什么关系?2)SplashScreen如何自动关闭?

p>

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



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



编辑:



实现启动画面(IMHO)的算法:)
$ b $ 1)创建自定义表单 - 启动画面

2)在单独的线程上运行它。 3)在你的启动画面窗体中,编写一个处理程序来捕获一个自定义的卸载事件处理程序,它关闭了闪屏窗体。

>

4)现在回到主线程,创建主应用程序窗体。设置它的 Visible 属性为false。


$ b $ 5写主表单的加载事件。在这个处理程序中,触发一个事件来启动屏幕卸载。然后,使主窗体可见。


I everyone. I currently have a problem with my focus when using a splash screen. I am using VS2008, with .NET framework 2.0. Also, I have linked my project with the VisualBasic.dll since I use the ApplicationServices to manage my single instance app and splash screen.

Here is a code snippet simplified of what I tried debugging.

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);
        }
    }
}

Now, what happens is that, when I start the application, the Splash Screen shows as expected, but when it is destroyed, it does not return the focus to the main form (Form1 in the test). The MainForm simply flashes orange in the taskbar. If I launch the application from the IDE (VS2008), focus works just fine. I am using XP Pro. Also, the main form is not on top of every other windows. If I comment out the OnCreateSplashScreen() method, the application gets focus normally.

To test normal execution, I am using the VS Command Prompt to launch my application. I am using the Release version of my project.

Any ideas?

Edit: I also handle the StartUpNextInstance event to send my command-line arguments to my main form. For test purposes, it was removed here.

Edit: Added a bit more code. Now you have the full extent of my bootstrap.

解决方案

The question is not in detail.

1) What the is the relationship between the SplashScreen and the main form of the application?

2) How does SplashScreen automatically close?

I'm sure the problem here is that the main form had already started loading up in the background while SplashScreen is yet to close. Due to bad timing, the main form loads up in the background and the SplashScreen unloads... hence the flash in the taskbar.

Make them appear in serial controlled manner. There are many ways. I cannot suggest exactly how since hardly any detail has been provided. Like what is VB doing in the project, how many threads are working, what are the custom forms used here.. etc.

EDIT:

Algorithm to implement Splash screen (IMHO) :)

1) Create a custom form - splash screen

2) Run it on a separate thread. Implement it's behaviour as you like.

3) In your splash screen form, write a handler to capture a custom unload event handler which closes the splash screen form.

4) Now, back in the main thread, create you main app form. Set its Visible property to false.

5) Write even handler of the main form's Load event. In this handler, fire an event to splash screen to unload. Then, make the main form visible.

这篇关于初始屏幕不会将焦点返回到主窗体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆