在MFC中利用SDK显示位图,遇到下面的问题
1、
int CTestView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CScrollView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
m_lpbi = (LPBITMAPINFO) malloc(sizeof( LPBITMAPINFO ) + 256 * sizeof( RGBQUAD ));
memset(m_lpbi, 0, sizeof( LPBITMAPINFO ) + 256 * sizeof( RGBQUAD ));
m_lpbi->bmiHeader.biSize = sizeof( BITMAPINFOHEADER );
m_lpbi->bmiHeader.biCompression = BI_RGB;
/* for(int i = 0; i < 256; ++i)
{
m_lpbi->bmiColors[ i ].rgbBlue = (BYTE) i;
m_lpbi->bmiColors[ i ].rgbGreen = (BYTE) i;
m_lpbi->bmiColors[ i ].rgbRed = (BYTE) i;
m_lpbi->bmiColors[ i ].rgbReserved = 0;
}
*/
return 0;
}
加上注释部分,会导致ToolBar初始化失败,是不是VC 6 本身的BUG?
2、
void CTestView::OnDraw(CDC* pDC)
{
// TODO: add draw code for native data here
TRACE("Enter OnDraw\n");
if(m_img.lpImg && m_lpbi)
{
HDC hDC = pDC->GetSafeHdc();
HDC hdc = ::CreateCompatibleDC( hDC );
RECT rc;
GetClientRect( &rc );
/* m_lpbi->bmiHeader.biBitCount = m_img.unBpp;
m_lpbi->bmiHeader.biWidth = m_img.lWidth;
m_lpbi->bmiHeader.biHeight = m_img.lHeight;
m_lpbi->bmiHeader.biSizeImage = m_img.dwSize;
*/
// ::SetDIBitsToDevice(hDC, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top,
// 0, 0, 0, m_img.lHeight, (void*)m_img.lpImg, m_lpbi, DIB_PAL_COLORS);
/* LPLOGPALETTE lpPal = (LPLOGPALETTE) malloc(sizeof(LOGPALETTE) + 256 * sizeof(PALETTEENTRY));
lpPal->palVersion = 0x300;
lpPal->palNumEntries = 256;
for(int i = 0; i < 256; ++i)
{
lpPal->palPalEntry[ i ].peBlue = (BYTE) i;
lpPal->palPalEntry[ i ].peGreen = (BYTE) i;
lpPal->palPalEntry[ i ].peRed = (BYTE) i;
lpPal->palPalEntry[ i ].peFlags = 0;
}
HPALETTE hPal = ::CreatePalette( lpPal );
HPALETTE hOldPal = ::SelectPalette(hdc, hPal, TRUE);
::RealizePalette( hdc );
*/ ::StretchDIBits(hdc, 0, 0, m_img.lWidth, m_img.lHeight,
0, 0, m_img.lWidth, m_img.lHeight, m_img.lpImg, m_lpbi, DIB_RGB_COLORS, SRCCOPY);
// ::SelectPalette(hdc, hOldPal, FALSE);
::BitBlt(hDC, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, hdc, 0, 0, SRCCOPY);
::DeleteDC( hdc );
// ::DeleteObject( hPal );
// free( lpPal );
}
}
无论怎样,就是不显示,我要显示8位位图,m_img.lpImg是位图元素首地址。