none
BS_OWNERDRAW

    Question

  • I've runtime runtime error when i use BS_OWNERDRAW:

    m_myButton.Create(L"My Button",BS_PUSHBUTTON | WS_VISIBLE |
    WS_CHILD | BS_OWNERDRAW,CRect(20,20,140,52),this,1);

    And my CMyButton:

    class CMyButton : public CButton
    {
    	DECLARE_DYNAMIC(CMyButton)
    
    public:
    	CMyButton();
    	virtual ~CMyButton();
    
    protected:
    	DECLARE_MESSAGE_MAP()
    public:
    	afx_msg void OnDrawItem(int nIDCtl, LPDRAWITEMSTRUCT lpDrawItemStruct);
    	afx_msg void OnMeasureItem(int nIDCtl, LPMEASUREITEMSTRUCT lpMeasureItemStruct);
    	virtual BOOL OnCreateAggregates();
    private:
    	CBrush m_bgBrush;
    	CBitmap bitmap;
    public:
    	afx_msg HBRUSH OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor);
    };


    When i remove BS_OWNERDRAW it's no errors, but i need BS_OWNERDRAW. Please help me!
    • Edited by thanhvinh Tuesday, August 25, 2009 3:27 AM
    Tuesday, August 25, 2009 3:18 AM

Answers

  • That's because you need to override DrawItem instead of OnDrawItem and MeasureItem instead of OnMeasureItem. Onxxxxxx functions are meant to be overridden by dialog classes or parents of these controls. The parent then calls DrawItem for each control.

    There is an ASSERT in CWnd::DrawItem just to help you out in such cases since there is no default owner drawing code.

    Check out this sample in MSDN -> CButton::DrawItem .
    Microsoft MVP - Visual C++
    Blog: http://nibuthomas.com
    Posts are provided as is without warranties or guaranties.
    • Marked as answer by Nancy Shao Monday, August 31, 2009 6:24 AM
    Tuesday, August 25, 2009 4:18 AM
    Moderator