Reputation: 11
I've an old Visual C++ program where a Owner Draw Combobox is filled with bitmap images. I need to rewrite the program making the combobox able to load jpeg or png instead of bitmaps. I've read about the CImage class and so i've rewritten the code changing CBitmap to CImage
Loading phase:
...
CImage image;
image.Load(filepath);
_ComboBoxBmp.AddImage(image,filename);
...
Loading function
void CComboBoxBmp::AddImage(CImage img, CString text)
{
int nIndex = GetCount();
CImage* image = new CImage;
image->Attach(img.Detach());
m_pBmpList.AddTail(image);
int n = CComboBox::AddString(text);
if (n != CB_ERR && n != CB_ERRSPACE)
{
int r = SetItemData(n, (DWORD)image);
int Width = image->GetWidth();
int Height = image->GetHeight();
m_nItemWidth = 50*Width/100;
m_nItemHeight = 50*Height/100;
for (int i = -1; i < GetCount(); i++)
SetItemHeight(i, m_nItemHeight + 6);
}
}
Drawing phase
void CComboBoxBmp::OutputBitmap(LPDRAWITEMSTRUCT lpDIS, bool selected)
{
const CImage *image = (CImage*)GetItemDataPtr(lpDIS->itemID);
if (image && image != (const CImage *)(0xffffffff))
{
CDC* pDC = CDC::FromHandle(lpDIS->hDC);
CString string;
CPoint point;
point.x = lpDIS->rcItem.left + 2;
point.y = lpDIS->rcItem.top + ((lpDIS->rcItem.bottom - lpDIS->rcItem.top) / 2) - (m_nItemHeight / 2);
DrawBitmap(image, pDC, point);
}
}
The problems are in the last function:
void CComboBoxBmp::DrawBitmap(const CImage *image, CDC *pDC, const CPoint &point)
{
CDC memDC;
memDC.CreateCompatibleDC((CDC*)pDC);
BITMAP bm;
((CBitmap*)bitmap)->GetBitmap(&bm);
CBitmap *pBmp = memDC.SelectObject((CBitmap*)bitmap);
pDC->StretchBlt(point.x, point.y, m_nItemWidth, m_nItemHeight, &memDC, 0, 0, bm.bmWidth, bm.bmHeight, SRCCOPY );
memDC.SelectObject(pBmp);
}
I don't know how to rewrite it because CDC::SelectObject accepts CBitmap but not CImages and all that i can obtain avter after several attempts is a dropdown list from the combobox with all blank images
Anyone can help to find a similar function SelectObject for the CImage class?
Upvotes: 1
Views: 773
Reputation: 2143
You can use CBitmap::FromHandle
for conversion from CImage
to CBitmap
.
CImage
class has an operator HBITMAP()
, so it is available.
CImage image;
// ...
CBitmap* pBitmap = CBitmap::FromHandle(image);
Upvotes: 0