Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display text in system tray icon with win32 API?

Trying to create a small monitor application that displays current internet usage as percentage in system tray in C using win32 API.

Also wanting to use colour background or colour text based on how much is used relative to days left in month.

EDIT: To clarify I am wanting the system tray icon to be dynamic. As the percentage changes I update the system tray icon. Looking for solution that uses just plain old win32 (ie. No MFC or WTL).

like image 613
grom Avatar asked Jan 19 '09 09:01

grom


People also ask

How do I display system tray icons?

Press and hold or right-click any empty space on the taskbar and select Taskbar settings. Under Taskbar corner icons: Select On for any icons you want to see on the taskbar. Select Off for any icons you don't want to see on the taskbar.

What is displayed on the system tray?

The notification area (also called the "system tray") is located in the Windows Taskbar, usually at the bottom right corner. It contains miniature icons for easy access to system functions such as antivirus settings, printer, modem, sound volume, battery status, and more.

How do I check the system tray?

Step 1 − Go to the SETTINGS window and choose System. Step 2 − In the SYSTEM window, select Notifications & actions. Here you can select the option that reads “Select which icons appear on the taskbar”.

What is the function of notification area in taskbar?

The notification area is a part of the taskbar that provides a temporary source for notifications and status. It can also be used to display icons for system and program features that are not on the desktop.


3 Answers

Okay here is my win32 solution:

HICON CreateSmallIcon( HWND hWnd )
{
    static TCHAR *szText = TEXT ( "100" );
    HDC hdc, hdcMem;
    HBITMAP hBitmap = NULL;
    HBITMAP hOldBitMap = NULL;
    HBITMAP hBitmapMask = NULL;
    ICONINFO iconInfo;
    HFONT hFont;
    HICON hIcon;

    hdc = GetDC ( hWnd );
    hdcMem = CreateCompatibleDC ( hdc );
    hBitmap = CreateCompatibleBitmap ( hdc, 16, 16 );
    hBitmapMask = CreateCompatibleBitmap ( hdc, 16, 16 );
    ReleaseDC ( hWnd, hdc );
    hOldBitMap = (HBITMAP) SelectObject ( hdcMem, hBitmap );
    PatBlt ( hdcMem, 0, 0, 16, 16, WHITENESS );

    // Draw percentage
    hFont = CreateFont (12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
                    TEXT ("Arial"));
    hFont = (HFONT) SelectObject ( hdcMem, hFont );
    TextOut ( hdcMem, 0, 0, szText, lstrlen (szText) );

    SelectObject ( hdc, hOldBitMap );
    hOldBitMap = NULL;

    iconInfo.fIcon = TRUE;
    iconInfo.xHotspot = 0;
    iconInfo.yHotspot = 0;
    iconInfo.hbmMask = hBitmapMask;
    iconInfo.hbmColor = hBitmap;

    hIcon = CreateIconIndirect ( &iconInfo );

    DeleteObject ( SelectObject ( hdcMem, hFont ) );
    DeleteDC ( hdcMem );
    DeleteDC ( hdc );
    DeleteObject ( hBitmap );
    DeleteObject ( hBitmapMask );

    return hIcon;
}
like image 81
grom Avatar answered Oct 02 '22 20:10

grom


By text you mean "Tips" ?

Assuming you have your icon on the System Tray

NOTIFYICONDATA _stNotifyIconData;

// For a simple Tip
_stNotifyIconData.uFlags = NIF_TIP;
strcpy_s(_stNotifyIconData.szTip, "Little Tip"); // Copy Tip    
Shell_NotifyIcon(NIM_MODIFY, &_stNotifyIconData);

// For a Ballon Tip
_stNotifyIconData.uFlags = NIF_INFO;
strcpy_s(_stNotifyIconData.szInfoTitle, "Title of the Ballon"); // Title
strcpy_s(_stNotifyIconData.szInfo, "Text..." ); // Copy Tip
_stNotifyIconData.uTimeout = 3000;  // 3 Seconds
_stNotifyIconData.dwInfoFlags = NIIF_INFO;

Shell_NotifyIcon(NIM_MODIFY, &_stNotifyIconData);
like image 45
João Augusto Avatar answered Oct 02 '22 19:10

João Augusto


The system tray only accepts icons to show, not text.

To get a text shown there, you have to first create a memory bitmap, draw your text on it, then convert that memory bitmap to a memory icon and have the system tray show that icon.

Example code below:

CDC dcMem;
dcMem.CreateCompatibleDC(NULL);

CBitmap* pOld = dcMem.SelectObject( &m_bmpIcon );

CBrush back( RGB(0,0,0) );
dcMem.FillRect( CRect(0,0,16,16), &back );

CBrush brush( COLORDOWN );
dcMem.FillRect( rcRecv, &brush );

dcMem.SelectObject( pOld );

HICON hIcon = CreateIconIndirect( &m_TaskBarIconInfo );  
like image 28
Stefan Avatar answered Oct 02 '22 18:10

Stefan