如何显示系统托盘图标与Win32 API的文字? [英] How to display text in system tray icon with win32 API?

查看:197
本文介绍了如何显示系统托盘图标与Win32 API的文字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图创建一个显示当前互联网的使用在使用Win32 API使用C系统托盘中的百分比小的监视器应用程序。

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.

编辑::要澄清,我想系统托盘图标是动态的。由于变化百分比我更新系统托盘图标。寻找使用只是普通的老win32的解决方案(即无MFC或WTL)。

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).

推荐答案

好吧,这里是我的Win32的解决方案:

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;
}

这篇关于如何显示系统托盘图标与Win32 API的文字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆