| ページ一覧 | ブログ | twitter |  書式 | 書式(表) |

MyMemoWiki

「C Sharp バックグラウンドで動く」の版間の差分

提供: MyMemoWiki
ナビゲーションに移動 検索に移動
 
(同じ利用者による、間の1版が非表示)
1行目: 1行目:
==C# バックグラウンドで動く==
+
==[[C# バックグラウンドで動く]]==
| [[C#]] | [[Visual Studio]] | [[C# タスクトレイ]]
+
[[C Sharp]] | [[Visual Studio]] | [[C Sharp タスクトレイ]] |
  
 
起動後、画面を指定時間経過後に非表示にしてタスクトレイに常駐させる
 
起動後、画面を指定時間経過後に非表示にしてタスクトレイに常駐させる
===画面のデザイン===
+
===画面の[[デザイン]]===
*NotifyIcon と ContextMenuStrip を使って、[C# タスクトレイ] [タスクトレイ]アプリケーションを作成する。
+
*NotifyIcon と ContextMenuStrip を使って、[[C# タスクトレイ|タスクトレイ]]アプリケーションを作成する。
 
[[File:0285_background_form01.jpg]]
 
[[File:0285_background_form01.jpg]]
 
===コーディング===
 
===コーディング===
 
====Progma.cs====
 
====Progma.cs====
*Application.Run に Form ではなく、ApplicationContext のインスタンスを渡すように修正
+
*Application.[[R]]un に Form ではなく、ApplicationContext のインスタンスを渡すように修正
 
  using System;
 
  using System;
  using System.Windows.Forms;
+
  using System.[[Windows]].Forms;
 
   
 
   
 
  namespace MyApp
 
  namespace MyApp
23行目: 23行目:
 
         {
 
         {
 
             Application.EnableVisualStyles();
 
             Application.EnableVisualStyles();
             Application.SetCompatibleTextRenderingDefault(false);
+
             Application.SetCompatibleText[[R]]enderingDefault(false);
 
             // Form ではなく、ApplicationContext のインスタンスを渡すように修正
 
             // Form ではなく、ApplicationContext のインスタンスを渡すように修正
             Application.Run(new MyAppContext());
+
             Application.[[R]]un(new MyAppContext());
 
         }
 
         }
 
     }
 
     }
 
  }
 
  }
 
====ApplicationContext を派生させてクラスを作成====
 
====ApplicationContext を派生させてクラスを作成====
*System.Windows.Forms.ApplicationContext
+
*System.[[Windows]].Forms.ApplicationContext
 
  using System;
 
  using System;
  using System.Windows.Forms;
+
  using System.[[Windows]].Forms;
 
   
 
   
 
  namespace MyApp
 
  namespace MyApp
59行目: 59行目:
 
====Form を作成====
 
====Form を作成====
 
  using System;
 
  using System;
  using System.Windows.Forms;
+
  using System.[[Windows]].Forms;
 
   
 
   
 
  namespace MyApp
 
  namespace MyApp
69行目: 69行目:
 
             InitializeComponent();
 
             InitializeComponent();
 
         }
 
         }
         private void toolStripMenuItemExit_Click(object sender, EventArgs e)
+
         private void toolStrip[[Menu]]ItemExit_Click(object sender, EventArgs e)
 
         {
 
         {
 
             // コンテキストメニューの終了メニューで、フォームを閉じる
 
             // コンテキストメニューの終了メニューで、フォームを閉じる

2020年2月16日 (日) 04:22時点における最新版

C# バックグラウンドで動く

C Sharp | Visual Studio | C Sharp タスクトレイ |

起動後、画面を指定時間経過後に非表示にしてタスクトレイに常駐させる

画面のデザイン

  • NotifyIcon と ContextMenuStrip を使って、タスクトレイアプリケーションを作成する。

0285 background form01.jpg

コーディング

Progma.cs

  • Application.Run に Form ではなく、ApplicationContext のインスタンスを渡すように修正
using System;
using System.Windows.Forms;

namespace MyApp
{
    static class Program
    {
        /// <summary>
        /// アプリケーションのメイン エントリ ポイントです。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            // Form ではなく、ApplicationContext のインスタンスを渡すように修正
            Application.Run(new MyAppContext());
        }
    }
}

ApplicationContext を派生させてクラスを作成

  • System.Windows.Forms.ApplicationContext
using System;
using System.Windows.Forms;

namespace MyApp
{
    class MyAppContext : ApplicationContext
    {
        public MyAppContext()
        {
            LauncherForm launcherForm = new LauncherForm();
            // 起動する画面のCloseイベントをこのクラスのOnLauncherFormClosedでハンドリングする
            launcherForm.FormClosed += new FormClosedEventHandler(this.OnLauncherFormClosed);
            
            launcherForm.Show();
            // 画面表示させて1秒待機
            System.Threading.Thread.Sleep(1000);
            // 画面を非表示に
            launcherForm.Hide();
        }
        private void OnLauncherFormClosed(object sender, EventArgs e)
        {
            // アプリケーションの終了
            ExitThread();
        }
    }
}

Form を作成

using System;
using System.Windows.Forms;

namespace MyApp
{
    public partial class LauncherForm : Form
    {
        public LauncherForm()
        {
            InitializeComponent();
        }
        private void toolStripMenuItemExit_Click(object sender, EventArgs e)
        {
            // コンテキストメニューの終了メニューで、フォームを閉じる
            this.Close();
        }
    }
}