■ 트레이 아이콘을 사용하는 방법을 보여준다.
▶ Program.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
using System; using System.Windows.Forms; namespace TestProject { /// <summary> /// 프로그램 /// </summary> static class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main() /// <summary> /// 프로그램 시작하기 /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new TestApplicationContext()); } #endregion } } |
▶ TestApplicationContext.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
using System; using System.Collections.Generic; using System.Text; using System.Windows.Forms; namespace TestProject { /// <summary> /// 테스트 애플리케이션 컨텍스트 /// </summary> public class TestApplicationContext : ApplicationContext { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// Configuration 메뉴 항목 /// </summary> private MenuItem configurationMenuItem; /// <summary> /// Exit 메뉴 항목 /// </summary> private MenuItem exitMenuItem; /// <summary> /// 알림 아이콘 /// </summary> private NotifyIcon notifyIcon; /// <summary> /// 구성 폼 /// </summary> private ConfigurationForm configurationForm; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - TaskTrayApplicationContext() /// <summary> /// 생성자 /// </summary> public TestApplicationContext() { this.configurationMenuItem = new MenuItem(); this.configurationMenuItem.Name = "configurationMenuItem"; this.configurationMenuItem.Text = "Configuration"; this.exitMenuItem = new MenuItem(); this.exitMenuItem.Name = "exitMenuItem"; this.exitMenuItem.Text = "Exit"; this.notifyIcon = new NotifyIcon(); this.notifyIcon.Icon = TestProject.Properties.Resources.tray; this.notifyIcon.ContextMenu = new ContextMenu(new MenuItem[] { configurationMenuItem, exitMenuItem }); this.notifyIcon.Visible = true; this.configurationForm = new ConfigurationForm(); this.configurationMenuItem.Click += configurationMenuItem_Click; this.exitMenuItem.Click += exitMenuItem_Click; this.notifyIcon.DoubleClick += notifyIcon_DoubleClick; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region Configuration 메뉴 항목 클릭시 처리하기 - configurationMenuItem_Click(sender, e) /// <summary> /// Configuration 메뉴 항목 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void configurationMenuItem_Click(object sender, EventArgs e) { if(this.configurationForm.Visible) { this.configurationForm.Focus(); } else { this.configurationForm.ShowDialog(); } } #endregion #region Exit 메뉴 항목 클릭시 처리하기 - exitMenuItem_Click(sender, e) /// <summary> /// Exit 메뉴 항목 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void exitMenuItem_Click(object sender, EventArgs e) { this.notifyIcon.Visible = false; Application.Exit(); } #endregion #region 알림 아이콘 더블 클릭시 처리하기 - notifyIcon_DoubleClick(sender, e) /// <summary> /// 알림 아이콘 더블 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void notifyIcon_DoubleClick(object sender, EventArgs e) { if(this.configurationForm.ShowMessage) { MessageBox.Show("Information", "Hello World", MessageBoxButtons.OK, MessageBoxIcon.Information); } } #endregion } } |
▶ ConfigurationForm.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
using System; using System.Windows.Forms; namespace TestProject { /// <summary> /// 구성 폼 /// </summary> public partial class ConfigurationForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 메시지 표시 여부 /// </summary> private bool showMessage = true; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 메시지 표시 여부 - ShowMessage /// <summary> /// 메시지 표시 여부 /// </summary> public bool ShowMessage { get { return this.showMessage; } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - ConfigurationForm() /// <summary> /// 생성자 /// </summary> public ConfigurationForm() { InitializeComponent(); Activated += Form_Activated; this.applyButton.Click += applyButton_Click; this.cancelButton.Click += cancelButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 폼 활성화시 처리하기 - Form_Activated(sender, e) /// <summary> /// 폼 활성화시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Form_Activated(object sender, EventArgs e) { this.showMessageCheckBox.Checked = this.showMessage; } #endregion #region Apply 버튼 클릭시 처리하기 - applyButton_Click(sender, e) /// <summary> /// Apply 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void applyButton_Click(object sender, EventArgs e) { this.showMessage = this.showMessageCheckBox.Checked; Close(); } #endregion #region Cancel 버튼 클릭시 처리하기 - cancelButton_Click(sender, e) /// <summary> /// Cancel 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void cancelButton_Click(object sender, EventArgs e) { Close(); } #endregion } } |