我有一个老的Visual C++程序,其中一个所有者绘制Combobox填充位图图像。 我需要重写程序,使组合框能够加载JPEG或PNG,而不是位图。 我读过有关CImage类,所以我已经重写代码改变的CBitmap到的CImageCombobox与JPEG图像
加载阶段:
...
CImage image;
image.Load(filepath);
_ComboBoxBmp.AddImage(image,filename);
...
加载功能
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);
}
}
绘图阶段
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);
}
}
问题出在最后的功能:
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);
}
我不知道如何,因为CDC ::选择对象接受的CBitmap但不是CImages,所有这些我都可以在多次尝试后获得avter是从组合框的下拉列表的所有空白影像改写
任何人都可以帮助找到类似于CImage类的函数SelectObject?
你错过['的CImage :: StretchBlt'(http://msdn.microsoft.com/en-us/library/x180d9tc.aspx)? –
韦尔,实际上我仍然在使用CDC :: StretchBlt ...我可以让1尝试 – Eddie