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 53 54 55
|
char bmpfile[] = "Wizard.bmp";
/***********************************************
bool LoadBMPIntoDC ( HDC hDC, LPCTSTR bmpfile )
Takes in a device context and the name of a
bitmap to load. If an error occurs the function
returns false, else the contents of the bmp
are blitted to the HDC
************************************************/
bool LoadBMPIntoDC (HDC hDC, LPCTSTR bmpfile )
{
// check if params are valid
if ( ( NULL == hDC ) || ( NULL == bmpfile ) )
MessageBox(0, "Window Parameters Failed!", "Error!", MB_ICONSTOP | MB_OK);
return false;
// load bitmap into a bitmap handle
HANDLE hBmp = LoadImage ( NULL, bmpfile, IMAGE_BITMAP, 0, 0,
LR_LOADFROMFILE );
if ( NULL == hBmp )
MessageBox(0, "Window Handler Failed!", "Error!", MB_ICONSTOP | MB_OK);
return false; // failed to load image
// bitmaps can only be selected into memory dcs:
HDC dcmem = CreateCompatibleDC ( NULL );
// now select bitmap into the memory dc
if ( NULL == SelectObject ( dcmem, hBmp ) )
{ // failed to load bitmap into device context
DeleteDC ( dcmem );
MessageBox(0, "Window Load Failed!", "Error!", MB_ICONSTOP | MB_OK);
return false;
}
// now get the bmp size
BITMAP bm;
GetObject ( hBmp, sizeof(bm), &bm );
// and blit it to the visible dc
if ( BitBlt ( hDC, 0, 0, bm.bmWidth, bm.bmHeight, dcmem,
0, 0, SRCCOPY ) == 0 )
{ // failed the blit
DeleteDC ( dcmem );
MessageBox(0, "Window Failed!", "Error!", MB_ICONSTOP | MB_OK);
return false;
}
DeleteDC ( dcmem ); // clear up the memory dc
return true;
}
|