标题:使用Microsoft.VisualBasic类来做 程序启动界面
只看楼主
heyu52
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2008-4-11
 问题点数:0 回复次数:0 
使用Microsoft.VisualBasic类来做 程序启动界面
一般的程序都有一个登录界面窗体LoginDialog,还有一个SplashScreen窗体,然后再到程序的主窗体MainForm
NET里面当我们指定了窗体的DialogResult时,窗体会自动关闭,相信这个很少人知道
首先我们在LoginDialog画两个button,一个为OK,另一个为cancel,代码分别为为
 private void OK_Click(object sender, EventMicrosoft.VisualBasicArgs e)
        {
//这里作你的验证
            this.DialogResult = DialogResult.OK;//这一句窗体会自动关闭,相当于close,这里只是为窗体指定了返回值
        }

        private void Cancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

当程序有了这三个窗体以后,现在来修改Program.cs这个类

 Main()只要这样就可以了:

 static void Main()
        {
            Application.EnableVisualStyles();
            string[] args = Environment.GetCommandLineArgs();
            WinFormApplication.Appplication.Run(args);
        }

然后我们新增一个类,命名为WinFormApplication
它继承于Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase这个类,在用之前,你要首先引用Microsoft.VisualBasic

代码如下:
 internal sealed class WinFormApplication : Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase
        {
            private static WinFormApplication _appplication;

            public static WinFormApplication Appplication
            {
                get
                {
                    if (_appplication == null)
                        _appplication = new WinFormApplication();

                    return _appplication;
                }
            }

            public WinFormApplication()
            {
                this.Initialize();
            }

            private void Initialize()
            {
                this.EnableVisualStyles = true;
                this.IsSingleInstance = true;//指定单实例
                this.SaveMySettingsOnExit = false;
                this.ShutdownStyle = Microsoft.VisualBasic.ApplicationServices.ShutdownMode.AfterMainFormCloses;
            }

            protected override bool OnInitialize(System.Collections.ObjectModel.ReadOnlyCollection<string> commandLineArgs)
            {
                try
                {
                    this.DoEvents();
                    if (ShowLoginDialog() == DialogResult.Cancel) //在这里,通过接收登录窗体的返回值来看是否启动主程序

                        return false;//如果按了cancel,返回false,程序将不会再执行下去,且自动退出
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                    return false;
                }

                return base.OnInitialize(commandLineArgs);
            }

            protected override void OnCreateSplashScreen()
            {
                this.SplashScreen = new ShowSplashScreen();//这里指定SplashScreen,也可以没有这个
            }

            protected override void OnCreateMainForm()
            {
                this.ShowSplashScreen();
                MainForm mainForm = new MainForm();
                this.DoEvents();
                this.MainForm = mainForm;//指定主窗体
            }

            protected override void OnStartupNextInstance(Microsoft.VisualBasic.ApplicationServices.StartupNextInstanceEventArgs eventArgs)
            {
                eventArgs.BringToForeground = true;
                base.OnStartupNextInstance(eventArgs);
            }


            private DialogResult ShowLoginDialog()
            {
                LoginDialog loginDialog = null;
                loginDialog = new LoginDialog();
                if (loginDialog.ShowDialog() != DialogResult.OK)
                    return DialogResult.Cancel;

                return DialogResult.OK;
            }
   
        }
搜索更多相关主题的帖子: VisualBasic Microsoft 界面 
2008-06-08 02:19



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-218181-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.044378 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved