0
使用標準按鈕(如果我有OK並取消),默認爲OK,然後按右箭頭取消焦點並按下鍵盤上的回車鍵取消按鈕功能。OwnerDraw CButton mfc焦點
這與ownerdraw按鈕不會發生。如果我按下右箭頭,取消按鈕將被聚焦,但按下鍵盤上的確定按鈕將調用OK按鈕功能。
我如何擁有標準行爲的所有者按鈕?
這是我的班。
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();
}
代碼項目的文章是有點過時了。自Windows Vista以來,可以使用[自定義繪製](https://msdn.microsoft.com/en-us/library/windows/desktop/bb761847(v = vs.85).aspx),它可以讓您保留BS_DEFPUSHBUTTON樣式,因爲不需要BS_OWNERDRAW。 – zett42