Comment afficher du texte dans l'icône de la barre d'état système avec l'API win32?
-
19-08-2019 - |
Question
Essayer de créer une application pour petit moniteur qui affiche l'utilisation actuelle d'Internet sous forme de pourcentage dans la barre d'état système en C à l'aide de l'API win32.
Vouloir également utiliser un fond de couleur ou un texte de couleur en fonction de la quantité utilisée par rapport au nombre de jours restant dans le mois.
MODIFIER: Pour préciser, je souhaite que l'icône de la barre d'état système soit dynamique. Au fur et à mesure que le pourcentage change, je mets à jour l'icône de la barre d'état système. Vous recherchez une solution qui utilise tout simplement le vieil win32 (c'est-à-dire sans MFC ni WTL).
La solution
Ok, voici ma solution win32:
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;
}
Autres conseils
Par texte, vous entendez " Conseils " ?
En supposant que votre icône se trouve dans la barre d'état système
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);
La barre d'état système accepte uniquement les icônes à afficher, pas le texte.
Pour obtenir un texte affiché ici, vous devez d'abord créer un bitmap de mémoire, dessiner votre texte dessus, puis convertir ce bitmap de mémoire en icône de mémoire et laisser la barre d'état système afficher cette icône.
Exemple de code ci-dessous:
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 );