1. 程式人生 > >一個比較完整的WindowsFormsApplication實現

一個比較完整的WindowsFormsApplication實現

這是前兩天課堂上的例子,提供了一個自定義WindowsFormsApplication的範例,主要包括如下的功能

1. 單一例項

2. 閃屏

3. 登陸視窗

4. 工作列圖示和選單

需要注意的是,WindowsFormsApplicationBase這個類是要新增Microsoft.VisualBisic.dll引用的

    class MyApplication : WindowsFormsApplicationBase
    {
        NotifyIcon taskBarIcon;
        public MyApplication(bool singleton)
            : base
(AuthenticationMode.ApplicationDefined) { //設定單例項 this.IsSingleInstance = true; //新增工作列按鈕 taskBarIcon = new NotifyIcon(); taskBarIcon.Icon = Properties.Resources.Northwind; ContextMenuStrip contextMenu = new ContextMenuStrip(); contextMenu.Items.Add( new
ToolStripMenuItem( "退出", Properties.Resources.close, (sender, eventArgs) => { if (MessageBox.Show( "你是否真的要退出?", "確認", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.Yes) { Application.Exit(); } })); contextMenu.Items.Add( new
ToolStripMenuItem( "幫助", Properties.Resources.help, (sender, eventArgs) => { MessageBox.Show("幫助文件還在製作中"); })); taskBarIcon.ContextMenuStrip = contextMenu; taskBarIcon.ShowBalloonTip(2000, "歡迎", "歡迎使用該軟體", ToolTipIcon.Info); taskBarIcon.Visible = true; } public MyApplication() : this(true) { } protected override void OnCreateSplashScreen() { base.OnCreateSplashScreen(); LoginForm login = new LoginForm(); if (login.ShowDialog() != DialogResult.OK) { Environment.Exit(-1);//這裡不能用Application.Exit,因為當前是一個自定義的Application } this.MinimumSplashScreenDisplayTime = 2000;//最少顯示兩秒 this.SplashScreen = new SplashForm(); } protected override void OnShutdown() { base.OnShutdown(); taskBarIcon.Dispose(); } protected override void OnStartupNextInstance(StartupNextInstanceEventArgs eventArgs) { base.OnStartupNextInstance(eventArgs); eventArgs.BringToForeground = true; } protected override void OnCreateMainForm() { base.OnCreateMainForm(); this.MainForm = new MainForm(); } }