如何使用win32 API在系统托盘图标中显示文本?

11

尝试使用win32 API在C语言中创建一个小型监视应用程序,该程序在系统托盘中以百分比显示当前的互联网使用情况。

另外,希望根据本月剩余天数与使用量之间的关系,使用颜色背景或颜色文本。

编辑:澄清一下,我想让系统托盘图标是动态的。随着百分比的变化,我会更新系统托盘图标。寻找只使用纯win32(即没有MFC或WTL)的解决方案。


1
参见:http://support.microsoft.com/kb/318876 - Paul
4个回答

8

好的,这里是我的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;
}

2

您提到的文本是指“提示”吗?

假设您的图标已在系统托盘上。

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

1
不是提示工具。我想让图标成为数字。 - grom

1
系统托盘只接受图标显示,不接受文本。
如果要在那里显示文本,您必须先创建一个内存位图,将文本绘制在上面,然后将该内存位图转换为内存图标,并让系统托盘显示该图标。
以下是示例代码:
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 );  

1
现在如果我能只使用纯Win32而不是MFC来实现这个就好了。 - grom
1
替换"CDC dcMem"为"HDC hdcMem"不应该成为问题,然后使用API而不是方法。 API与MFC方法具有相同的名称,例如:dcMem.CreateCompatibleDC() => ::CreateCompatibleDC(hdcMem, NULL)。 - Stefan

0

对于那些寻找Python解决方案的人来说,使用pywin32,以下就是我的解决方案:

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)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接