using System; using System.Collections.Generic; using System.Linq; using System.Text; //启动窗体虚基类,继承自ApplicationContext using System.Windows.Forms; using System.Threading; namespace TransmissionLine.PublicClass { public abstract class ShelterfromClass : ApplicationContext { private Form _SplashScreenForm;//启动窗体 private Form _PrimaryForm;//主窗体 private System.Timers.Timer _SplashScreenTimer; private int _SplashScreenTimerInterVal = 20000;//默认是启动窗体显示20秒 private bool _bSplashScreenClosed = false; private delegate void DisposeDelegate();//关闭委托,下面需要使用控件的Invoke方法,该方法需要这个委托 public ShelterfromClass() { this.ShowSplashScreen();//这里创建和显示启动窗体 this.MainFormLoad();//这里创建和显示启动主窗体 } protected abstract void OnCreateSplashScreenForm(); protected abstract void OnCreateMainForm(); protected abstract void SetSeconds(); protected Form SplashScreenForm { set { this._SplashScreenForm = value; } } protected Form PrimaryForm {//在派生类中重写OnCreateMainForm方法,在MainFormLoad方法中调用OnCreateMainForm方法 // ,在这里才会真正调用Form1(主窗体)的构造函数,即在启动窗体显示后再调用主窗体的构造函数 // ,以避免这种情况:主窗体构造所需时间较长,在屏幕上许久没有响应,看不到启动窗体 set { this._PrimaryForm = value; } } protected int SecondsShow {//未设置启动画面停留时间时,使用默认时间 set { if (value != 0) { this._SplashScreenTimerInterVal = 1000 * value; } } } private void ShowSplashScreen() { this.SetSeconds(); this.OnCreateSplashScreenForm(); this._SplashScreenTimer = new System.Timers.Timer(((double)(this._SplashScreenTi merInterVal))); _SplashScreenTimer.Elapsed += new System.Timers.ElapsedEventHandler(new System.Timers.ElapsedEventHandler(this.SplashScree nDisplayTimeUp)); this._SplashScreenTimer.AutoReset = false;//这句不处理控件带来的异常 Thread DisplaySpashScreenThread = new Thread(new ThreadStart(DisplaySplashScreen));//心跳开启 DisplaySpashScreenThread.Start(); } private void DisplaySplashScreen() { this._SplashScreenTimer.Enabled = true; Application.Run(this._SplashScreenForm); } private void SplashScreenDisplayTimeUp(object sender, System.Timers.ElapsedEventArgs e) { this._SplashScreenTimer.Dispose(); this._SplashScreenTimer = null; this._bSplashScreenClosed = true; } private void MainFormLoad() { this.OnCreateMainForm(); while (!(this._bSplashScreenClosed)) { Application.DoEvents(); } DisposeDelegate SplashScreenFormDisposeDelegate = new DisposeDelegate(this._SplashScreenForm.Dispose); this._SplashScreenForm.Invoke(SplashScreenFormDisp oseDelegate); this._SplashScreenForm = null; //必须先显示,再激活,否则主窗体不能在启动窗体消失后出现 this._PrimaryForm.Show(); this._PrimaryForm.Activate(); this._PrimaryForm.Closed += new EventHandler(_PrimaryForm_Closed); } private void _PrimaryForm_Closed(object sender, EventArgs e) { base.ExitThread(); } } } using System; using System.Collections.Generic; using System.Linq; using System.Windows.Forms; using System.Runtime; using System.Runtime.InteropServices; namespace TransmissionLine { static class Program { /// /// 应用程序的主入口点。 /// //[STAThread] //static void Main() //{ // Application.EnableVisualStyles(); // Application.SetCompatibleTextRenderingDefault(fals e); // Application.Run(new Login()); //} //------------------------------------------------ ------------------------ //指示该属性化方法由非托管动态链接库 (DLL) 作为静态入口点公开。引入皮肤接口 [关键代码] [DllImport(@"appface.dll")] //注意路径 public static extern int SkinStart(string SkinFile, int nDefWinType, int CheckSum, int nType, int hInstance, int nLen); [DllImport(@"appface.dll")] public static extern int SkinRemove(); [DllImport(@"appface.dll")] public static extern int SkinWindowSet(IntPtr hWnd, int nSkintype); [DllImport(@"appface.dll")] public static extern int SkinWindowSetEx(IntPtr hWnd, int nSkintype, int nResourceId, int nUrfLoadType, string SkinFile, int hInstance, int nLen); //------------------------------------------------ ------------------------ [STAThread] static void Main() { //加载皮肤(加载你需要的皮肤,注意路径)[关键代码] SkinStart("skins\\basic_urf\\winmp_af.urf", 0, 0, 1, 0, 0); //第一次循环 LoginContext loginContext = new LoginContext(); Application.Run(loginContext); SkinRemove();//[关键代码] if (Login.LoginResult == true) //必须判断登陆是否成功,如果为真才执行下面操作,打开主页面 {//第二次循环 //加载皮肤(加载你需要的皮肤,注意路径)[关键代码] //SkinStart("skin\\plex.urf", 0, 0, 1, 0, 0); //SkinStart("skin\\leaf.urf", 0, 0, 1, 0, 0); //Second Message Loop try { Application.Run(new mycontext()); } catch { } //SkinRemove();//[关键代码] } //else if (dpMain.logout == true) //{ // MainContext mainContext = new MainContext(); // Application.Run(mainContext); //} } public class LoginContext : ApplicationContext { private Login loginDialog; public LoginContext() { loginDialog = new Login(); loginDialog.StartPosition = FormStartPosition.CenterScreen; loginDialog.Closed += new EventHandler(OnFormClosed); loginDialog.Show(); } private void OnFormClosed(object sender, EventArgs e) {//页面关闭时执行的操作 if (Login.LoginResult == true) {//为真打开主页面 Login.LoginResult = !(this.loginDialog.DialogResult == DialogResult.Cancel); ExitThread(); } else {//否则关闭线程 //Application.Exit(); ExitThread(); } } } //public class MainContext : ApplicationContext //{ // private dpMain mainForm; // public MainContext() // { // mainForm = new dpMain(); // mainForm.StartPosition = FormStartPosition.CenterScreen; // mainForm.Closed += new EventHandler(OnFormClosed); // mainForm.Show(); // //mainForm.Hide(); // } // private void OnFormClosed(object sender, EventArgs e) // {//页面关闭时记录操作 // ExitThread(); // } //} //启动窗体类(继承自启动窗体虚基类),启动画面会停留一段时间,该时间是设定的时间和主窗体构造所需时间两个的最大值 public class mycontext : PublicClass.ShelterfromClass { protected override void OnCreateSplashScreenForm() { this.SplashScreenForm = new Shelterfrom();//启动窗体 } protected override void OnCreateMainForm() { this.PrimaryForm = new dpMain();//主窗体 } protected override void SetSeconds() { this.SecondsShow = 2;//启动窗体显示的时间(秒) } } } }