Load a bitmap and palette from a resource file (Views: 32)
Problem/Question/Abstract: Load a bitmap and palette from a resource file Answer: This routine loads a bitmap from a resource file and gets its palette and bitmap handle: procedure LoadBitmapPalette(szTitle: PChar; var hPal: HPalette; var hBitmap: HBitmap); type TMLogPalette = record palVersion: Word; palNumEntries: Word; palPalEntry: array[0..255] of TPaletteEntry; end; var hOldPal: HPALETTE; pPal: TMLogPalette; dc: hDC; { device context for palette } hRes: THandle; { resource handle for bitmap } pBits: PBITMAPINFOHEADER; { pointer to bitmapinfoheader in resource } pRgb: ^TRGBQUAD; { Zeiger auf DIB-Palettendaten } i: integer; Data: PChar; begin hRes := FindResource(hInstance, szTitle, RT_BITMAP); if hRes <> 0 then hRes := LoadResource(hInstance, hRes); if hRes <> 0 then begin pBits := PBITMAPINFOHEADER(LockResource(hRes)); { so: having 16 colors, we do not need a palette (LoadBitmap is allright) > 256 colors: no palette is needed ("hi-/direct/true color") } if (pBits^.biBitCount <= 8) and { only for <= 256 color bitmaps } (pBits^.biSize = sizeof(TBITMAPINFOHEADER)) { only Windows-bitmaps, not OS/2 } then begin pRgb := pointer(pBits); inc(PChar(pRgb), pBits^.biSize); pPal.palNumEntries := 1 shl pBits^.biBitCount; pPal.palVersion := $300; for i := 0 to pPal.palNumEntries - 1 do begin pPal.palPalEntry[i].peRed := pRgb^.rgbRed; pPal.palPalEntry[i].peGreen := pRgb^.rgbGreen; pPal.palPalEntry[i].peBlue := pRgb^.rgbBlue; pPal.palPalEntry[i].peFlags := 0 {PC_NOCOLLAPSE}; inc(PChar(pRgb), 4); end; hPal := CreatePalette(PLogPalette(@pPal)^); DC := GetDC(0); hOldPal := SelectPalette(DC, hPal, false); RealizePalette(DC); with pBits^ do begin biClrImportant := 0; biClrUsed := 0; end; Data := pointer(pBits); inc(Data, pBits^.biSize + pPal.palNumEntries * sizeof(TRGBQUAD)); hBitmap := CreateDIBitmap(DC, pBits^, CBM_INIT, Data, PBitmapInfo(pBits)^, dib_RGB_Colors); SelectPalette(DC, hOldPal, FALSE); ReleaseDC(0, DC); end; UnlockResource(hRes); end else hBitmap := LoadBitmap(hInstance, szTitle); end; |