??

解决方案 »

  1.   

    当然是从文件中导入位图,即文件是独立与你的程序的
    你也可以从程序的资源中导入
    下面的代码显示了从文件读入位图并显示BOOL CSplashWnd::LoadBmpImage(LPCTSTR lpszBmpFile, CBitmap& bitmap)
    {
    CFile file; if( !file.Open(lpszBmpFile, CFile::modeRead) )
    return FALSE; long nFileLen = file.GetLength();
    BITMAPFILEHEADER bmfHeader; // read file header
    if (file.Read((LPSTR)&bmfHeader, sizeof(bmfHeader)) != sizeof(bmfHeader))
    return FALSE; // file type should be 'bm'
    if (bmfHeader.bfType != ((WORD) ('M' << 8) | 'B')) 
    return FALSE;  // get length of the remainder of the file and allocate memory 
    HGLOBAL hDib=::GlobalAlloc(GMEM_FIXED, nFileLen); 
    if (hDib == NULL) 
    return FALSE;  // read the remainder of the bitmap file. 
    if ( file.ReadHuge((LPSTR)hDib, nFileLen - sizeof(BITMAPFILEHEADER)) != (nFileLen - sizeof(BITMAPFILEHEADER)) ) 

    ::GlobalFree(hDib); 
    return FALSE; 
    }  CPalette* pPal = new CPalette();
    BITMAPINFO &bmInfo=*(LPBITMAPINFO)hDib;  int nColors = bmInfo.bmiHeader.biClrUsed ? bmInfo.bmiHeader.biClrUsed : 1 << bmInfo.bmiHeader.biBitCount;  LPVOID lpDibBits; // pointer to dib bits
    if( bmInfo.bmiHeader.biBitCount> 8 )
    lpDibBits = (LPVOID)((LPDWORD)(bmInfo.bmiColors +
    bmInfo.bmiHeader.biClrUsed) +
    ((bmInfo.bmiHeader.biCompression == BI_BITFIELDS) ? 3 : 0));
    else
    lpDibBits = (LPVOID)(bmInfo.bmiColors + nColors); // create the logical palette
    CClientDC dc(NULL);
    if( nColors <= 256 ) 

    UINT nSize = sizeof(LOGPALETTE) + sizeof(PALETTEENTRY) * nColors; 
    LOGPALETTE* pLP= (LOGPALETTE *) new BYTE[nSize]; 
    pLP->palVersion = 0x300;
    pLP->palNumEntries = nColors; for(int i = 0; i < nColors; i++)
    {
    pLP->palPalEntry[i].peRed = bmInfo.bmiColors[i].rgbRed;
    pLP->palPalEntry[i].peGreen = bmInfo.bmiColors[i].rgbGreen;
    pLP->palPalEntry[i].peBlue = bmInfo.bmiColors[i].rgbBlue;
    pLP->palPalEntry[i].peFlags = 0;
    } pPal->CreatePalette(pLP); delete[] pLP;
    }
    else
    pPal->CreateHalftonePalette(&dc); CPalette* pOldPalette = NULL;
    if (pPal)
    {
    pOldPalette = dc.SelectPalette(pPal, FALSE);
    dc.RealizePalette();
    } BITMAPINFOHEADER &bmiHeader = *(LPBITMAPINFOHEADER)hDib;  HBITMAP hBmp = CreateDIBitmap(dc.m_hDC, // handle to device context 
    &bmiHeader, // pointer to bitmap size and format data 
    CBM_INIT, // initialization flag 
    lpDibBits, // pointer to initialization data 
    &bmInfo, // pointer to bitmap color-format data 
    DIB_RGB_COLORS); // color-data usage  bitmap.Attach(hBmp); if(pOldPalette)
    dc.SelectPalette(pOldPalette, FALSE); ::GlobalFree(hDib); delete pPal; return TRUE;
    }