libnick 2025.1.0
A cross-platform base for native Nickvision applications.
Loading...
Searching...
No Matches
Creating a Context Menu

The Nickvision::Notifications::NotifyMenuItem class provides an easy API for creating context menus for a Nickvision::Notifications::NotifyIcon to use within your Windows app.

Assume we want to create the following context menu for a NotifyIcon:

---------------
| Show Window |
| ----------- |
| Exit |
---------------

Here is the code we could use to accomplish this:

using namespace Nickvision::Notifications;
...
bool m_running{ true };
NotifyIconMenu contextMenu;
contextMenu.addAction("Show Window", [&]()
{
showWindow();
});
contextMenu.addSeparator();
contextMenu.addAction("Exit", [&m_running]()
{
m_running = false;
});
NotifyIcon icon{ GetConsoleWindow(), "", contextMenu };
// The following also works:
// NotifyIcon icon{ GetConsoleWindow(), "" };
// icon.setContextMenu(contextMenu);
...
A menu for a NotifyIcon.
Definition notifyiconmenu.h:115
size_t addSeparator()
Adds a separator to the bottom of the menu.
size_t addAction(const std::string &label, const std::function< void()> &action)
Adds an action item to the bottom of the menu.
An icon for the Windows system tray.
Definition notifyicon.h:45
Definition notificationsenteventargs.h:31