將圖象列表轉換成裝置無關點陣圖 (轉)

gugu99發表於2008-05-24
將圖象列表轉換成裝置無關點陣圖 (轉)[@more@]  在我的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 Imageinffailed
  return NULL;
  }

  // Create compatible stuff and 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* pOlitmap = memDC.Select( &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 );
}

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10748419/viewspace-1004511/,如需轉載,請註明出處,否則將追究法律責任。

相關文章