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

qgzx9mmu  于 2022-12-03  发布在  其他
关注(0)|答案(4)|浏览(138)

尝试使用win32 API在C中创建一个小型监视器应用程序,以百分比形式在系统托盘中显示当前的Internet使用情况。
也希望使用彩色背景或彩色文本的基础上有多少是使用相对于天在一个月内离开。

**编辑:**为了澄清我希望系统托盘图标是动态的。随着百分比的变化,我更新系统托盘图标。寻找解决方案,只是使用普通的老win32(即没有MFC或WTL)。

mm9b1k5b

mm9b1k5b1#

下面是我的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;
}
olqngx59

olqngx592#

你说的短信是指"小贴士"吗"?
假设您的图标在系统托盘上

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

dxxyhpgq3#

系统匣只接受要显示的图标,而不接受文字。
要在那里显示文本,您必须首先创建一个内存位图,在上面绘制文本,然后将该内存位图转换为内存图标,并让系统托盘显示该图标。
示例代码如下:

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

bwntbbo34#

对于那些寻找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)

相关问题