How do I override DrawItem for BS_SPLITBUTTON butt

2019-07-27 22:39发布

问题:

If I make a new button class inheriting from CButton, specifying the BS_OWNERDRAW style , and overriding the DrawItem method I can control the rendering of the button, like this:

void CMyButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) {
    CDC dc;
    dc.Attach(lpDrawItemStruct->hDC);
    RECT rect = lpDrawItemStruct->rcItem;
    dc.FillSolidRect(&rect, RGB(0, 0xFF, 0));
    dc.Detach();
}

m_button.Create(L"Foo", WS_CHILD | WS_VISIBLE | BS_OWNERDRAW, CRect(0, 150, 100, 200), this, IDC_FOO);

I get the following result:

which is what I hoped for. However if I instead inherit my button from CSplitButton, which adds the BS_SPLITBUTTON style, I get the following result:

which is not the result I had hoped for (I had hoped it would look exactly the same as the CButton). The DrawItem method isn't being called in this case so I guess BS_SPLITBUTTONS are treated differently by the system.

How can I get the result I want, a button with split button behaviour with customized drawing?

回答1:

As it says in MSDN for BS_OWNERDRAW:

Creates an owner-drawn button. The owner window receives a WM_DRAWITEM message when a visual aspect of the button has changed. Do not combine the BS_OWNERDRAW style with any other button styles.

(My emphasis).

If you're doing your own drawing, you have to do all of it yourself.



标签: c++ mfc