How to stretch a bitmap for CImageList::Add()? 
Author Message
 How to stretch a bitmap for CImageList::Add()?

In my application I want to allow the user to customise toolbars by changing
the image on each button, using their own bitmap file on disk as the image.

CImageList::Add() can be used to add new bitmaps to an existing image list
being used for a toolbar. I can get this working, as long as the bitmap
being loaded from disk is the correct size.

 int nImage = 1;
 if (_tcslen(lpszBitmapFileName) > 0 && _taccess(lpszBitmapFileName, 00) ==
0)
 {
  HGDIOBJ hbm = NULL;
  if ((hbm =
    ::LoadImage(
     NULL, lpszBitmapFileName, IMAGE_BITMAP, 0, 0,
     LR_LOADFROMFILE | LR_CREATEDIBSECTION | LR_DEFAULTSIZE)) != NULL)
  {
   BOOL bSuccess = TRUE;
   CBitmap Bitmap;
   if (Bitmap.Attach(hbm))
   {
    nImage = m_imgLargeHotToolBar.Add(&Bitmap, RGB(255, 0, 255));
   }
  }
 }

If the bitmap is not the right size I know I need to StretchBlt it and this
is where I'm stuck - the code below produces a black square rather than the
correct bitmap image on the toolbar: Please help - just put me on the right
track! Basically, I need to end up with a CBitmap object that has my resized
bitmap in it, which I can then pass to CImageList::Add().

  HGDIOBJ hbm = NULL;
  if ((hbm =
    ::LoadImage(
     NULL, lpszBitmapFileName, IMAGE_BITMAP, 0, 0,
     LR_LOADFROMFILE | LR_CREATEDIBSECTION | LR_DEFAULTSIZE)) != NULL)
  {
   BOOL bSuccess = TRUE;
   CBitmap Bitmap;
   if (Bitmap.Attach(hbm))
   {
    BITMAP bm;
    CRect rcSrc(CPoint(0, 0), Bitmap.GetBitmap(&bm) ? CSize(bm.bmWidth,
bm.bmHeight) : CSize(0,0));
    CRect rcDstSmall(CPoint(0, 0), CSize(g_nSmallToolBarImageWidth,
g_nSmallToolBarImageHeight));
    CRect rcDstLarge(CPoint(0, 0), CSize(g_nLargeToolBarImageWidth,
g_nLargeToolBarImageHeight));
    CRect rcDst(rcDstLarge);

    CDC* pdc = GetDC();

    if (pdc)
    {
     CDC memdc;
     // Create a compatible memory DC
     memdc.CreateCompatibleDC(pdc);
     // Select the bitmap into the DC
     CBitmap* poldbmp = memdc.SelectObject(&Bitmap);

     if (rcDst.Size() == rcSrc.Size())
     {
      bSuccess =
       pdc->BitBlt(
        rcDst.left, rcDst.top, rcDst.Width(), rcDst.Height(),
        &memdc, rcSrc.left, rcSrc.top, SRCCOPY);
     }
     else
     {
      pdc->SetStretchBltMode(COLORONCOLOR);
      bSuccess =
       pdc->StretchBlt(
        rcDst.left, rcDst.top, rcDst.Width(), rcDst.Height(),
        &memdc, rcSrc.left, rcSrc.top, rcSrc.Width(), rcSrc.Height(),
SRCCOPY);
     }

     nImage = m_imgLargeHotToolBar.Add(&Bitmap, RGB(255, 0, 255));

     memdc.SelectObject(poldbmp);

     ReleaseDC(pdc);
    }
   }



Fri, 09 Jul 2004 05:51:29 GMT  
 
 [ 1 post ] 

 Relevant Pages 

1. added lines are too fat when Stretching Bitmap in memory DC

2. Stretching a CImageList

3. stretch bitmap

4. Stretch bitmap

5. How can I have a CFormView with background bitmap (tile or stretch)

6. Stretch bitmap

7. Stretch a bitmap

8. Stretch Blt Bitmap

9. stretching a bitmap to print

10. Prining a Bitmap Stretches it

11. Stretch a loaded bitmap ?

12. How to retrieve a transparent bitmap from a CImageList

 

 
Powered by phpBB® Forum Software