c++ - OwnerDraw CButton mfc 焦点

标签 c++ mfc focus ownerdrawn cbutton

使用标准按钮,如果我有 OK 和 Cancel,默认为 OK,我按下右箭头,Cancel 被选中,按下键盘上的 enter 键,Cancel 按钮函数被调用。

ownerdraw 按钮不会发生这种情况。如果我按向右箭头,则“取消”按钮获得焦点,但按键盘上的 enter 键则会调用“确定”按钮功能。

我怎样才能拥有一个具有标准行为的 ownerdraw 按钮?

这是我的课。

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();
}

最佳答案

主要是Dialog一般使用BS_DEFPUSHBUTTON和BS_PUSHBUTTON来表示,而ownerdraw标志与之互斥。

查看这篇文章:它解释了完整的背景: http://www.codeproject.com/Articles/1318/COddButton

关于c++ - OwnerDraw CButton mfc 焦点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31021442/

相关文章:

c++ - Windows MFC 宏

c++ - 如何使子控件处理父 CView 的加速器命令

Delphi:TRadioButton的TabStop问题

c++ - malloc 和 new 的实现差异。堆栈实现?

c++ - 使用自定义比较函数排序时出现错误 :"invalid comparator"

c++ - 加工和管道

c++ - 在编译时获取 std::array 中的元素数量

mfc - 对话使用分析/热图工具

focus - 以编程方式控制网络摄像头的焦点

javascript - 当 Flash 小程序失去焦点时,如何在 JavaScript 中捕获该事件?