OwnerDraw CButton mfc焦点 [英] OwnerDraw CButton mfc focus

查看:249
本文介绍了OwnerDraw CButton mfc焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用标准按钮,如果我有确定和取消,默认为OK,然后按右箭头键取消取消,按下键盘上的回车按钮取消按钮功能。



这与ownerdraw按钮不会发生。如果我按右箭头取消按钮被关注,但按下键盘上的确定按钮功能被称为。

我如何拥有一个标准行为的所有者按钮?



这是我的课。

  BEGIN_MESSAGE_MAP(CFlatButton,CButton) 
// {{{AFX_MSG_MAP(CMyClass)
//注意 - ClassWizard将在这里添加和删除映射宏。
//}} AFX_MSG_MAP
END_MESSAGE_MAP()

void CFlatButton :: DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// TODO:将您的代码添加到绘制指定的项目
CDC dc;
dc.Attach(lpDrawItemStruct-> hDC); //获取设备上下文对象
CRect rt;
rt = lpDrawItemStruct-> rcItem; //获取按钮rect

UINT状态= lpDrawItemStruct-> itemState; //获取按钮的状态
if((state& ODS_SELECTED))
dc.FillSolidRect(rt,RGB(255,0,0)); ((state& ODS_DISABLED))
{
dc.FillSolidRect(rt,RGB(0,255,0));
else
{
if


{
if((state& ODS_FOCUS))//如果按钮被聚焦
{
//绘制焦点矩形它表示用户
//该按钮的焦点是
dc.FillSolidRect(rt,RGB(0,0,255));
}
else
{
dc.FillSolidRect(rt,RGB(255,255,0));
}
}
}
dc.SetTextColor(RGB(255,255,255)); //将标题的颜色设置为黄色
CString strTemp;
GetWindowText(strTemp); //获取已设置的标题
dc.DrawText(strTemp,rt,DT_CENTER | DT_VCENTER | DT_SINGLELINE); //绘制标题


dc.Detach();


解决方案

主要原因是对话框通常使用BS_DEFPUSHBUTTON和BS_PUSHBUTTON来表明这一点,但是ownerdraw标志是互斥的。



检查这篇文章:它解释了完整的背景:
< a href =http://www.codeproject.com/Articles/1318/COddButton =nofollow> http://www.codeproject.com/Articles/1318/COddButton


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

解决方案

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

这篇关于OwnerDraw CButton mfc焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆