如何覆盖BS_SPLITBUTTON按钮的DrawItem [英] How do I override DrawItem for BS_SPLITBUTTON buttons

查看:498
本文介绍了如何覆盖BS_SPLITBUTTON按钮的DrawItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我创建一个新的按钮类继承自 CButton ,指定 BS_OWNERDRAW 样式,并覆盖 DrawItem 方法我可以控制按钮的呈现,如下所示:

  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(LFoo,WS_CHILD | WS_VISIBLE | BS_OWNERDRAW,CRect(0,150,100,200),this,IDC_FOO);

我得到下面的结果:





这是我所希望的对于。然而,如果我继承我从CSplitButton,添加 BS_SPLITBUTTON 样式的按钮,我得到以下结果:





这是不是我希望的结果(我希望它看起来与 CButton 完全相同)。在这种情况下, DrawItem 方法没有被调用,所以我猜测 BS_SPLITBUTTONS p>

如何获得我想要的结果,一个带有自定义绘图的按钮分割按钮?

解决方案

因为它在MSDN 中为 BS_OWNERDRAW 说:


创建自绘图形按钮。当按钮的视觉方面已经改变时,所有者窗口接收到WM_DRAWITEM
消息。 不要
将BS_OWNERDRAW样式与任何其他按钮样式组合




如果你自己绘制图形,你必须自己做


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?

解决方案

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.

这篇关于如何覆盖BS_SPLITBUTTON按钮的DrawItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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