I am trying to create an icon that displays a piece of text in the system tray. (Obviously, it will be no longer than a few characters.)
So far I have tried:
#include <tchar.h> #include <Windows.h> #include <Windowsx.h> static HICON CreateIcon(LPCTSTR txt) { HICON hIcon = NULL; HDC hDC = NULL; { HDC hDCScreen = GetDC(NULL); if (hDCScreen != NULL) { __try { hDC = CreateCompatibleDC(hDCScreen); } __finally { ReleaseDC(NULL, hDCScreen); } } } if (hDC != NULL) { __try { HFONT hFont = CreateFontIndirect(&ncm.lfMessageFont); if (hFont != NULL) { __try { SelectFont(hDC, hFont); } __finally { DeleteFont(hFont); } } int width = GetSystemMetrics(SM_CXSMICON), height = GetSystemMetrics(SM_CYSMICON); HBITMAP hBmp = CreateCompatibleBitmap(hDC, width, height); if (hBmp != NULL) { __try { HBITMAP hMonoBmp = CreateCompatibleBitmap(hDC, width, height); if (hMonoBmp != NULL) { __try { RECT rect = { 0, 0, width, height }; HGDIOBJ prev = SelectObject(hDC, hBmp); __try { SetBkMode(hDC, TRANSPARENT); SetTextColor(hDC, RGB(255, 255, 255)); ICONINFO ii = { TRUE, 0, 0, hMonoBmp, hBmp }; int textHeight = DrawText(hDC, txt, _tcslen(txt), &rect, 0); if (textHeight != 0) { hIcon = CreateIconIndirect(&ii); } } __finally { SelectObject(hDC, prev); } } __finally { DeleteObject(hMonoBmp); } } } __finally { DeleteObject(hBmp); } } } __finally { DeleteDC(hDC); } } return hIcon; }
using this code:
static void _tmain(int argc, TCHAR* argv[]) { HICON hIcon = CreateIcon(_T("Hi")); if (hIcon != NULL) { __try { NOTIFYICONDATA nid = { sizeof(nid) }; nid.hWnd = GetConsoleWindow(); BOOL success = Shell_NotifyIcon(NIM_ADD, &nid); if (success) { nid.uFlags = NIF_ICON; nid.hIcon = hIcon; success = Shell_NotifyIcon(NIM_MODIFY, &nid); } } __finally { DestroyIcon(hIcon); } } }
but all I get is a monochrome bitmap that says Hi in white text on a black background. (If I changed RGB(255, 255, 255) bit, say, to RGB(255, 255, 254) , it will turn black, so it will be monochrome.)
Any ideas?
(* Note: I'm not looking for MFC, ATL or any other library solutions, just Win32 / GDI calls.)
Edit:
Here it looks like:
