728x90
반응형
WPF & C# - 트레이아이콘 ( TrayIcon / notifyIcon ) |
MainWindow.xaml.cs
<Grid>
</Grid>
MainWindow.cs
private TrayIconManager _trayIconManager;
public MainWindow()
{
InitializeComponent();
_trayIconManager = new TrayIconManager();
}
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
// Prevent the window from actually closing
e.Cancel = true;
this.Hide();
}
protected override void OnClosed(EventArgs e)
{
this.Hide();
_trayIconManager.Dispose();
base.OnClosed(e);
}
TrayIconManager.cs
private NotifyIcon _notifyIcon;
public TrayIconManager()
{
_notifyIcon = new NotifyIcon
{
Icon = new System.Drawing.Icon("TryIcon.ico"),
Visible = true,
Text = "TryIcon"
};
// 이벤트 핸들러 등록
_notifyIcon.MouseDoubleClick += NotifyIcon_MouseDoubleClick;
_notifyIcon.ContextMenu = CreateContextMenu();
}
// 더블클릭
private void NotifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
System.Windows.Application.Current.Dispatcher.Invoke(() =>
{
var mainWindow = System.Windows.Application.Current.MainWindow;
if (mainWindow != null)
{
mainWindow.Show();
mainWindow.WindowState = WindowState.Normal;
mainWindow.Activate();
}
});
}
}
// ContextMenu 추가
private ContextMenu CreateContextMenu()
{
var contextMenu = new ContextMenu();
// MessageBox 창
var exitMenuItem2 = new MenuItem
{
Text = "MessageBox"
};
exitMenuItem2.Click += (sender, args) => System.Windows.MessageBox.Show("aaa");
contextMenu.MenuItems.Add(exitMenuItem2);
// 프로그램 종료
var exitMenuItem = new MenuItem
{
Text = "Exit"
};
exitMenuItem.Click += (sender, args) => System.Windows.Application.Current.Shutdown();
contextMenu.MenuItems.Add(exitMenuItem);
return contextMenu;
}
public void Dispose()
{
_notifyIcon.Dispose();
}
728x90
반응형
'Programing (프로그래밍) > WPF & C# (C Sharp)' 카테고리의 다른 글
WPF - 햄버거 버튼 유니코드 엔터티 ( WPF / HTML) (0) | 2024.08.01 |
---|---|
WPF & C# - 엑셀 읽기 쓰기 ( Microsoft.Office.Interop.Excel ) (0) | 2024.03.03 |
WPF & C# - ASCII or HEX 구분 방법 ( 아스키 ) (0) | 2023.12.30 |
WPF C# HttpClient vs WebRequest 장단점 (0) | 2023.08.20 |
WPF & C# - DataGrid 바탕화면으로 Drag&Drop 하여 파일 복사하기 ( DragnDrop 데이터 그리드 데이타그리드 ) (0) | 2023.08.08 |
WPF & C# - 엑셀없이 엑셀파일 빠르게 읽기 ( XLS, XLSX, ExcelDataReader, EXCEL ) (0) | 2023.03.12 |
[자작] WPF & C# - isIPSCAN( Ping Test Program / 맥주소 / MACaddress / 호스트명 / HostName / 닉네임 / NicName / 핑 테스트 ) (0) | 2022.11.13 |
WPF & C# - Array Index Check ( 배열 인덱스 내외 여부 체크 확인 ) (0) | 2022.10.15 |