I'm using the following procedure in order to get the small icon of an application window:
Code:
HICON GetWindowIcon
(
HWND hwnd
)
{
LRESULT lResult;
HICON hIcon;
hIcon = NULL;
lResult = SendMessageTimeout(hwnd, WM_GETICON, ICON_SMALL, 0, SMTO_ABORTIFHUNG|SMTO_BLOCK, 1000, (DWORD_PTR *)&hIcon);
if (lResult==0) {
hIcon = NULL;
}
if (hIcon == NULL) {
hIcon = (HICON)GetClassLong(hwnd, GCL_HICONSM);
}
if (hIcon == NULL) {
hIcon = LoadIcon(NULL, IDI_APPLICATION);
}
return hIcon;
}
Sometimes, with some application, SendMessageTimeout returns a hIcon which is different to NULL but the icon returned doesn't seem to be a small one, although I'm using the ICON_SMALL constant. Is it normal?
I'm also using the following procedure in order to get the array of bytes corresponding to the icon image:
Code:
int GetIconBits
(
HICON hIcon,
int iIconSize,
long* bits,
int* iIconBitsLength
)
{
ICONINFO iconInfo;
HDC dc;
BITMAPINFO bmi;
int iBits;
long maskBits[4096];
int i;
int iMask;
if(!GetIconInfo((HICON)hIcon, &iconInfo))
return -1;
if((dc = GetDC(NULL)) == NULL) {
DeleteObject(iconInfo.hbmColor);
DeleteObject(iconInfo.hbmMask);
return -1;
}
memset(&bmi, 0, sizeof(BITMAPINFO));
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bmi.bmiHeader.biWidth = iIconSize;
bmi.bmiHeader.biHeight = -iIconSize;
bmi.bmiHeader.biPlanes = 1;
bmi.bmiHeader.biBitCount = 32;
bmi.bmiHeader.biCompression = BI_RGB;
iBits = iIconSize * iIconSize * 32 / 8;
GetDIBits(dc, iconInfo.hbmColor, 0, iIconSize, bits, &bmi, DIB_RGB_COLORS);
GetDIBits(dc, iconInfo.hbmMask, 0, iIconSize, maskBits, &bmi, DIB_RGB_COLORS);
for(i = 0; i < iBits; i++) {
bits[i] = bits[i] | ((maskBits[i] != 0) ? 0 : 0xff000000);
}
(*iIconBitsLength) = iBits;
ReleaseDC(NULL, dc);
DeleteObject(iconInfo.hbmColor);
DeleteObject(iconInfo.hbmMask);
return 0;
}
There is a problem with 32-bit anti-aliased icons, which are typical in Windows XP. The images represented by the arrays of bytes returned by this procedure have a sort of dark area around. I guess this happens because the procedure doesn't handle correctly the mask information or something like that, I don't know. To tell the truth the procedure isn't mine, I've copied it. Does anybody know how I must treat the information return by the GetDIBits calls in order get a correct array of bytes representing the image?
Finally, which is the real format of the array of bytes returned by this procedure? I mean, if I'm working with 32-bit icons, I need 8 bytes in order to represent each pixel. The image is supposed to be in a RGB format so, how are distributed the bytes? Is it something like this?
Code:
bits[0] bits[1] bits[2] bits[3] -> (0,0) pixel
bits[4] bits[5] bits[6] bits[7] -> (0,1) pixel
...
If this is correct, how are the RGB components mapped? Is it something like this?:
Code:
A R G B
bits[0] bits[1] bits[2] bits[3] -> (0,0) pixel
bits[4] bits[5] bits[6] bits[7] -> (0,1) pixel
...
Thanks for your help.