17 #pragma comment(lib, "gdi32.lib")
19 #define LR_CREATEDIBSECTION 0
33struct TPhDeleteBitmapHandle
35 void operator()(HBITMAP h)
38 if ( ::GetObjectType(h) == NULL )
40 ASSERT0(
false,
"~CBitmapHandle",
"先に HBITMAPが破棄されてしまっています。" );
42 else if ( ! ::DeleteObject(h) )
44 ASSERT1(
false,
"~CBitmapHandle",
"HBITMAP の破棄に失敗しました。\ncode = %d", ::GetLastError() );
145 CBitmapHandle(UINT uBitmapResourceId,
int cx = 0,
int cy = 0, UINT fuLoad = LR_CREATEDIBSECTION)
160 CBitmapHandle(LPCTSTR lpszFile,
int cx = 0,
int cy = 0, UINT fuLoad = LR_CREATEDIBSECTION)
176 CBitmapHandle(
const CString& strFile,
int cx = 0,
int cy = 0, UINT fuLoad = LR_CREATEDIBSECTION)
192 if ( ::GetObject(*
this,
sizeof(BITMAP), &bm) > 0 )
194 _size.cx = bm.bmWidth;
195 _size.cy = bm.bmHeight;
198 _GetLastError(
"GetObject");
213 if (
IsNull() ) {
return NULL; }
215 HBITMAP h =
static_cast<HBITMAP
>(::CopyImage(*
this, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION));
233 _super::operator=(
static_cast<HBITMAP
>(::CopyImage(hBmp, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION)));
249 bool Draw(HDC hdc,
int x = 0,
int y = 0)
const
252 return Draw(hdc, h, x, y);
263 DWORD
Save(LPCTSTR lpszFileName, WORD bitsPixel = 0)
265 return Save(lpszFileName, *
this, bitsPixel);
284 if ( ! ::GetObject(hbm,
sizeof(BITMAP), &bm) )
288 HDC hdcSrc = ::CreateCompatibleDC(NULL);
289 HDC hdcDst = ::CreateCompatibleDC(NULL);
290 HGDIOBJ hbmOld = ::SelectObject(hdcSrc, hbm);
291 BITMAPINFOHEADER bmi;
293 bmi.biSize =
sizeof(BITMAPINFOHEADER);
294 bmi.biWidth = bm.bmWidth;
295 bmi.biHeight = bm.bmHeight;
298 bmi.biCompression = BI_RGB;
299 HBITMAP hbmNew = ::CreateDIBSection(hdcSrc,
reinterpret_cast<BITMAPINFO*
>(&bmi), DIB_RGB_COLORS,
reinterpret_cast<void **
>(&_pRgb), NULL, 0);
300 HGDIOBJ hbmOld2 = ::SelectObject(hdcDst, hbmNew);
301 ::BitBlt(hdcDst, 0, 0, bm.bmWidth, bm.bmHeight, hdcSrc, 0, 0, SRCCOPY);
302 ::SelectObject(hdcDst, hbmOld2);
303 ::SelectObject(hdcSrc, hbmOld);
306 if ( pHeader != NULL )
324 static HBITMAP
Load(UINT uBitmapResourceId,
int cx = 0,
int cy = 0, UINT fuLoad = LR_CREATEDIBSECTION)
327 LPCTSTR lpId = MAKEINTRESOURCE(uBitmapResourceId);
328 return static_cast<HBITMAP
>(::LoadImage(hIns, lpId, IMAGE_BITMAP, cx, cy, fuLoad));
341 static HBITMAP
Load(LPCTSTR lpszFileName,
int cx = 0,
int cy = 0, UINT fuLoad = LR_CREATEDIBSECTION)
343 if ( lpszFileName == NULL || *lpszFileName == 0 )
349 return static_cast<HBITMAP
>(::LoadImage(hIns, lpszFileName, IMAGE_BITMAP, cx, cy, fuLoad | LR_LOADFROMFILE));
351 return static_cast<HBITMAP
>(::LoadBitmap(hIns, lpszFileName));
365 static HBITMAP
LoadOem(UINT uOemBitmapResourceId,
int cx = 0,
int cy = 0, UINT fuLoad = 0)
367 LPCTSTR lpId = MAKEINTRESOURCE(uOemBitmapResourceId);
371 return static_cast<HBITMAP
>(::LoadImage(NULL, lpId, IMAGE_BITMAP, cx, cy, fuLoad));
384 static bool Draw(HDC hdc, HBITMAP hBmp,
int x = 0,
int y = 0)
387 LPARAM l =
reinterpret_cast<LPARAM
>(hBmp);
388 bool r = !! ::DrawState(hdc, NULL, NULL, l, 0, x, y, 0, 0, DST_BITMAP | DSS_NORMAL);
392 if ( ::GetObject(hBmp,
sizeof(BITMAP), &bm) > 0 )
394 HDC tempDC = ::CreateCompatibleDC(hdc);
395 HGDIOBJ oldBmp = ::SelectObject(tempDC, hBmp);
396 bool r =!! ::BitBlt(hdc, x, y, bm.bmWidth, bm.bmHeight, tempDC, 0, 0, SRCCOPY);
397 ::SelectObject(tempDC, oldBmp);
418 if ( ::GetObject(bmp,
sizeof(BITMAP), &bp) <= 0 )
420 return _GetLastError(
"GetObject");
423 BITMAPFILEHEADER fh = { 0 };
425 fh.bfType =
'B' +
'M' * 256;
426 fh.bfOffBits =
sizeof(BITMAPFILEHEADER) +
sizeof(BITMAPINFOHEADER);
427 CWorkMem workHead(
sizeof(BITMAPINFOHEADER) +
sizeof(RGBQUAD) * 256);
428 BITMAPINFOHEADER* pih = (BITMAPINFOHEADER*)workHead.
Ref();
429 ZeroMemory(pih, workHead.
GetSize());
430 pih->biSize =
sizeof(BITMAPINFOHEADER);
431 pih->biWidth = bp.bmWidth;
432 pih->biHeight = bp.bmHeight;
433 pih->biPlanes = bp.bmPlanes;
434 pih->biBitCount = (bitsPixel == 0) ? bp.bmBitsPixel : bitsPixel;
435 pih->biCompression = BI_RGB;
436 pih->biSizeImage = bp.bmWidthBytes * bp.bmHeight;
437 switch(pih->biBitCount)
441 pih->biSizeImage = (((pih->biWidth / 4) + 3) & 0xfffffffc) * pih->biHeight;
442 fh.bfOffBits += (
sizeof(RGBQUAD) * 2);
446 pih->biSizeImage = (((pih->biWidth / 2) + 3) & 0xfffffffc) * pih->biHeight;
447 fh.bfOffBits += (
sizeof(RGBQUAD) * 16);
451 pih->biSizeImage = ((pih->biWidth + 3) & 0xfffffffc) * pih->biHeight;
452 fh.bfOffBits += (
sizeof(RGBQUAD) * 256);
456 pih->biSizeImage = ((pih->biWidth * 2 + 3) & 0xfffffffc) * pih->biHeight;
460 pih->biSizeImage = ((pih->biWidth * 3 + 3) & 0xfffffffc) * pih->biHeight;
464 pih->biSizeImage = pih->biWidth * 4 * pih->biHeight;
467 return ERROR_INVALID_PARAMETER;
470 CWorkMem data(pih->biSizeImage + 256 *
sizeof(RGBQUAD));
471 HDC dc = ::CreateCompatibleDC(NULL);
472 HGDIOBJ old = ::SelectObject(dc, bmp);
473 int r = ::GetDIBits(dc, bmp, 0, pih->biHeight, data.
Ref(),
reinterpret_cast<LPBITMAPINFO
>(pih), DIB_RGB_COLORS);
476 return _GetLastError(
"GetDIBits");
478 ::SelectObject(dc, old);
480 fh.bfSize = fh.bfOffBits + pih->biSizeImage;
483 size_t l1 =
sizeof(fh);
486 size_t l2 =
sizeof(BITMAPINFOHEADER) +
sizeof(RGBQUAD) * paletCount;
488 LPCVOID P3 = data.
Ref();
489 size_t l3 = pih->biSizeImage;
491 _save.
Resize(l1 + l2 + l3);
492 BYTE* B = _save.
Ref();
499 return ERROR_SUCCESS;
505 BITMAPINFOHEADER header;
507 ZeroMemory(&fh,
sizeof(fh));
508 fh.bfType =
'B' +
'M' * 256;
509 fh.bfOffBits =
sizeof(BITMAPFILEHEADER) +
sizeof(BITMAPINFOHEADER);
513 return _GetLastError(
"GetObject");
515 size_t len = header.biWidth * 4 * header.biHeight;
516 fh.bfSize = fh.bfOffBits + len;
519 size_t l1 =
sizeof(fh);
521 LPCVOID P2 = &header;
522 size_t l2 =
sizeof(header);
527 _save.
Resize(l1 + l2 + l3);
528 BYTE* B = _save.
Ref();
536 return ERROR_SUCCESS;
549 static DWORD
Save(LPCTSTR lpszFileName, HBITMAP bmp, WORD bitsPixel = 0)
553 if ( r != ERROR_SUCCESS )
558 HANDLE hFile = ::CreateFile(lpszFileName, GENERIC_WRITE, FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, 0, NULL);
559 if ( hFile == INVALID_HANDLE_VALUE )
561 return _GetLastError(
"CreateFile");
566 DWORD r = _GetLastError(
"WriteFile");
567 ::CloseHandle(hFile);
570 ::CloseHandle(hFile);
571 return ERROR_SUCCESS;
CBitmapHandle(void)
コンストラクタ.
CBitmapHandle(const CString &strFile, int cx=0, int cy=0, UINT fuLoad=LR_CREATEDIBSECTION)
代入コンストラクタ.
bool GetSize(SIZE &_size) const
[取得] ビットマップサイズ取得.
static HBITMAP Load(LPCTSTR lpszFileName, int cx=0, int cy=0, UINT fuLoad=LR_CREATEDIBSECTION)
[読込] BMPファイル読込み.
CBitmapHandle(HBITMAP P)
代入コンストラクタ.
CBitmapHandle(UINT uBitmapResourceId, int cx=0, int cy=0, UINT fuLoad=LR_CREATEDIBSECTION)
代入コンストラクタ.
DWORD Save(LPCTSTR lpszFileName, WORD bitsPixel=0)
[作成] BMPファイル作成.
void SetClone(HBITMAP hBmp)
[設定] HBITMAPセット.
bool Draw(HDC hdc, int x=0, int y=0) const
[処理] イメージ描画.
CBitmapHandle(const CBitmapHandle &other)
コピーコンストラクタ.
static HBITMAP Load(UINT uBitmapResourceId, int cx=0, int cy=0, UINT fuLoad=LR_CREATEDIBSECTION)
[読込] リソース読込み
static DWORD SaveMemory(CWorkMem &_save, HBITMAP bmp, WORD bitsPixel=0)
[作成] BMPファイルイメージ作成.
static HBITMAP Create32bitDibSection(HBITMAP hbm, RGBQUAD *&_pRgb, BITMAPINFOHEADER *pHeader=NULL)
[作成] 32bitビットマップ作成.
static DWORD Save(LPCTSTR lpszFileName, HBITMAP bmp, WORD bitsPixel=0)
[作成] BMPファイル作成.
static HBITMAP LoadOem(UINT uOemBitmapResourceId, int cx=0, int cy=0, UINT fuLoad=0)
[読込] OEMリソース読込み
HBITMAP Detach(void)
[取得] デタッチ.
static bool Draw(HDC hdc, HBITMAP hBmp, int x=0, int y=0)
[処理] イメージ描画.
CBitmapHandle(LPCTSTR lpszFile, int cx=0, int cy=0, UINT fuLoad=LR_CREATEDIBSECTION)
代入コンストラクタ.
CPointerHandleBaseT & operator=(HBITMAP t)
[代入] 代入.
bool IsNull(void) const
[確認] NULLチェック
size_t GetSize(void) const
[取得] サイズ取得
void Resize(size_t l)
[設定] サイズ再設定
const TYP * Ref(void) const
[取得] ポインタ取得
DWORD ToDword(LPCSTR lpsz, int iBase=10)
[変換] INT変換(ASCII/SJIS用).
void Zero(V &value)
[設定] ゼロクリア.
HINSTANCE GetInstanceHandleByTnb(EInstanceType type=EI_Process)
[取得] インスタンスハンドル取得.
void MemCopy(T *_pDst, const void *pSrc, size_t len)
[複製] メモリコピー