0

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();
}
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
SNC
  • 59
  • 2
  • 15

1 Answers1

2

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

xMRi
  • 14,982
  • 3
  • 26
  • 59
  • The code project article is a bit dated. Since Windows Vista, one can use [custom draw](https://msdn.microsoft.com/en-us/library/windows/desktop/bb761847(v=vs.85).aspx) instead, which allows you to keep the `BS_DEFPUSHBUTTON` style because `BS_OWNERDRAW` is not required. – zett42 Jan 14 '18 at 00:35