0
votes

I am writing a DrawItem override method to modify an application so the text in the ComboBox DropDowns are all centred using the pDC->DrawText function parsing DT_SINGLELINE|DT_VCENTER as the final parameter. The issue I'm having at the moment is I can get the first value repeated in the DropDown but I want a list of all the values displayed in the DropDown.

I'm not certain if there is a fundamental flaw here as in other controls in the application e.g. a ListCtr lpDrawItemStruct->itemData seems to be populated when the DrawItem override is called. However for the case of the ComboBox lpDrawItemStruct->itemData appears empty.

Please can anyone help with this? Below is the code so far.

void CFCDropDown::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC) ;
int nSavedDC = pDC->SaveDC();

//I can't use the following because at this stage lpDrawItemStruct->itemData doesn't contain anything   
//LPCTSTR lpszText = (LPCTSTR)lpDrawItemStruct->itemData ;

//I do however have access to a member variable that contains the list of items I want in the drop down
//m_strListEntry contains a CString of format "ONE;TWO;THREE;FOUR;FIVE;SIX"
CString strFieldValue = m_strListEntry ;

int noOfItems  = GetCount();
CString item;

int iStartPos = 0;
int iFirstDelimiter = 0;
iFirstDelimiter = m_strListEntry.Find(LISTDELIMITER,iStartPos);

int i = iFirstDelimiter + 1;

int iStrLen = strFieldValue.GetLength();
int iNewLen = iStrLen - ++iFirstDelimiter;

item = strFieldValue.Left(i -1) ;

LPCTSTR lpszText = (LPCTSTR)item ;

//At the moment I'm getting "ONE" repeated 6 times. I want a list of all the values displayed in the DropDown. 
pDC->DrawText(lpszText, strlen(lpszText), &lpDrawItemStruct->rcItem, DT_SINGLELINE|DT_VCENTER) ;

pDC->RestoreDC( nSavedDC ); 
}
1
In case you add the ComboBox contents through the data property in the resource editor, enable 'Has Strings' as well. Then you can get the text to draw with GetLBText(lpDIS->itemID,strItem);. Otherwise populate the ComboBox whenever m_strListEntry is set.Anonymous Coward

1 Answers

1
votes

It looks like you do not use lpDrawItemStruct->itemID but always extracting the first item from your m_strListEntry. lpDrawItemStruct->itemID contains the item that is currently being drawn.

On a side note, I recommend replacing CString m_strListEntry with a CStringArray m_arrListEntry. In this case extracting the item will be just a single line of code:

CString item = m_arrListEntry[lpDrawItemStruct->itemID];