HICON CTrayIcon::Text2Icon(const char* const text,COLORREF color)
{
int cxIcon = GetSystemMetrics(SM_CXSMICON);
int cyIcon = GetSystemMetrics(SM_CYSMICON);
HDC hdcDisplay = GetDC(NULL);
if (!hdcDisplay){
return NULL;
}
HDC hdcXOR = CreateCompatibleDC(hdcDisplay);
if (!hdcXOR){
ReleaseDC(NULL, hdcDisplay);
return NULL;
}
HDC hdcAND = CreateCompatibleDC(hdcDisplay);
if (!hdcAND){
DeleteDC(hdcXOR);
ReleaseDC(NULL, hdcDisplay);
return NULL;
}
ICONINFO iconinfo;
ZeroMemory(&iconinfo,sizeof(ICONINFO));
iconinfo.fIcon = TRUE;
iconinfo.hbmMask = CreateCompatibleBitmap(hdcDisplay,cxIcon, cyIcon);
if (!iconinfo.hbmMask){
DeleteDC(hdcAND);
DeleteDC(hdcXOR);
ReleaseDC(NULL, hdcDisplay);
return NULL;
}
iconinfo.hbmColor = CreateCompatibleBitmap(hdcDisplay, cxIcon, cyIcon);
if (!iconinfo.hbmColor){
DeleteObject(iconinfo.hbmMask);
DeleteDC(hdcAND);
DeleteDC(hdcXOR);
ReleaseDC(NULL, hdcDisplay);
return NULL;
}
ReleaseDC(NULL, hdcDisplay);
RECT rc; rc.left = 0; rc.top = 0; rc.right = cxIcon; rc.bottom = cyIcon;
LOGFONT lf;
ZeroMemory(&lf,sizeof(LOGFONT));
lf.lfCharSet = ANSI_CHARSET;
lf.lfOutPrecision = OUT_DEFAULT_PRECIS;
lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
lf.lfQuality = ANTIALIASED_QUALITY; //DEFAULT_QUALITY;
lf.lfPitchAndFamily = DEFAULT_PITCH;
lf.lfHeight = cyIcon;
lf.lfWidth = (cxIcon / 3)-1;
lf.lfWeight = FW_NORMAL;
strcpy_s(lf.lfFaceName ,_countof(lf.lfFaceName),TEXT("TAHOMA"));
HFONT hfontANDOld = NULL;
HFONT hfontXOROld = NULL;
HFONT hfont = CreateFontIndirect(&lf);
if (hfont){
hfontXOROld = (HFONT)SelectObject(hdcAND, hfont);
hfontANDOld = (HFONT)SelectObject(hdcXOR, hfont);
}
HBITMAP hbmMaskOld = (HBITMAP)SelectObject(hdcAND, iconinfo.hbmMask);
PatBlt(hdcAND, 0, 0, cxIcon, cyIcon, WHITENESS);
int nBkModeANDOld = SetBkMode(hdcAND, TRANSPARENT);
DrawText(hdcAND, text, -1, &rc, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
SetBkMode(hdcAND, nBkModeANDOld);
if (hfontANDOld)
SelectObject(hdcAND, hfontANDOld);
HBITMAP hbmColorOld = (HBITMAP)SelectObject(hdcXOR, iconinfo.hbmColor);
PatBlt(hdcXOR, 0, 0, cxIcon, cyIcon, BLACKNESS);
int nBkModeXOROld = SetBkMode(hdcXOR, TRANSPARENT);
COLORREF rgbOld = SetTextColor(hdcXOR, color);
DrawText(hdcXOR, text, -1, &rc, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
SetBkMode(hdcXOR, nBkModeXOROld);
SetTextColor(hdcXOR, rgbOld);
if (hfontXOROld)
SelectObject(hdcXOR, hfontXOROld);
if (hbmMaskOld)
SelectObject(hdcAND, hbmMaskOld);
if (hbmColorOld)
SelectObject(hdcXOR, hbmColorOld);
if (hfont)
DeleteObject(hfont);
DeleteDC(hdcAND);
DeleteDC(hdcXOR);
HICON hIcon = CreateIconIndirect(&iconinfo);
DeleteObject(iconinfo.hbmColor);
DeleteObject(iconinfo.hbmMask);
return hIcon;
}
{
int cxIcon = GetSystemMetrics(SM_CXSMICON);
int cyIcon = GetSystemMetrics(SM_CYSMICON);
HDC hdcDisplay = GetDC(NULL);
if (!hdcDisplay){
return NULL;
}
HDC hdcXOR = CreateCompatibleDC(hdcDisplay);
if (!hdcXOR){
ReleaseDC(NULL, hdcDisplay);
return NULL;
}
HDC hdcAND = CreateCompatibleDC(hdcDisplay);
if (!hdcAND){
DeleteDC(hdcXOR);
ReleaseDC(NULL, hdcDisplay);
return NULL;
}
ICONINFO iconinfo;
ZeroMemory(&iconinfo,sizeof(ICONINFO));
iconinfo.fIcon = TRUE;
iconinfo.hbmMask = CreateCompatibleBitmap(hdcDisplay,cxIcon, cyIcon);
if (!iconinfo.hbmMask){
DeleteDC(hdcAND);
DeleteDC(hdcXOR);
ReleaseDC(NULL, hdcDisplay);
return NULL;
}
iconinfo.hbmColor = CreateCompatibleBitmap(hdcDisplay, cxIcon, cyIcon);
if (!iconinfo.hbmColor){
DeleteObject(iconinfo.hbmMask);
DeleteDC(hdcAND);
DeleteDC(hdcXOR);
ReleaseDC(NULL, hdcDisplay);
return NULL;
}
ReleaseDC(NULL, hdcDisplay);
RECT rc; rc.left = 0; rc.top = 0; rc.right = cxIcon; rc.bottom = cyIcon;
LOGFONT lf;
ZeroMemory(&lf,sizeof(LOGFONT));
lf.lfCharSet = ANSI_CHARSET;
lf.lfOutPrecision = OUT_DEFAULT_PRECIS;
lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
lf.lfQuality = ANTIALIASED_QUALITY; //DEFAULT_QUALITY;
lf.lfPitchAndFamily = DEFAULT_PITCH;
lf.lfHeight = cyIcon;
lf.lfWidth = (cxIcon / 3)-1;
lf.lfWeight = FW_NORMAL;
strcpy_s(lf.lfFaceName ,_countof(lf.lfFaceName),TEXT("TAHOMA"));
HFONT hfontANDOld = NULL;
HFONT hfontXOROld = NULL;
HFONT hfont = CreateFontIndirect(&lf);
if (hfont){
hfontXOROld = (HFONT)SelectObject(hdcAND, hfont);
hfontANDOld = (HFONT)SelectObject(hdcXOR, hfont);
}
HBITMAP hbmMaskOld = (HBITMAP)SelectObject(hdcAND, iconinfo.hbmMask);
PatBlt(hdcAND, 0, 0, cxIcon, cyIcon, WHITENESS);
int nBkModeANDOld = SetBkMode(hdcAND, TRANSPARENT);
DrawText(hdcAND, text, -1, &rc, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
SetBkMode(hdcAND, nBkModeANDOld);
if (hfontANDOld)
SelectObject(hdcAND, hfontANDOld);
HBITMAP hbmColorOld = (HBITMAP)SelectObject(hdcXOR, iconinfo.hbmColor);
PatBlt(hdcXOR, 0, 0, cxIcon, cyIcon, BLACKNESS);
int nBkModeXOROld = SetBkMode(hdcXOR, TRANSPARENT);
COLORREF rgbOld = SetTextColor(hdcXOR, color);
DrawText(hdcXOR, text, -1, &rc, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
SetBkMode(hdcXOR, nBkModeXOROld);
SetTextColor(hdcXOR, rgbOld);
if (hfontXOROld)
SelectObject(hdcXOR, hfontXOROld);
if (hbmMaskOld)
SelectObject(hdcAND, hbmMaskOld);
if (hbmColorOld)
SelectObject(hdcXOR, hbmColorOld);
if (hfont)
DeleteObject(hfont);
DeleteDC(hdcAND);
DeleteDC(hdcXOR);
HICON hIcon = CreateIconIndirect(&iconinfo);
DeleteObject(iconinfo.hbmColor);
DeleteObject(iconinfo.hbmMask);
return hIcon;
}
No comments:
Post a Comment