CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Hybrid View

  1. #1
    Join Date
    Aug 2000
    Location
    Virginia, US
    Posts
    158

    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

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: OwnerDrawn CComboBoxEx

    Quote Originally Posted by cbpetro View Post
    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

  3. #3
    Join Date
    Aug 2000
    Location
    Virginia, US
    Posts
    158

    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)

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    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

  5. #5
    Join Date
    Aug 2000
    Location
    Virginia, US
    Posts
    158

    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.

  6. #6
    Join Date
    Aug 2000
    Location
    Virginia, US
    Posts
    158

    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
  •  





Click Here to Expand Forum to Full Width

Featured