【发布时间】:2010-10-07 17:27:07
【问题描述】:
我每个人。使用闪屏时,我目前的注意力有问题。我正在使用带有 .NET 框架 2.0 的 VS2008。此外,我已将我的项目与 VisualBasic.dll 链接起来,因为我使用 ApplicationServices 来管理我的单实例应用程序和启动屏幕。
这是我尝试调试的简化代码 sn-p。
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 Pro。此外,主窗体不在所有其他窗口之上。如果我注释掉 OnCreateSplashScreen() 方法,应用程序会正常获得焦点。
为了测试正常执行,我使用 VS 命令提示符来启动我的应用程序。我正在使用我的项目的 Release 版本。
有什么想法吗?
编辑: 我还处理 StartUpNextInstance 事件以将我的命令行参数发送到我的主窗体。出于测试目的,此处已将其删除。
编辑:添加了更多代码。现在你已经完全掌握了我的引导程序。
【问题讨论】:
-
这是我的代码。 SO 非常重视这一点,需要注明出处。您必须包含指向原始代码的链接和作者姓名。
-
我很久以前(我认为是几个月前)使用了这段代码,并且只添加了 SplashScreen。对不起,如果它是你的开始。如何添加您的署名和链接?
标签: c# focus splash-screen