启动屏幕C#winform后,主窗体将不会显示在任务栏上
我已经显示了一个启动屏幕,它工作得很好,但主窗体不会显示在任务栏和焦点上 有两种形式 1.溅屏 2.福尔曼 在splashscreen上,我添加了一个计时器,并执行下面的一些代码启动屏幕C#winform后,主窗体将不会显示在任务栏上,c#,winforms,C#,Winforms,我已经显示了一个启动屏幕,它工作得很好,但主窗体不会显示在任务栏和焦点上 有两种形式 1.溅屏 2.福尔曼 在splashscreen上,我添加了一个计时器,并执行下面的一些代码 public SplashScreen() { InitializeComponent(); } private void splashtimer_Tick(object sender, EventArgs e) {
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());
}
关于主形式“formMain”
我在下面添加了代码
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中时,将显示formMain。为什么
问题出在哪里?请尝试以下代码以显示启动屏幕:
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;
}
请尝试以下代码以显示启动屏幕:
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;
}
闪屏工作正常,但主窗体不会聚焦。我运行EXE
从调试文件夹运行它,显示启动屏幕和主窗体
没有播种。未显示任务栏图标。在Ctrl+tab中时,formMain为
展示。为什么
我试过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));
}
}
公共部分类SplashScreen:表单{
公共屏幕(){
初始化组件();
progressBar1.Style=ProgressBarStyle.Marquee;
}
}
公共部分类formMain:Form{
公共formMain(表单飞溅){
初始化组件();
//请确保始终服从GUI更新,否则您的progressbar将无法刷新
对于(int i=0;i<100;++i){
线程。睡眠(100);//做些工作
Application.DoEvents();
}
splash.Close();
}
}
静态类程序{
[状态线程]
静态void Main(){
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var frm=新的飞溅屏幕();
frm.Show();
运行(新formMain(frm));
}
}
闪屏工作正常,但主窗体不会聚焦。我运行EXE
从调试文件夹运行它,显示启动屏幕和主窗体
没有播种。未显示任务栏图标。在Ctrl+tab中时,formMain为
展示。为什么
我试过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));
}
}
公共部分类SplashScreen:表单{
公共屏幕(){
初始化组件();
progressBar1.Style=ProgressBarStyle.Marquee;
}
}
公共部分类formMain:Form{
公共formMain(表单飞溅){
初始化组件();
//请确保始终服从GUI更新,否则您的progressbar将无法刷新
对于(int i=0;i<100;++i){
线程。睡眠(100);//做些工作
Application.DoEvents();
}
splash.Close();
}
}
静态类程序{
[状态线程]
静态void Main(){
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var frm=新的飞溅屏幕();
frm.Show();
运行(新formMain(frm));
}
}
永远不要调用Thread.Abort()
,除非您试图强制关闭整个应用程序。它会使进程处于未定义状态。I comment abort()它不会隐藏启动屏幕是的,但它可能会杀死整个应用程序。您应该保留对ss=newsplashscreen()
的引用,并调用ss.Invoke((操作)(()=>ss.Close())代码>相反。在关闭启动屏幕之前,您需要显示主窗口。你现在这样做,有一个短暂的时刻,你的应用程序没有窗口可以获得焦点。Windows将找到另一个焦点,它将由另一个应用程序拥有。你的主窗口终于出现了,在它后面。请不要自己这样做,使用启动屏幕支持。永远不要调用Thread.Abort()
,除非您试图强制关闭整个应用程序。它会使进程处于未定义状态。I comment abort()它不会隐藏启动屏幕是的,但它可能会杀死整个应用程序。您应该保留对ss=newsplashscreen()
的引用,并调用ss.Invoke((操作)(()=>ss.Close())代码>相反。在关闭启动屏幕之前,您需要显示主窗口。你现在这样做,有一个短暂的时刻,你的应用程序没有窗口可以获得焦点。Windows将找到另一个焦点,它将由另一个应用程序拥有。你的主窗口终于出现了,在它后面。只是不要自己做,使用启动屏幕支持。我尝试了这段代码,但启动屏幕显示在formMain中。我需要先启动屏幕,然后是formMain。我尝试了这个代码,它可以工作,但是启动屏幕显示在formMain中。我需要先启动屏幕,然后是formMain。