《C#程序启动欢迎窗体实现.docx》由会员分享,可在线阅读,更多相关《C#程序启动欢迎窗体实现.docx(2页珍藏版)》请在三一办公上搜索。
1、C#程序启动欢迎窗体实现C#程序启动欢迎窗体实现 当程序在启动过程中需要花一些时间去加载资源时,我们希望程序能显示一个欢迎界面,能简单介绍软件功能的同时还能告知用户该程序还在加载中,使得用户体验更友好。 实现如下: 1. 添加欢迎界面的窗体(比如SlpashForm),做以下调整: 将FormBorderStyle属性设成None,即没有窗体边框 将StartPosition属性设成CenterScreen,即总是居中 将TopMost属性设成True,即总是在顶部 将UseWaitCursor属性设成Ture,即显示等待光标,让人感觉后台还在运行 增加一个PictureBox控件,与欢迎图片
2、大小一致,窗体的大小也设成一致 增加一个ProgressBar控件,将Style设成Marquee,将MarqueeAnimationSpeed设成50 2. 主界面的构造函数改成以下代码: / Create thread to show splash window Thread showSplashThread = new Thread(new ThreadStart(ShowSplash); showSplashThread.Start; / Time consumed here InitializeFrame; / 把原来构造函数中的所有代码移到该函数中 / Abort show spl
3、ash thread showSplashThread.Abort; showSplashThread.Join; / Wait until the thread aborted showSplashThread = null; 3. 显示SplashForm的线程函数 / / Thread to show the splash. / private void ShowSplash SplashForm sForm = null; try sForm = new SplashForm; sForm.ShowDialog; catch (ThreadAbortException e) / Thr
4、ead was aborted normally if (_log.IsDebugEnabled) _log.Debug(“Splash window was aborted normally: ” + e.Message); finally sForm = null; 4. 在主窗体的Load事件加激活自己的代码 SetForegroundWindow(Process.GetCurrentProcess.MainWindowHandle); 在使用SetForegroundWindow之前先声明一下 / Uses to active the exist window DllImport(User32.dll) public static extern void SetForegroundWindow(IntPtr hwnd);