Pregunta

He escrito el siguiente código, que intenta tomar un mapa de bits de 32x32 (cargado a través del sistema de recursos de MFC) y convertirlo en un mapa de bits de 16x16, para que puedan usarse como las cimagelistas grandes y pequeñas para un CLISTCTRL. Sin embargo, cuando abro el clistctrl, todos los iconos son negros (en una vista pequeña y grande). Antes de comenzar a jugar con el cambio de tamaño, todo funcionó perfectamente a gran vista.

¿Qué estoy haciendo mal?

 // Create the CImageLists
 if (!m_imageListL.Create(32,32,ILC_COLOR24, 1, 1))
 {
  throw std::exception("Failed to create CImageList");
 }
 if (!m_imageListS.Create(16,16,ILC_COLOR24, 1, 1))
 {
  throw std::exception("Failed to create CImageList");
 }

 // Fill the CImageLists with items loaded from ResourceIDs
 int i = 0;
 for (std::vector<UINT>::iterator it = vec.begin(); it != vec.end(); it++, i++)
 {
  CBitmap* bmpBig = new CBitmap();
  bmpBig->LoadBitmap(*it);
  CDC bigDC;
  bigDC.CreateCompatibleDC(m_itemList.GetDC());
  bigDC.SelectObject(bmpBig);

  CBitmap* bmpSmall = new CBitmap();
  bmpSmall->CreateBitmap(16, 16, 1, 24, 0);
  CDC smallDC;
  smallDC.CreateCompatibleDC(&bigDC);
  smallDC.SelectObject(bmpSmall);
  smallDC.StretchBlt(0, 0, 32, 32, &bigDC, 0, 0, 16, 16, SRCCOPY);

  m_imageListL.Add(bmpBig, RGB(0,0,0));
  m_imageListS.Add(bmpSmall, RGB(0,0,0));
 }

 m_itemList.SetImageList(&m_imageListS, LVSIL_SMALL);
 m_itemList.SetImageList(&m_imageListL, LVSIL_NORMAL);
¿Fue útil?

Solución 4

Asegúrese de anular la selección de CBITMAPS después de usarlos:

// Select the objects
CBitmap* ret1 = bigDC.SelectObject(bmpBig);
CBitmap* ret2 = smallDC.SelectObject(bmpSmall);
...
// Do the painting
...
// Deselect
bigDC.SelectObject(ret1);
smallDC.SelectObject(ret2);

Otros consejos

Necesito crear un compatiblleCC para BigDC. es decir, obtenga primero el DC de la ventana actual y haga algo como

bigDC.CreateCompatibleDC(&myWindowHdc);

Está agregando una referencia al objeto CBITMAP local en la lista. La referencia ya no sería válida una vez que esté fuera de bucle. Intente crear el objeto en Heap.

Intente usar createCompatibleBitMap () en lugar de crearBitMap (): los dos mapas de bits deben ser los mismos para que BITBLT/STRINGBLT funcione.

Además, www.gdiwatch.com puede ser útil al depurar problemas como este. Parece abandonado, pero la versión para la descarga también puede funcionar con VS2008.

necesitas hacer un cambio:

bmpSmall->CreateBitmap(16, 16, 1, 32, 0);
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top