主窗体在初始屏幕 C# winform 后不会显示在任务栏上
本文关键字:显示 任务栏 窗体 屏幕 winform | 更新日期: 2023-09-27 18:31:08
我展示了一个启动画面,它工作正常,但主窗体不会显示在任务栏和焦点上。
有2种形式1. 启动画面2. 表格主
在启动屏幕上,我添加了一个计时器并在下面执行一些代码。
public SplashScreen()
{
InitializeComponent();
}
private void splashtimer_Tick(object sender, EventArgs e)
{
progressBar1.Increment(1);
if (progressBar1.Value == 100) splashtimer.Stop();
}
在主窗体"窗体主"上
我在下面添加了代码。
public formMain()
{
this.ShowInTaskbar = true;
Thread t = new Thread(new ThreadStart(ShowSplashScreen));
t.Start();
Thread.Sleep(5000);
InitializeComponent();
t.Abort();
}
public void ShowSplashScreen()
{
Application.Run(new SplashScreen());
}
初始屏幕工作正常,但主窗体不会聚焦。我从调试文件夹运行 EXE 并运行它,显示初始屏幕并且未播种主窗体。未显示任务栏图标。在 Ctrl+tab 中时,将显示窗体 Main。为什么???
问题出在哪里?
初始屏幕工作正常,但主窗体不会聚焦。我运行 EXE 从调试文件夹并运行它,显示初始屏幕和主窗体 不播种。未显示任务栏图标。在 Ctrl+tab 中时,窗体 Main 是 显示。为什么???
我在VS 2015上尝试过,我的formMain和SplashScreen工作正常,除了你提到的,formMain没有聚焦。这可能是因为您在创建并聚焦 formMain 后使用 Application.Run 将焦点转到 SplashScreen。
无论哪种情况,您的方法都不是那么干净。试试我在下面的方式。这也将解决焦点问题
public partial class SplashScreen : Form {
public SplashScreen() {
InitializeComponent();
progressBar1.Style = ProgressBarStyle.Marquee;
}
}
public partial class formMain : Form {
public formMain(Form splash) {
InitializeComponent();
// make sure to keep yielding to GUI updates, else your progressbar will nto refresh
for (int i = 0; i < 100; ++i) {
Thread.Sleep(100); // do some work
Application.DoEvents();
}
splash.Close();
}
}
static class Program {
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var frm = new SplashScreen();
frm.Show();
Application.Run(new formMain(frm));
}
}
尝试使用以下代码来显示初始屏幕:
private void formMain_Load(object sender, EventArgs e)
{
EventHandler activated = null;
activated = (s2, e2) =>
{
this.Activated -= activated;
this.ShowInTaskbar = true;
var splash = new SplashScreen();
splash.ShowInTaskbar = false;
splash.Show();
var timer = new System.Windows.Forms.Timer();
EventHandler tick = null;
tick = (s3, e3) =>
{
timer.Enabled = false;
timer.Tick -= tick;
timer.Dispose();
splash.Close();
splash.Dispose();
};
timer.Tick += tick;
timer.Interval = 5000;
timer.Enabled = true;
};
this.Activated += activated;
}