Cool Push Menu Button

Sample Image - coolbtn.jpg

Introduction

This article shows the use of a Push button with a drop down menu, similar to the one found in the Office 2000 suite. The code is encapsulated in a MFC class. The class itself is derived from a CButton class, by deriving from this class most of the button behavior is supplied.

The main core of the code is the drawing routine draw the button and its behavior. Also there is extensive use of mouse trapping code.

The public interface to access the class is shown here...

BOOL Create( LPCTSTR lpszCaption, DWORD dwStyle,
const RECT& rect, CWnd* pParentWnd, UINT nID );
BOOL SetButtonImage(UINT nResourceId, COLORREF crMask);
BOOL AddMenuItem(UINT nMenuId,const CString strMenu, UINT nFlags);

These functions create the class, set the bitmap for the button and add menu items for the drop menu menu.

The bitmap must be added as a bitmap resource in the class and its probably best to use the standard toolbar button size (16x15 pixels).

The color reference for the mask is usually RGB(255,0,255) (Magenta).

Menu items are added to the menu button by the AddMenuItem function. You'll need to create a resource symbol and use the relevant menu flags found in the Windows API under menus (Beyond the scope of this document). Command handlers are then added to the parent windows message map (see the code example).

In the sample shown there the button is constructed in the OnInitDialog handler. The button is created as a Window, but could quite easy be changed to a subclassed dialog item.

There is room for improvement in the class; RemoveMenuItem, ModifyMenuItem could be added and the SetButtonImage could be modified to replace a current image, unfortunately project deadlines prevent me adding this functionality.

History

  • 29 July 2000 - updated source and demo
  • 4 July 2003 - update source and demo

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

转载于:https://www.cnblogs.com/junzhongxu/archive/2008/08/11/1264865.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值