WPF 라고 해서 뭔가 특별하거나. 윈도우 폼과 전혀 다르거나 하지 않다!
윈도우 응용프로그램의 NotifyIcon을 그대로 사용한다.
그래서 WPF에서는 System.Windows.Form.dll 을 참조 하고 해당 dll 내의 클레스를 사용하면 되는 것이다.
먼저 코드를 보면 먼가 이해가 더 빠를 것 같다.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace WpfApplication1
{
///
/// CartList.xaml에 대한 상호 작용 논리
///
public partial class CartList : Window
{
public CartList()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
try
{
System.Windows.Forms.ContextMenu menu = new System.Windows.Forms.ContextMenu();
System.Windows.Forms.MenuItem item1 = new System.Windows.Forms.MenuItem();
menu.MenuItems.Add(item1);
item1.Index = 0;
item1.Text = "E&xit";
item1.Click +=
delegate(object click, EventArgs eClick)
{
this.Close();
};
System.Windows.Forms.NotifyIcon notify = new System.Windows.Forms.NotifyIcon();
notify.Icon = new System.Drawing.Icon(@"Icon\Angeleyes-Mark.ico");
notify.Visible = true;
notify.DoubleClick +=
delegate(object senders, EventArgs args)
{
this.Show();
this.WindowState = WindowState.Normal;
};
notify.ContextMenu = menu;
notify.Text = "Test";
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
protected override void OnStateChanged(EventArgs e)
{
if (WindowState.Minimized.Equals(WindowState))
{
this.Hide();
}
base.OnStateChanged(e);
}
}
}
테스트 하느라 window_loaded 이벤트를 만들어 NotifyIcon을 생성하였으나여기에 NotifyIcon을 생성하지 않고 생성자에서 생성하는 방식으로 변경하여도 무관하다.
위와 같이 ContextMenu도 생성하고 입력하였다.
어려운 부분은 하나도 없다~
윈도우 폼에서 트레이 아이콘을 한번이라도 만들어 보았다면 말이다.
아~
그리고 Click 과 같은 이벤트 헨들러를 delegate로 처리 하지 않고 자동 생성 되는 코드를 이용해도 된다~
이런 것도 있구나 라고 봐주시면 될 것 같다~
그리고 icon 의 경우.
폴더를 하나 생성하고 그 안에 아이콘 파일을 입력하였으며, 해당 아이콘 파일의 속성에서 빌드작업을
"포함 리소스"로 변경 후 출력 디렉토리로 복사 부분을 "항상 복사 혹은 변경된 내용만 복사"로 변경하여
빌드시 리소스로 포함되게 하여야 ContextMenu.Icon 생성시 에러가 발생되지 않는다.~
그럼 즐거운 코딩이 되시길~