将图象列表转换成设备无关位图

类别:VC语言 点击:0 评论:0 推荐:
       在我的PrintTree例子中,我想打印彩色的树控制.对于文本很容易,但是图标就
不简单了.我试着将图象列表转换成DIB,但是我真正需要的是一个方法,能够取出一
个ICON的DIB,而不是整个图象列表。下面就是我使用的具体方法:

HANDLE CAdvancedTreeCtrl::ImageToDIB( CImageList* pImageList, int iImageNumber, CWnd* pWnd, BOOL bOverlay, HTREEITEM actualItem )
{
    // Local Variables
    CBitmap bitmap;
    CWindowDC dc( pWnd );

    CDC memDC;
    CRect rect;
    CPalette    pal;
    IMAGEINFO   imageInfo;

    if( FALSE == pImageList->GetImageInfo( iImageNumber, &imageInfo ) )
    {
        // Getting of the Imageinfos failed
        return NULL;
    }

    // Create compatible stuff and select Bitmap
    if( FALSE == memDC.CreateCompatibleDC( &dc ) )
    {
        // Create failed
        return NULL;
    }

    if( FALSE == bitmap.CreateCompatibleBitmap( &dc,
                                            imageInfo.rcImage.bottom-imageInfo.rcImage.top,
                                            imageInfo.rcImage.right-imageInfo.rcImage.left ) )
    {
        // Create failed
        return NULL;
    }

    CBitmap* pOldBitmap = memDC.SelectObject( &bitmap );
    if( NULL == pOldBitmap )
    {
        // Select failed
        return NULL;
    }

    // Local Variables for Draw
    CPoint point( 0, 0);
    UINT nStyle = ILD_NORMAL;

    // Is there an Overlay
    if( TRUE == bOverlay )
    {
        TV_ITEM     tv_item;

        // Set up the Item-Struct
        tv_item.hItem       = actualItem;

        // Get the full Item-Struct
        GetItem( &tv_item );

        // Set the wanted style
        nStyle = ILD_TRANSPARENT|( tv_item.state & TVIS_OVERLAYMASK );
    }

    // Draw Image to the compatible DC
    if( FALSE == pImageList->Draw( &memDC, iImageNumber, point, nStyle ) )
    {
        // Drawing of the Image failed
        return NULL;
    }

    // Create logical palette if device support a palette
    if( dc.GetDeviceCaps( RASTERCAPS ) & RC_PALETTE )
    {
UINT        nSize   = sizeof(LOGPALETTE) + ( sizeof(PALETTEENTRY) * 256 );
LOGPALETTE* pLP     = (LOGPALETTE*)new BYTE[nSize];
pLP->palVersion     = 0x300;
pLP->palNumEntries = (unsigned short)GetSystemPaletteEntries( dc, 0, 255,
pLP->palPalEntry );

// Create the palette
pal.CreatePalette( pLP );

// Free memory
delete[] pLP;
    }

    memDC.SelectObject( pOldBitmap );

    // Convert the bitmap to a DIB
    return DDBToDIB( bitmap, BI_RGB, &pal );
}

本文地址:http://com.8s8s.com/it/it3651.htm