大家好。我在使用启动屏幕时遇到了聚焦问题。我正在使用VS2008,.NET框架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)。在任务栏中,主窗体只是闪烁橙色。如果我从IDE(VS2008)启动应用程序,则焦点运作正常。我正在使用XP Pro。此外,主窗体不在所有其他窗口的顶部。如果注释掉OnCreateSplashScreen()方法,则应用程序可以正常获取焦点。
为了测试正常执行,我使用VS命令提示符启动我的应用程序,我使用项目的Release版本。
有任何想法吗?
编辑:我还处理StartUpNextInstance事件,以将我的命令行参数发送到我的主窗体。出于测试目的,在这里删除了它。
编辑:添加了更多代码。现在你已经知道了我引导的全部范围。