2013-01-09 70 views
0

我正在编写一个DrawItem覆盖方法来修改应用程序,以便ComboBox DropDowns中的文本都以使用pDC-> DrawText函数解析DT_SINGLELINE | DT_VCENTER作为最终参数的 为中心。 我现在遇到的问题是我可以获得DropDown中重复的第一个值,但我想要显示DropDown中显示的所有值 的列表。MFC ComboBox DrawItem问题

我不确定在应用程序的其他控件中是否存在根本缺陷,例如, ListCtr lpDrawItemStruct-> itemData 似乎在调用DrawItem覆盖时被填充。但是对于ComboBox的情况lpDrawItemStruct-> itemData显示为空。

请任何人都可以帮忙吗?以下是目前的代码。

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); 
} 
+0

如果您通过资源编辑器中的data属性添加ComboBox内容,请启用'Has Strings'。然后你可以用GetLBText(lpDIS-> itemID,strItem);'来绘制文本。否则,只要设置了“m_strListEntry”,就填充组合框。 –

回答

1

它看起来像您不使用lpDrawItemStruct->itemID但总是从你的m_strListEntry提取的第一个项目。 lpDrawItemStruct->itemID包含当前正在绘制的项目。

在旁注中,我建议用CStringArray m_arrListEntry替换CString m_strListEntry。在这种情况下,提取项目将只是一行代码:

CString item = m_arrListEntry[lpDrawItemStruct->itemID];