Bitmap Buttons |
|
Overview |
A bitmap button is a button that display a picture or a picture and text on its face. This is usually used the button a little explicit. There are two ways you can create a bitmap button: with or without an existing resource identifier. A bitmap button is created using the CBitmapButton class, which is a child of CButton, which in turn is a descendent of the CWnd class. This means that it can use many of the method of CWnd. Because the bitmap button is in fact a customized version of a button, you must first declare a variable or pointer of it: |
CBitmapButton *btnMap = new CBitmapButton; Using the variable or pointer, you can call the Create() method to formally create the button. Here is an example: BOOL CDialog6Dlg::OnInitDialog() { CDialog::OnInitDialog(); . . . // TODO: Add extra initialization here CBitmapButton *btnMap = new CBitmapButton; btnMap->Create(NULL, WS_CHILD | WS_VISIBLE | BS_OWNERDRAW, CRect(10,10,100,100), this, IDC_BTN_NEW); return TRUE; // return TRUE unless you set the focus to a control }
Creating a bitmap button without an identifier is considered creating it from scratch. Creating a bitmap button is more practical because you can easily use the messages of such a button and refer to it in code. Therefore, before creating a bitmap button, you can first add a button to a dialog box and specify its identifier. For a button to display a picture, it must have the BS_OWNERDRAW style. This means that you should check or set to True the Owner Draw option. Visually, the most appealing aspect of a bitmap button is the picture it displays. The bitmap can be designed following the same tools and instructions we reviewed for the icons. To give a 3-D or realistic appearance to the button, you should mimic the focus effect when designing the bitmaps. Therefore, you should create one to four bitmaps for the control. Here is how they would work:
Since these options, to use a bitmap button to its fullest, you should strive to provide four bitmaps. After creating the bitmaps, you can load them into the button. This is done using the LoadBitmaps() method. It comes in two versions as follows: BOOL LoadBitmaps(LPCTSTR lpszBitmapResource, LPCTSTR lpszBitmapResourceSel = NULL, LPCTSTR lpszBitmapResourceFocus = NULL, LPCTSTR lpszBitmapResourceDisabled = NULL ); BOOL LoadBitmaps(UINT nIDBitmapResource, UINT nIDBitmapResourceSel = 0, UINT nIDBitmapResourceFocus = 0, UINT nIDBitmapResourceDisabled = 0 ); Each version takes four arguments. The first version uses the bitmaps as string resources while the second version uses resource identifiers. As stated above, you can one to four bitmaps. As seen on these functions, the first bitmap is required. The first argument must specify the first or default bitmap and is required. The second argument is the name or identifier of the bitmap that would be used when the button is selected. The third is used when the button has focus. The last bitmap is used when the button is disabled. Here is an example: |
BOOL CDialog6Dlg::OnInitDialog() { CDialog::OnInitDialog(); . . . // TODO: Add extra initialization here CBitmapButton *btnMap = new CBitmapButton; btnMap->Create(NULL, WS_CHILD|WS_VISIBLE|BS_OWNERDRAW, CRect(10,10,100,100), this, IDC_BTN_NEW); btnMap->LoadBitmaps(IDB_BMP_BTN1, IDB_BMP_BTN2, IDB_BMP_BTN3, IDB_BMP_BTN4); return TRUE; // return TRUE unless you set the focus to a control }
It is very likely that you may not be able to design the button on one hand and the bitmaps on the other hand at exactly the same dimensions. To adjust these measures, the CBitmapButton class is equipped with the SizeToContent() method. Its syntax is: void SizeToContent(); When this method is called on the button, it resizes it to the size of the bitmaps. If one bitmap is larger and/or taller than the others, if you had loaded more than one, this method would resize the button to the larger and taller bitmap. For this reason, always make sure that you design bitmaps that have the same dimension. Here is an example of using this method: |
BOOL CDialog6Dlg::OnInitDialog() { CDialog::OnInitDialog(); . . . // TODO: Add extra initialization here CBitmapButton *btnMap = new CBitmapButton; btnMap->Create(NULL, WS_CHILD|WS_VISIBLE|BS_OWNERDRAW, CRect(10,10,100,100), this, IDC_BTN_NEW); btnMap->LoadBitmaps(IDB_BMP_BTN1, IDB_BMP_BTN2, IDB_BMP_BTN3, IDB_BMP_BTN4); btnMap->SizeToContent(); return TRUE; // return TRUE unless you set the focus to a control }
As you may realize if you create a bitmap button strictly using this approach, it would not work. The suggestion is to "subclass" your button class so the messages sent to the bitmap button would be applied effectively. The function used to do this is the CWnd::SubclassDlgItem() method and its syntax is: BOOL SubclassDlgItem( UINT nID, CWnd* pParent ); The first argument is the resource ID of the existing button. The second argument is the control that is hosting the button; this is usually the dialog box but it can be another control container. |
|
|
||
Copyright © 2003-2015, FunctionX | ||
|