OwnerDraw CButton mfc 焦点
如果我有确定"和取消",则使用标准按钮,默认为确定",然后按右箭头,取消焦点,按键盘上的 Enter 键调用取消按钮功能.
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.
所有者绘制按钮不会发生这种情况.如果我按下右箭头,取消按钮将获得焦点,但按下键盘上的 enter 则会调用 OK 按钮功能.
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.
我怎样才能拥有一个具有标准行为的 ownerdraw 按钮?
How can I have an ownerdraw button with standard behaviour?
这是我的课.
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标志是互斥的.
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.
查看这篇文章:它解释了完整的背景:http://www.codeproject.com/Articles/1318/COddButton
Check this article: It explains the complete Background: http://www.codeproject.com/Articles/1318/COddButton
相关文章