Programing (프로그래밍)/WPF & C# (C Sharp)
트레이아이콘 ( TrayIcon / notifyIcon )
insurang
2018. 1. 2. 11:52
728x90
반응형
트레이아이콘 ( TrayIcon / notifyIcon )
트레이아이콘 사용할 일이 생겨서 찾아보았다.
프로그램 종료시 아이콘이 남아있는 현상이 있어서,
종료코드에 notify.Dispose(); 을 넣어 주니 해결되었다.
확실한지는 잘 모르겠으나 일단 반응은 잘 된다.
아이콘도 사용하기 쉽게 URI로 설정을 바꾸었다.
아... 코딩하기전에 반드시 참조추가로 'System.Windows.Forms' 를 추가해주어야 한다.
참조추가는 우측에 '해당 솔루션 > 해당 프로젝트 > 참조' 에서 마우스 우클릭해서 '참조추가' 하면 된다.
MainWindows.xaml
1 2 3 4 5 6 7 8 9 10 11 12 | <Window x:Class="TryIcon_Test.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:TryIcon_Test" mc:Ignorable="d" Title="MainWindow" Height="171" Width="310" Loaded="Window_Loaded"> <Grid> <TextBlock HorizontalAlignment="Left" Margin="10,10,0,0" TextWrapping="Wrap" Text="트레이아이콘 테스트 ( notifyIcon )" VerticalAlignment="Top"/> </Grid> </Window> | cs |
MainWindows.xaml.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 | using System; using System.Windows; namespace TryIcon_Test { /// <summary> /// MainWindow.xaml에 대한 상호 작용 논리 /// </summary> /// public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } public System.Windows.Forms.NotifyIcon notify; private void Window_Loaded(object sender, RoutedEventArgs e) { try { System.Windows.Forms.ContextMenu menu = new System.Windows.Forms.ContextMenu(); // 아이콘 설정부분 notify = new System.Windows.Forms.NotifyIcon(); // notify.Icon = new System.Drawing.Icon(@"TiimeAlram.ico"); // 외부아이콘 사용 시 notify.Icon = Properties.Resources.TiimeAlram; // Resources 아이콘 사용 시 notify.Visible = true; notify.ContextMenu = menu; notify.Text = "Test"; // 아이콘 더블클릭 이벤트 설정 notify.DoubleClick += Notify_DoubleClick; System.Windows.Forms.MenuItem item1 = new System.Windows.Forms.MenuItem(); menu.MenuItems.Add(item1); item1.Index = 0; item1.Text = "프로그램 종료"; item1.Click += delegate (object click, EventArgs eClick) { System.Windows.Application.Current.Shutdown(); notify.Dispose(); }; System.Windows.Forms.MenuItem item2 = new System.Windows.Forms.MenuItem(); menu.MenuItems.Add(item2); item2.Index = 0; item2.Text = "프로그램 설정"; item2.Click += delegate (object click, EventArgs eClick) { this.Close(); }; this.Close(); // 시작시 창 닫음 (아이콘만 띄우기 위함) } catch (Exception ee) { } } private void Notify_DoubleClick(object sender, EventArgs e) { this.Show(); this.WindowState = WindowState.Normal; this.Visibility = Visibility.Visible; // notify.Visible = false; // 트레이아이콘 숨기기 } // X 버튼으로 종료 시를 위한 오버라이드 protected override void OnClosing(System.ComponentModel.CancelEventArgs e) { e.Cancel = true; this.Hide(); base.OnClosing(e); } } } | cs |
참고자료 : https://blog.naver.com/ljy5745/220461261949
728x90
반응형