void Label::DrawImage(HDC hDC, int x, int y) { int w = 0; int h = 0; // Get the image's size GetLSBitmapSize(mImage, &w, &h); HDC imageDC = CreateCompatibleDC(hDC); HBITMAP oldImageBitmap = SelectBitmap(imageDC, mImage); // Draw the image transparently TransparentBltLS(hDC, x, y, w, h, imageDC, 0, 0, RGB(255, 0, 255)); SelectBitmap(imageDC, oldImageBitmap); DeleteDC(imageDC); }
void Label::Paint(HDC hDC) { // Figure out how much space we have to work with int x = mPaddingLeft; int y = mPaddingTop; int w = mWidth - mPaddingLeft - mPaddingRight; int h = mHeight - mPaddingTop - mPaddingBottom; // Compute the size of the image int wImage = 0; int hImage = 0; if (mImage) { GetLSBitmapSize(mImage, &wImage, &hImage); } // Compute the amount of space between the image and the text int wGap = 0; int hGap = 0; if (mImage && StrLen(mText) > 0) { wGap = mImageTextGap; hGap = mImageTextGap; } // Compute the size of the text int wText = 0; int hText = 0; if (StrLen(mText) > 0) { GetTextExtent(hDC, &wText, &hText); } // Compute the size of the content area (image and text) int wContent = 0; int hContent = 0; if (mImagePosition == IMAGE_POSITION_LEFT || mImagePosition == IMAGE_POSITION_RIGHT) { wContent = wImage + wGap + wText; hContent = max(hImage, hText); } else { wContent = max(wImage, wText); hContent = hImage + hGap + hText; } // Align the content area horizontally int xContent = 0; switch (mAlign) { case ALIGN_LEFT: xContent = x; break; case ALIGN_CENTER: xContent = x + (w - wContent) / 2; break; case ALIGN_RIGHT: xContent = x + (w - wContent); break; } // Align the content area vertically int yContent = 0; switch (mVerticalAlign) { case VERTICAL_ALIGN_TOP: yContent = y; break; case VERTICAL_ALIGN_CENTER: yContent = y + (h - hContent) / 2; break; case VERTICAL_ALIGN_BOTTOM: yContent = y + (h - hContent); break; } // Place the image and the text int xImage = 0; int yImage = 0; int xText = 0; int yText = 0; switch (mImagePosition) { case IMAGE_POSITION_LEFT: xImage = xContent; yImage = yContent + (hContent - hImage) / 2; xText = xContent + wImage + wGap; yText = yContent + (hContent - hText) / 2; break; case IMAGE_POSITION_TOP: xImage = xContent + (wContent - wImage) / 2; yImage = yContent; xText = xContent + (wContent - wText) / 2; yText = yContent + hImage + hGap; break; case IMAGE_POSITION_RIGHT: xImage = xContent + wText + wGap; yImage = yContent + (hContent - hImage) / 2; xText = xContent; yText = yContent + (hContent - hText) / 2; break; case IMAGE_POSITION_BOTTOM: xImage = xContent + (wContent - wImage) / 2; yImage = yContent + hText + hGap; xText = xContent + (wContent - wText) / 2; yText = yContent; break; } // Paint the background DrawBackground(hDC); // Clip the content area IntersectClipRect(hDC, x, y, x + w, y + h); // Paint the image if (mImage) { DrawImage(hDC, xImage, yImage); } // Paint the text if (StrLen(mText) > 0) { DrawText(hDC, xText, yText); } }
void Label::DrawBackground(HDC hDC) { if (mBackgroundImage) { int w = 0; int h = 0; // Fill background with an image HDC backgroundDC = CreateCompatibleDC(hDC); HBITMAP oldBackgroundBitmap = SelectBitmap(backgroundDC, mBackgroundImage); GetLSBitmapSize(mBackgroundImage, &w, &h); ScaleEdgeBlt(hDC, 0, 0, mWidth, mHeight, backgroundDC, 0, 0, w, h, mBackgroundImageBorderLeft, mBackgroundImageBorderTop, mBackgroundImageBorderRight, mBackgroundImageBorderBottom, mBackgroundImageTile, SRCCOPY); SelectBitmap(backgroundDC, oldBackgroundBitmap); DeleteDC(backgroundDC); } else { // Fill background with color HBRUSH brush = CreateSolidBrush(mBackgroundColor); HBRUSH oldBrush = SelectBrush(hDC, brush); PatBlt(hDC, 0, 0, mWidth, mHeight, PATCOPY); SelectBrush(hDC, oldBrush); DeleteBrush(brush); // Draw top border if (mBorderTop > 0) { HPEN pen = CreatePen(PS_SOLID, mBorderTop * 2, mBorderColorTop); HPEN oldPen = SelectPen(hDC, pen); MoveToEx(hDC, 0, 0, NULL); LineTo(hDC, mWidth, 0); SelectPen(hDC, oldPen); DeletePen(pen); } // Draw right border if (mBorderRight > 0) { HPEN pen = CreatePen(PS_SOLID, mBorderRight * 2, mBorderColorRight); HPEN oldPen = SelectPen(hDC, pen); MoveToEx(hDC, mWidth, 0, NULL); LineTo(hDC, mWidth, mHeight); SelectPen(hDC, oldPen); DeletePen(pen); } // Draw bottom border if (mBorderBottom > 0) { HPEN pen = CreatePen(PS_SOLID, mBorderBottom * 2, mBorderColorBottom); HPEN oldPen = SelectPen(hDC, pen); MoveToEx(hDC, mWidth, mHeight, NULL); LineTo(hDC, 0, mHeight); SelectPen(hDC, oldPen); DeletePen(pen); } // Draw left border if (mBorderLeft > 0) { HPEN pen = CreatePen(PS_SOLID, mBorderLeft * 2, mBorderColorLeft); HPEN oldPen = SelectPen(hDC, pen); MoveToEx(hDC, 0, mHeight, NULL); LineTo(hDC, 0, 0); SelectPen(hDC, oldPen); DeletePen(pen); } } }
// // HBITMAP LoadLSImage(LPCSTR pszImage, LPCSTR pszFile) // // Takes strings of the form: // File.bmp // .extract // .extract=file.exe[,3] HBITMAP LoadLSImage(LPCSTR pszImage, LPCSTR pszFile) { HBITMAP hbmReturn = NULL; if (pszImage != NULL) { if (_stricmp(pszImage, ".none") != 0) { char szImage[MAX_PATH]; StringCchCopy(szImage, MAX_PATH, pszImage); // Bitmap merging by Thedd // Thedd - pic1.bmp|pic2.bmp merges the images. Works recursively, // so pic1.bmp|.extract=whatever.dll,3|pic2.bmp also works etc... // bitmap merging by grd LPSTR pszSecondImage = strchr(szImage, '|'); if (pszSecondImage) { HDC hdcFirst, hdcSecond, hdcResult; HBITMAP hbmFirst, hbmFirstOld; HBITMAP hbmSecond, hbmSecondOld; HBITMAP hbmResult, hbmResultOld; HBRUSH hbrTransparent; RECT rc; int wdtFirst, hgtFirst; int wdtSecond, hgtSecond; int wdtResult, hgtResult; // get the position after the [|] character *pszSecondImage = '\0'; ++pszSecondImage; // load the two bitmaps hbmFirst = LoadLSImage(szImage, pszFile); hbmSecond = LoadLSImage(pszSecondImage, pszFile); // if the second one is NULL, then there's no merging to do and if (hbmSecond != NULL) { // create mem dcs for the bitmaps hdcFirst = CreateCompatibleDC(NULL); hdcSecond = CreateCompatibleDC(NULL); // select the bitmaps hbmFirstOld = (HBITMAP)SelectObject(hdcFirst, hbmFirst); hbmSecondOld = (HBITMAP)SelectObject(hdcSecond, hbmSecond); // get the bitmap sizes.. GetLSBitmapSize(hbmFirst, &wdtFirst, &hgtFirst); GetLSBitmapSize(hbmSecond, &wdtSecond, &hgtSecond); // in earlier version of bitmap merge, those were painted on // to each other now let's paint both images to a new one // and we support different sized images!! therefore: wdtResult = std::max(wdtFirst, wdtSecond); hgtResult = std::max(hgtFirst, hgtSecond); // create another dc, compatible with second dc hdcResult = CreateCompatibleDC(hdcSecond); // create a new bitmap for the new dc and select it hbmResult = CreateCompatibleBitmap(hdcSecond, wdtResult, hgtResult); hbmResultOld = (HBITMAP)SelectObject(hdcResult, hbmResult); rc.top = 0; rc.left = 0; rc.right = wdtResult; rc.bottom = hgtResult; // paint the background in transparent color... hbrTransparent = CreateSolidBrush(RGB(255, 0, 255)); FillRect(hdcResult, &rc, hbrTransparent); DeleteObject(hbrTransparent); // first "standard blit" the second image into the new one: BitBlt(hdcResult, (wdtResult - wdtSecond) / 2, (hgtResult - hgtSecond) / 2, wdtSecond, hgtSecond, hdcSecond, 0, 0, SRCCOPY); // Secondly "tranparent blit" the first image over the // second one Since TransparentBltLS double buffers the // painting to reduce flicker and we are using only memory // DC's in this function, we will just call // TransparentBltLSWorker and shave off a few BitBlt calls TransparentBltLSWorker(hdcResult, wdtFirst, hgtFirst, hdcFirst, 0, 0, RGB(255, 0, 255)); // deselect the bitmap from the dc and delete the dc to get // the image SelectObject(hdcResult, hbmResultOld); DeleteDC(hdcResult); // delete all used objects SelectObject(hdcFirst, hbmFirstOld); DeleteObject(hbmFirst); DeleteDC(hdcFirst); SelectObject(hdcSecond, hbmSecondOld); DeleteObject(hbmSecond); DeleteDC(hdcSecond); hbmReturn = hbmResult; } else { hbmReturn = hbmFirst; } } else { if (!_strnicmp(szImage, ".extract", 8 /*strlen(".extract")*/)) { HICON hIcon = NULL; hIcon = LoadLSIcon(szImage, pszFile); if (hIcon) { hbmReturn = BitmapFromIcon(hIcon); DestroyIcon(hIcon); } } else { // Append the image name to the LiteStep image path and // attempt to load the image. char szExpandedImage[MAX_PATH]; VarExpansionEx(szExpandedImage, szImage, MAX_PATH); LSGetImagePath(szImage, MAX_PATH); PathAppend(szImage, szExpandedImage); if (PathMatchSpec(szImage, "*.png")) { hbmReturn = LoadFromPNG(szImage); } else { hbmReturn = (HBITMAP)LoadImage( NULL, szImage, IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR | LR_LOADFROMFILE); } // If that fails, treat the image as a fully qualified path // and try loading it if (hbmReturn == NULL) { if (PathMatchSpec(szExpandedImage, "*.png")) { hbmReturn = LoadFromPNG(szExpandedImage); } else { hbmReturn = (HBITMAP)LoadImage( NULL, szExpandedImage, IMAGE_BITMAP, 0, 0, LR_DEFAULTCOLOR | LR_LOADFROMFILE); } } } } } } return hbmReturn; }