Click to See Complete Forum and Search --> : Customer Control (Owner Draw ListBox)


April 20th, 1999, 06:57 PM
Hi All,

Dids anybody know how to use GDI instead of Bitmap picture to fill in the Listbox ? I got one project regarding the customer
control (owner-draw listbox) which display a list of shapes. There are Ellipse, Rectangle, triangle,......

Following was the code for DRAWITEM function by using Bitmap object. My question is that how can I replace it by
using GDI object ?

Thanks.

Kevin
------------------------------------------------------------------------------------------------------------------------------------------------

void CMyListBox::DrawItem(LPDRAWITEMSTRUCT lpDIS)
{
CDC* pDC = CDC::FromHandle(lpDIS->hDC); // create an MFC CDC

// this function will be called even if the listbox is empty so be prepared to
// do something, in this I simply return otherwise show a focus rectangle
if (lpDIS->itemID == -1) {
return;
}

// cast the data item into a CDataEntry
CDataEntry *pData = reinterpret_cast<CDataEntry *> (lpDIS->itemData);
CString text = pData->GetName();
//GetText(lpDIS->itemID, text); valid only if LBS_HASSTRINGS is set

// make a shorthand for the rectangle area
CRect r = lpDIS->rcItem;

// to draw the item, first get the bitmap, then blit it into the rectangular
// area, then draw the text
if (lpDIS->itemAction & ODA_DRAWENTIRE) {
------> BITMAP bmpInfo; // information structure for a bitmap
------> CBitmap *pBmp = pData->GetPicture(); // get pointer to bitmap
------> pBmp->GetObject(sizeof(BITMAP), &bmpInfo); // fill in the info structure
------> CDC memDC; // create a local dc for the bitmap
------> memDC.CreateCompatibleDC(pDC); // make it compatible with the pDC
------>
------> // select in the bitmap then copy the bitmap to the pDC context
------> CBitmap *pOldBitmap = memDC.SelectObject(pBmp);
------> pDC->BitBlt(r.left, r.top, bmpInfo.bmWidth, bmpInfo.bmHeight, &memDC, 0, 0, SRCCOPY);
------> memDC.SelectObject(pOldBitmap);

// draw the text offset by the width of the bitmap
pDC->SetBkMode(TRANSPARENT);
pDC->TextOut(r.left + bmpInfo.bmWidth + 5, r.top + 5, text);
}

// draw a focus rectangle for highlighting the item, if the user
// clicks another item, we call DrawFocusRect() again to erase the
// previous rectangle

if ((lpDIS->itemState & ODS_SELECTED) && (lpDIS->itemAction & (ODA_SELECT | ODA_DRAWENTIRE)))
{
// item has been selected - hilite frame
COLORREF crHilite = RGB(255-GetRValue(m_bkColor), 255-GetGValue(m_bkColor), 255-GetBValue(m_bkColor));
CBrush br(crHilite);
pDC->FrameRect(&r, &br);
}

if (!(lpDIS->itemState & ODS_SELECTED) && (lpDIS->itemAction & ODA_SELECT))
{
// Item has been de-selected -- remove frame
CBrush br(m_bkColor);
pDC->FrameRect(&lpDIS->rcItem, &br);
}
}