1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
bool
AddImage::SetImage(LPCTSTR fileName, int alpha)
{
CxImage img;
img.Load(fileName, CXIMAGE_FORMAT_PNG);
RECT rcWnd;
GetWindowRect(&rcWnd);
POINT ptWindowScreenPosition = {rcWnd.left, rcWnd.top}, ptSrc = {0, 0};
SIZE size = {img.GetWidth(), img.GetHeight()};
CDC* dcScreen = GetDC();
// Création d'un dc mémoire de la taille de l'image, et dans lequel on dessine l'image
HDC hmemdc = CreateCompatibleDC(dcScreen->GetSafeHdc());
HBITMAP hbmp = CreateCompatibleBitmap(dcScreen->GetSafeHdc(), size.cx, size.cy);
HGDIOBJ oldbmp = SelectObject(hmemdc, hbmp);
img.Draw(hmemdc);
BITMAP bmp;
GetObject(hbmp, sizeof bmp, &bmp);
BITMAPINFO bi;
memset(&bi, 0, sizeof bi);
bi.bmiHeader.biSize = sizeof bi.bmiHeader;
bi.bmiHeader.biWidth = bmp.bmWidth;
bi.bmiHeader.biHeight = bmp.bmHeight;
bi.bmiHeader.biBitCount= bmp.bmBitsPixel;
bi.bmiHeader.biPlanes = bmp.bmPlanes;
bi.bmiHeader.biCompression = BI_RGB;
int nbbytes = bmp.bmBitsPixel * bmp.bmWidth * bmp.bmHeight / 8;
LPBYTE lpBits = new BYTE[nbbytes];
// Récupération des bits de l'image
GetDIBits(hmemdc, hbmp, 0, bmp.bmHeight, lpBits, &bi, DIB_RGB_COLORS);
//ici on gère la transparence des pixels un par un
LPBYTE pAlpha = img.AlphaGetPointer();
for(int i = 0; i < nbbytes; i += 4) lpBits[i + 3] = *pAlpha++;
SetDIBits(hmemdc, hbmp, 0, bmp.bmHeight, lpBits, &bi, DIB_RGB_COLORS);
delete[] lpBits;
BLENDFUNCTION bf = {AC_SRC_OVER, 0, alpha, AC_SRC_ALPHA};
UpdateLayeredWindow(GetSafeHwnd(), dcScreen->GetSafeHdc(), &ptWindowScreenPosition, &size, hmemdc, &ptSrc, 0, &bf, ULW_ALPHA);
SelectObject(hmemdc, oldbmp);
DeleteObject(hbmp);
DeleteDC(hmemdc);
ReleaseDC(dcScreen);
return true;
} |