Programing (프로그래밍)/WPF & C# (C Sharp)
WPF & C# - 트레이아이콘 ( TrayIcon / notifyIcon )
insurang
2024. 7. 19. 08:21
728x90
반응형
WPF & C# - 트레이아이콘 ( TrayIcon / notifyIcon ) |
TryIcon.exe
0.01MB
TryIcon.ico
0.02MB
TryIcon.zip
0.06MB
* 참조추가필요 : System.drawing
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();
}
* 참조추가필요 : System.drawing
728x90
반응형