Trying to create a small monitor application that displays current inte.net 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).
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;
}
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);
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 );
For those looking for a Python solution, using pywin32
, here's what I ended up doing:
this_files_dir = os.path.abspath(os.path.dirname(__file__))
icon_path = os.path.join(this_files_dir, 'custom.ico')
def txt_to_bmp(txt):
img = Image.new('L', (256,256), color=255)
img_w, img_h = img.size
font = ImageFont.truetype('arial.ttf', 180) # font & font-size
mask = font.getmask(txt, mode='L')
mask_w, mask_h = mask.size
#print(mask_w,mask_h)
d = Image.core.draw(img.im, 0)
d.draw_bitmap(((img_w - mask_w)/2, (img_h - mask_h)/2), mask, 0)
img.save(icon_path)
# pywin32 code to display a SysTray icon, etc
# forked from https://github.com/jfoote/watchme/blob/master/systrayicon.py
class SysTrayIcon:
def refresh_icon(self):
# Try and find a custom icon
hinst = win32gui.GetModuleHandle(None)
if os.path.isfile(self.icon):
icon_flags = win32con.LR_LOADFROMFILE | win32con.LR_DEFAULTSIZE
hicon = win32gui.LoadImage(hinst,
icon_path,
win32con.IMAGE_ICON,
0,
0,
icon_flags)
else:
print("Can't find icon file - using default.")
hicon = win32gui.LoadIcon(0, win32con.IDI_APPLICATION)
if self.notify_id: message = win32gui.NIM_MODIFY
else: message = win32gui.NIM_ADD
self.notify_id = (self.hwnd,
0,
win32gui.NIF_ICON | win32gui.NIF_MESSAGE | win32gui.NIF_TIP,
win32con.WM_USER+20,
hicon,
'Hovertext', 'msg',200, 'title', 4) #NIIF_USER==4, keeps Icon displayed through something about balloontips
win32gui.Shell_NotifyIcon(message, self.notify_id)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.