OwnerDraw CButton mfc focus

喜夏-厌秋 提交于 2019-12-04 05:34:57

问题


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:


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



来源:https://stackoverflow.com/questions/31021442/ownerdraw-cbutton-mfc-focus

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!