-
March 29th, 2011, 10:02 PM
#1
OwnerDrawn CComboBoxEx
I want to owner draw a CComboBoxEx m_ctlCombo with text and images.
I create the image list and create COMBOBOXEXITEM and insert them into m_ctlCombo. It's all good.
Code:
// Add strings to the combo box.
for (int idx = 0; idx < 3; idx++)
{
// Each item has text, an lParam with extra data, and an image.
cbI.mask = CBEIF_TEXT | CBEIF_IMAGE | CBEIF_SELECTEDIMAGE;
cbI.pszText = _T("test"); // this will be different for each item
cbI.cchTextMax = sizeof(_T("test"));
cbI.iItem = idx; // Add the item to the end of the list.
cbI.iImage = idx; // Image to display.
cbI.iSelectedImage = idx;
cbI.iOverlay = 2;
cbI.iIndent = 0;
// Add the item to the combo box drop-down list.
int test = m_ctlCombo.InsertItem(&cbI); // works!
}
My question is how do I get the COMBOBOXEXITEM items in my ownerdrawn function from which I can extract the text "test" to draw.
Code:
// COwnerDraw
LRESULT DrawItem(UINT /*uMsg*/, WPARAM wParam, LPARAM lParam, BOOL& /*bHandled*/)
{
LPDRAWITEMSTRUCT lpDrawItemStruct = (LPDRAWITEMSTRUCT) lParam;
if( lpDrawItemStruct->itemID == -1 )
return 0;
CDCHandle dc = lpDrawItemStruct->hDC;
const int nIndex = lpDrawItemStruct->itemID;
COMBOBOXEXITEM cbei;
GetItem(&cbei); // this Does Not work to get items with text
LPCTSTR lpszText = cbei.pszText; // How?
RECT rc = lpDrawItemStruct->rcItem; // this works to get rect
CImageList pImageList = GetImageList(); // this works to get image list
// ...
// code to draw text and images
// ...
}
I'm using WTL but I think it does not matter. TIA
-
March 30th, 2011, 03:11 AM
#2
Re: OwnerDrawn CComboBoxEx
Originally Posted by cbpetro
Code:
// COwnerDraw
LRESULT DrawItem(UINT /*uMsg*/, WPARAM wParam, LPARAM lParam, BOOL& /*bHandled*/)
{
LPDRAWITEMSTRUCT lpDrawItemStruct = (LPDRAWITEMSTRUCT) lParam;
if( lpDrawItemStruct->itemID == -1 )
return 0;
CDCHandle dc = lpDrawItemStruct->hDC;
const int nIndex = lpDrawItemStruct->itemID;
COMBOBOXEXITEM cbei;
GetItem(&cbei); // this Does Not work to get items with text
LPCTSTR lpszText = cbei.pszText; // How?
RECT rc = lpDrawItemStruct->rcItem; // this works to get rect
CImageList pImageList = GetImageList(); // this works to get image list
}
Why don't you use DRAWITEMSTRUCT itemData to get the item text?
Have a look at the owner draw comobox example.
Victor Nijegorodov
-
March 30th, 2011, 03:33 AM
#3
Re: OwnerDrawn CComboBoxEx
I'm working with the CComboBoxEx class which uses strings and images stored in COMBOBOXEXITEM items. The CComboBox class which you referenced just uses strings.
I want to be able to access the COMBOBOXEXITEMS in my owner draw function to get the images and strings which I inserted into my owner drawn control using ...
Code:
m_ctlCombo.InsertItem(&cbI)
-
March 30th, 2011, 03:37 AM
#4
Re: OwnerDrawn CComboBoxEx
Again:
1. Did you try to use DRAWITEMSTRUCT itemData to get the item text?
2. Did you look at the owner draw combobox example from MSDN?
Victor Nijegorodov
-
March 30th, 2011, 03:52 AM
#5
Re: OwnerDrawn CComboBoxEx
Yes, I checked out the MSDN example and tried...
Code:
CDCHandle dc = lpDrawItemStruct->hDC;
// COMBOBOXEXITEM cbei = (COMBOBOXEXITEM) lpDrawItemStruct->itemData; // Won't compile
LPCTSTR lpszText = (LPCTSTR) lpDrawItemStruct->itemData; // does't access strings - gives junk
I don't think that's the solution. I scoured the net for a solution/example and haven't found out how to do this. Thanks for your ideas.
-
April 1st, 2011, 07:00 AM
#6
Re: OwnerDrawn CComboBoxEx
I figured it out.
You have to 'prep' COMBOBOXEXITEM prior to calling GetITem.
Code:
const int nIndex = lpDrawItemStruct->itemID;
TCHAR tstr[20];
COMBOBOXEXITEM cbei;
ZeroMemory( &cbei, sizeof( COMBOBOXEXITEM ) );
cbei.mask = CBEIF_TEXT;
cbei.iItem = nIndex;
cbei.pszText = tstr;
cbei.cchTextMax = 20;
GetItem(&cbei); // now it works !
// tstr is the assigned string
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|