Tôi tất cả mọi người. Tôi hiện đang gặp vấn đề với tiêu điểm của mình khi sử dụng màn hình giật gân. Tôi đang sử dụng VS2008, với .NET framework 2.0. Ngoài ra, tôi đã liên kết dự án của tôi với VisualBasic.dll vì tôi sử dụng ApplicationServices để quản lý ứng dụng cá thể và màn hình giật gân của mình.Màn hình Splash không trả về tiêu điểm ở dạng chính
Đây là đoạn mã được đơn giản hóa về những gì tôi đã thử gỡ lỗi.
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);
}
}
}
Bây giờ, điều xảy ra là khi tôi khởi động ứng dụng, màn hình Splash hiển thị như mong đợi, nhưng khi nó bị hủy, nó sẽ không trả tiêu điểm về biểu mẫu chính (Form1 trong thử nghiệm). MainForm chỉ đơn giản là nhấp nháy màu cam trên thanh tác vụ. Nếu tôi khởi chạy ứng dụng từ IDE (VS2008), tiêu điểm hoạt động tốt. Tôi đang sử dụng XP Pro. Ngoài ra, hình thức chính không phải là trên đầu trang của mỗi cửa sổ khác. Nếu tôi nhận xét phương thức OnCreateSplashScreen(), ứng dụng sẽ tập trung bình thường.
Để kiểm tra việc thực hiện bình thường, tôi đang sử dụng Dấu nhắc lệnh VS để khởi chạy ứng dụng của mình. Tôi đang sử dụng phiên bản Release của dự án của tôi.
Bất kỳ ý tưởng nào?
Chỉnh sửa: Tôi cũng xử lý sự kiện StartUpNextInstance để gửi các đối số dòng lệnh của tôi tới biểu mẫu chính của tôi. Đối với mục đích thử nghiệm, nó đã được gỡ bỏ ở đây.
Chỉnh sửa: Đã thêm mã một chút. Bây giờ bạn có toàn bộ mức độ của bootstrap của tôi.
Đây là mã của tôi. SO thực hiện điều này khá nghiêm túc, phân bổ là bắt buộc. Bạn phải bao gồm một liên kết đến mã gốc và tên của tác giả. –
Tôi đã lấy mã này từ lâu (tháng tháng tôi nghĩ) và chỉ được thêm vào cho SplashScreen. Xin lỗi nếu đó là của bạn để bắt đầu. Làm cách nào để thêm thuộc tính và liên kết của bạn? – Olyan