Yo a todos. Actualmente tengo un problema con mi enfoque cuando uso una pantalla de inicio. Estoy usando VS2008, con .NET framework 2.0. Además, he vinculado mi proyecto con VisualBasic.dll ya que uso ApplicationServices para administrar mi aplicación de instancia única y la pantalla de bienvenida.La pantalla de bienvenida no devuelve el foco al formulario principal
Aquí hay un fragmento de código simplificado de lo que probé para depurar.
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);
}
}
}
Ahora, lo que sucede es que, cuando comienzo a la aplicación, la pantalla de bienvenida muestra como se esperaba, pero cuando es destruido, no devuelve el foco al formulario principal (Form1 en la prueba). MainForm simplemente parpadea en naranja en la barra de tareas. Si ejecuto la aplicación desde el IDE (VS2008), el enfoque funciona bien. Estoy usando XP Pro. Además, el formulario principal no se encuentra en la parte superior de las demás ventanas. Si hago un comentario sobre el método OnCreateSplashScreen(), la aplicación obtiene el foco normalmente.
Para probar la ejecución normal, estoy usando el símbolo del sistema VS para iniciar mi aplicación. Estoy usando la versión de lanzamiento de mi proyecto.
¿Alguna idea?
Edit: También manejo el evento StartUpNextInstance para enviar mis argumentos de línea de comandos a mi formulario principal. Para propósitos de prueba, fue eliminado aquí.
Editar: Se ha añadido un poco más de código. Ahora tienes toda la extensión de mi bootstrap.
Éste es mi código. SO toma esto bastante en serio, se requiere atribución. Debe incluir un enlace al código original y el nombre del autor. –
Tomé este código hace mucho tiempo (creo que las fechas son meses) y solo lo agregué al SplashScreen. Lo siento si fue tuyo para empezar. ¿Cómo puedo agregar su atribución y enlace? – Olyan