OwnerDraw CButton mfc focus

2.9k views Asked by At

With standard buttons if I have OK and Cancel, with default on OK and I press the right arrow the Cancel is focused and pressing enter on the keyboard the Cancel button function is called.

This doesn't happen with ownerdraw buttons. If I press the right arrow the Cancel button is focused but pressing enter on the keyboard the OK button function is called.

How can I have an ownerdraw button with standard behaviour?

This is my class.

BEGIN_MESSAGE_MAP(CFlatButton, CButton)
    //{{AFX_MSG_MAP(CMyClass)
        // NOTE - the ClassWizard will add and remove mapping macros here.
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

void CFlatButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
    // TODO: Add your code to draw the specified item
    CDC dc;
    dc.Attach(lpDrawItemStruct->hDC);       //Get device context object
    CRect rt;
    rt = lpDrawItemStruct->rcItem;      //Get button rect

    UINT state = lpDrawItemStruct->itemState;   //Get state of the button
    if ( (state & ODS_SELECTED) )
        dc.FillSolidRect(rt, RGB(255, 0, 0));
    else
    {
        if ((state & ODS_DISABLED))
        {
            dc.FillSolidRect(rt, RGB(0, 255, 0));
        }
        else
        {
            if ((state & ODS_FOCUS))       // If the button is focused
            {
                // Draw a focus rect which indicates the user 
                // that the button is focused
                dc.FillSolidRect(rt, RGB(0, 0, 255));
            }
            else
            {
                dc.FillSolidRect(rt, RGB(255, 255, 0));
            }
        }
    }
    dc.SetTextColor(RGB(255,255,255));      // Set the color of the caption to be yellow
    CString strTemp;
    GetWindowText(strTemp);     // Get the caption which have been set
    dc.DrawText(strTemp,rt,DT_CENTER|DT_VCENTER|DT_SINGLELINE);     // Draw out the caption


    dc.Detach();
}
1

There are 1 answers

1
xMRi On BEST ANSWER

The major reason is that the Dialog normally uses BS_DEFPUSHBUTTON and BS_PUSHBUTTON to indicate this, but the ownerdraw flag is mutually exclusive to that.

Check this article: It explains the complete Background: http://www.codeproject.com/Articles/1318/COddButton