首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

TopMost属性无效,帮忙看看是什么有关问题,多谢了

2012-02-22 
TopMost属性无效,帮忙看看是什么问题,谢谢了!我参考下面的文章写了一个SplashScreen:http://www.codeproje

TopMost属性无效,帮忙看看是什么问题,谢谢了!
我参考下面的文章写了一个SplashScreen:
http://www.codeproject.com/csharp/PrettyGoodSplashScreen.asp
为了简化问题,我的主要代码如下:

SplashScreen.cs:
using   System;
using   System.Collections.Generic;
using   System.ComponentModel;
using   System.Data;
using   System.Drawing;
using   System.Text;
using   System.Windows.Forms;
using   System.Threading;

namespace   SplashScreen
{


        public   partial   class   SplashScreen   :   Form
        {
                static   SplashScreen   ms_frmSplash   =   null;
                static   Thread   ms_oThread   =   null;

                static   public   void   ShowSplashScreen()
                {
                        //   Make   sure   it   is   only   launched   once.
                        if   (ms_frmSplash   !=   null)
                                return;
                        ms_oThread   =   new   Thread(new   ThreadStart(SplashScreen.ShowForm));
                        ms_oThread.Start();
                }

                static   private   void   ShowForm()
                {
                        ms_frmSplash   =   new   SplashScreen();
                        Application.Run(ms_frmSplash);
                }

                static   public   void   CloseForm()
                {
                        ms_frmSplash.Close();
                }
               
                public   SplashScreen()
                {
                        InitializeComponent();

                        this.ClientSize   =   this.BackgroundImage.Size;
                }
        }
}

Program.cs:
using   System;
using   System.Collections.Generic;
using   System.Windows.Forms;
using   System.Threading;
namespace   SplashScreen
{
        static   class   Program
        {
                ///   <summary>


                ///   应用程序的主入口点。
                ///   </summary>
                [STAThread]
                static   void   Main()
                {
                        Application.EnableVisualStyles();
                        Application.SetCompatibleTextRenderingDefault(false);
                        //Application.Run(new   SplashScreen());
                        SplashScreen.ShowSplashScreen();
                        Thread.Sleep(3000);
                        SplashScreen.CloseForm();

                        Form1   form1   =   new   Form1();
                        form1.TopMost   =   true;
                        form1.ShowDialog();

                }
        }
}

无论怎样设置form1的TopMost属性,都无法将form1显示在最前端,而且似乎不仅仅是TopMost属性的问题,当SplashScreen窗口关闭后根本看不到form1窗口,form1窗口隐藏到了当前窗口(例如VisualStudio开发窗口)的后面,一定要最小化当前的窗口才能看到form1窗口。

如果我把Program.cs中显示SplashScreen的三行代码去掉,form1就正常了。

请大家帮忙看看问题在哪?


[解决办法]
对于界面上的编程,不是不得已,最好不把界面或控件在其它的线程操作,这样会带来不必要的麻烦.觉得你完全可以不使用线程而达到相同的效果.

当然在其它的线程中是可以操作任何一个控件的,不过是通过Invoke要转到建立控件的线程中去执行.还是那句话,没有必要的话,不要这样做.
[解决办法]
to 但是,如果我需要在一个新的线程中启动SplashScreen该如何写呢? :)

你可以参考我的一篇文章
如何弹出一个模式窗口来显示进度条
http://blog.csdn.net/knight94/archive/2006/05/27/757351.aspx

热点排行