streetfighter 2
New Member
- Joined
- Jul 26, 2010
- Messages
- 1,655 (0.33/day)
- Location
- Philly
It's a nice coincidence that I should fall into a GDI trap just when ctrain is showing off his skills.
So I'm trying to create a tray icon (16x16) that contains whatever one or two digit number I want to throw at it. My requirements are that the icon appear as only plain text with a transparent background. My code works, but leaks like it was earning money by the byte .
Here's the code:
I am calling DestroyIcon() on the handle returned from CreateIntIcon() when I'm done with it. I'm absolutely sure that the leak is in the terrible function I wrote.
Any help rewriting the function or fixing the leak would be greatly appreciated.
For reference I stole code from:
http://support.microsoft.com/kb/318876
http://www.winprog.org/tutorial/transparency.html
So I'm trying to create a tray icon (16x16) that contains whatever one or two digit number I want to throw at it. My requirements are that the icon appear as only plain text with a transparent background. My code works, but leaks like it was earning money by the byte .
Here's the code:
Code:
HICON CreateIntIcon(int temp)
{
HDC hMemDC, hdc, hdcMem, hdcMem2;
DWORD dwWidth, dwHeight;
HBITMAP hBitmap, hbmMask;
BITMAP bm;
void *lpBits;
DWORD x,y;
HICON hAlphaCursor = NULL;
TCHAR buf[3];
_stprintf(buf,"%d",temp);
int size = _tcslen(buf);
dwWidth = 16; // width of cursor
dwHeight = 16; // height of cursor
hdc = GetDC(hDlg);
hBitmap = CreateCompatibleBitmap ( hdc, dwWidth, dwHeight );
hMemDC = CreateCompatibleDC(hdc);
ReleaseDC(NULL,hdc);
hbmMask = (HBITMAP)SelectObject(hMemDC, hBitmap);
PatBlt(hMemDC,0,0,dwWidth,dwHeight,WHITENESS);
SetTextColor(hMemDC,RGB(0,0,0));
TextOut(hMemDC,0,0,buf,size);
SelectObject(hMemDC, hbmMask);
DeleteDC(hMemDC);
// Create monochrome (1 bit) mask bitmap.
GetObject(hBitmap, sizeof(BITMAP), &bm);
hbmMask = CreateBitmap(bm.bmWidth, bm.bmHeight, 1, 1, NULL);
// Get some HDCs that are compatible with the display driver
hdcMem = CreateCompatibleDC(0);
hdcMem2 = CreateCompatibleDC(0);
SelectObject(hdcMem, hBitmap);
SelectObject(hdcMem2, hbmMask);
// set the transparent color
SetBkColor(hdcMem, RGB(0xFF,0xFF,0xFF));
//Create the mask with some voodoo
BitBlt(hdcMem2, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY);
BitBlt(hdcMem, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem2, 0, 0, SRCINVERT);
//clean up
DeleteDC(hdcMem);
DeleteDC(hdcMem2);
DeleteDC(hdc);
ICONINFO ii;
ii.fIcon = FALSE; // Change fIcon to TRUE to create an alpha icon
ii.xHotspot = 0;
ii.yHotspot = 0;
ii.hbmMask = hbmMask;
ii.hbmColor = hBitmap;
// Create the icon with transparent background
hAlphaCursor = CreateIconIndirect(&ii);
DeleteObject(hBitmap);
DeleteObject(hbmMask);
return hAlphaCursor;
}
Any help rewriting the function or fixing the leak would be greatly appreciated.
For reference I stole code from:
http://support.microsoft.com/kb/318876
http://www.winprog.org/tutorial/transparency.html