Dynamic Create ownerdraw ListBox in Mfc

Rocky Shr 41 Reputation points
2020-09-21T13:09:10.107+00:00

I have created my own list box with the base class of CListBox. when I use it in the Dialog box it works fine.
but when I try to create dynamic at runtime it gives an error.

here is my code for it

m_LstList= new x_ListBox;  
		m_LstList->Create(WS_CHILD | WS_VISIBLE | WS_BORDER | WS_HSCROLL | WS_VSCROLL |  
			LBS_OWNERDRAWVARIABLE | LBS_HASSTRINGS, rect, m_hWnd, IDC_X_LIST);  

am I doing anything wrong?
or there is any extra setting I have to make inside x_ListBox class

	class x_ListBox : public CListBox  
	{  
public:  
		x_ListBox();  
		virtual ~x_ListBox();  
		LIST_BOX_TYPE m_ListBoxType;  

// my functions for custom draw

	protected:  
		afx_msg void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);  
		DECLARE_MESSAGE_MAP()  
  
  
};  

26175-image.png

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,753 questions
0 comments No comments
{count} votes

Accepted answer
  1. SM 416 Reputation points
    2020-09-21T13:18:25.077+00:00

    You need to override OnMeasureItem()

    -SM

    0 comments No comments

7 additional answers

Sort by: Most helpful
  1. Rocky Shr 41 Reputation points
    2020-09-21T13:30:11.433+00:00

    hi - SM

    thanks for your reply. but still not working

    I added this line

    	afx_msg void OnMeasureItem(int nIDCtl, LPMEASUREITEMSTRUCT lpMeasureItemStruct);
    

    can't add any item or String to Listbox

    0 comments No comments

  2. Rocky Shr 41 Reputation points
    2020-09-21T13:35:43.367+00:00

    Ok, my Mistake.

    sorry above code is I Made a mistake

    Added these 2 functions from wizard
    afx_msg void MeasureItem(LPMEASUREITEMSTRUCT /lpMeasureItemStruct/);
    afx_msg int CompareItem(LPCOMPAREITEMSTRUCT /lpCompareItemStruct/);

    now it did not give an error.

    but control did not show. (display)


  3. Rocky Shr 41 Reputation points
    2020-09-21T14:01:01.71+00:00

    yes. I have.
    at first, when I click on the dialog box it does not show. but when I move the mouse it comes like this.
    26107-asdf.jpg

    do not know what is wrong. I am having difficulty understanding this.

    my Code for this
    rect = { pf->m_ValueRc.left - 1, pf->m_ValueRc.bottom, pf->m_ValueRc.right + 1, pf->m_ValueRc.bottom + 250 };
    m_LstList= new x_ListBox;
    m_LstList->Create(WS_CHILD | WS_VISIBLE | WS_BORDER | WS_HSCROLL | WS_VSCROLL |
    LBS_OWNERDRAWVARIABLE | LBS_HASSTRINGS, rect, m_hWnd, IDC_X_LIST);
    m_LstList->InitLineWeight();
    m_LstList->SetFont(m_hWnd->GetFont());
    m_LstList->ShowWindow(SW_SHOW);

    thank you for your help.


  4. Rocky Shr 41 Reputation points
    2020-09-21T14:20:59.27+00:00

    Yes. you are right. I am trying to make like a Property sheet. I made my own grid UI. and need to pop up when mouse click on a related field

    it supposed to be like this.
    26212-asdf.jpg

    but it is not taking SetBkMode functions from DrawItem inside xListBox

    dcContext.SetBkMode( TRANSPARENT ); // not useing

    dcContext.TextOut( TxRc.left, TxRc.top, strColour );

    i have use OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) on dialog box Class to repain Text EditBox only but

    if (pWnd == m_Prop.m_TxEdit)  
     {  
     pDC->SetBkMode(TRANSPARENT);  
     pDC->SetTextColor(RGB(200, 200, 200));  
     return m_brBkgnd;  
     }  
     return hbr;  
    

    that image is displayed when I use scroll in Listbox. but when I click on the list box and outside of list box. it like previous image.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.