Fog Creek Software
Discussion Board




Generating Windows Icons

I'm building a little application to show the current outdoor temperature in the system tray on Windows.

(Currently 104 F in Australia today!)

I am right in thinking that you can't directly display text in the system tray but can only use icons instead?

If so does anyone know of a way or a library of windows icons so I don't have to make an icon for all possible temperatures?

Matthew Lock
Sunday, February 09, 2003

I've made a similiar application to display the (very) local temperature in the system tray.

Here are some (slightly modified) function I've created for this:

HICON PaintIcon(double fTemp, COLORREF colorref) {
    HDC hScreenDC,hMemDC;
    HICON hIcon;
    ICONINFO IconInfo;
    HGDIOBJ hOldBM;
    char pszTemp[64];

    sprintf(pszTemp,"%.0f°",fabs(fTemp));

    hScreenDC=GetDC(NULL);
    hMemDC=CreateCompatibleDC(hScreenDC);

    IconInfo.hbmColor=CreateCompatibleBitmap(hScreenDC,16,16);
    IconInfo.hbmMask=CreateCompatibleBitmap(hMemDC,16,16);
    IconInfo.fIcon=TRUE;

    ReleaseDC(NULL,hScreenDC);

    //draw the icon mask, to get correct transparancy
    hOldBM=SelectObject(hMemDC,IconInfo.hbmMask);
    PatBlt(hMemDC,0,0,16,16,WHITENESS);
    DisplayText(hMemDC, 0x0, pszBlaat);


    //draw the temp in the correct colour.
    SelectObject(hMemDC,IconInfo.hbmColor);
    PatBlt(hMemDC,0,0,16,16,BLACKNESS);
    DisplayText(hMemDC, colorref, pszBlaat);    

    SelectObject(hMemDC,hOldBM);
    DeleteDC(hMemDC);

    hIcon=CreateIconIndirect(&IconInfo);

    DeleteObject(IconInfo.hbmColor);
    DeleteObject(IconInfo.hbmMask);

    return hIcon;
}

void DisplayText(HDC hDC,COLORREF colorref,char *pszText) {
    SelectObject(hDC,hIconFont);
    SetTextColor(hDC,colorref);
    SetTextAlign(hDC,TA_CENTER);
    SetBkMode(hDC,TRANSPARENT);
    TextOut(hDC,8,-1,pszText,strlen(pszText));
}

and you'll need

hIconFont=CreateFont(14,0,0,0,FW_NORMAL,FALSE,0,0,DEFAULT_CHARSET,0,0,0,0,"Arial");

somewhere.

Use PaintIcon to get a handle to an icon you can use to draw the systray-icon. Don't forget to destroy it with DestroyIcon() after sending in to the systray.

HTH,

Randy

Randy Simons
Sunday, February 09, 2003

If you are doing it in .Net, here is a project I did that puts the number of new mail messages in the system tray.  You should have no trouble adapting it to your needs.

http://www.c-sharpcorner.com/Code/2003/Jan/MailChecker1.0.asp

Matt Watson
Monday, February 10, 2003

*  Recent Topics

*  Fog Creek Home